Skip to content

Commit

Permalink
Refactor DTO classes and update constructors
Browse files Browse the repository at this point in the history
Updated the DTO classes Metadata, ResponseData, and Pagination to not be read-only, and added explicit property definitions. The constructors for these classes were refactored to follow a more traditional pattern, with properties being assigned inside the constructor body rather than through public property promotion in the parameter list

Signed-off-by: B24io <app@b24.io>
  • Loading branch information
b24io-sdk committed May 22, 2024
1 parent 5e394c2 commit 2bb553c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 24 deletions.
41 changes: 33 additions & 8 deletions src/Core/Response/DTO/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,41 @@
use DateTimeImmutable;
use Exception;

readonly class Metadata
class Metadata
{
/**
* @readonly
*/
public DateTimeImmutable $requestStartTime;
/**
* @readonly
*/
public string $requestId;
/**
* @readonly
*/
public Context $role;
/**
* @readonly
*/
public float $duration;
/**
* @readonly
*/
public string $message;

public function __construct(
public DateTimeImmutable $requestStartTime,
public string $requestId,
public Context $role,
public float $duration,
public string $message
)
DateTimeImmutable $requestStartTime,
string $requestId,
Context $role,
float $duration,
string $message)
{
$this->requestStartTime = $requestStartTime;
$this->requestId = $requestId;
$this->role = $role;
$this->duration = $duration;
$this->message = $message;
}

/**
Expand All @@ -29,7 +54,7 @@ public static function initFromArray(array $metadata): self
return new self(
new DateTimeImmutable($metadata['request_start_time']),
$metadata['request_id'],
Context::from($metadata['role']),
new Context($metadata['role']),
$metadata['duration'],
$metadata['message']
);
Expand Down
36 changes: 27 additions & 9 deletions src/Core/Response/DTO/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,36 @@

namespace B24io\Loyalty\SDK\Core\Response\DTO;

readonly class Pagination
class Pagination
{
public function __construct(
public ?int $count,
public ?int $total,
public ?int $page,
public ?int $pages,
public ?int $pageSize,
)
/**
* @readonly
*/
public ?int $count;
/**
* @readonly
*/
public ?int $total;
/**
* @readonly
*/
public ?int $page;
/**
* @readonly
*/
public ?int $pages;
/**
* @readonly
*/
public ?int $pageSize;
public function __construct(?int $count, ?int $total, ?int $page, ?int $pages, ?int $pageSize)
{
$this->count = $count;
$this->total = $total;
$this->page = $page;
$this->pages = $pages;
$this->pageSize = $pageSize;
}

/**
* @param array<string,?int> $pagination
* @return self
Expand Down
27 changes: 20 additions & 7 deletions src/Core/Response/DTO/ResponseData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,28 @@

namespace B24io\Loyalty\SDK\Core\Response\DTO;

readonly class ResponseData
class ResponseData
{
/**
* @readonly
*/
public array $result;
/**
* @readonly
*/
public Metadata $metadata;
/**
* @readonly
*/
public Pagination $pagination;

public function __construct(
/**
* @var array<string, mixed>
*/
public array $result,
public Metadata $metadata,
public Pagination $pagination)
array $result,
Metadata $metadata,
Pagination $pagination)
{
$this->result = $result;
$this->metadata = $metadata;
$this->pagination = $pagination;
}
}

0 comments on commit 2bb553c

Please sign in to comment.