-
I wonder what would be the recommended way of converting a response containing several items like these:
into set of DTOs? I came up with this solution. In the request class method I used a modified public function createDtoFromResponse(Response $response): mixed
{
$outcome = [];
foreach ($response->json() as $item) {
$outcome[] = PostDto::fromItem($item);
}
return $outcome;
} and the PostDto class: class PostDto
{
public function __construct(
public int $id,
public string $name,
public string $address,
) {
}
public function fromItem($item): self
{
return new static(
$item['id'],
$item['name'],
$item['address'],
);
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
Sammyjo20
Mar 21, 2023
Replies: 1 comment 1 reply
-
Yep, that's pretty much what I do! |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
boryn
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yep, that's pretty much what I do!