|
6 | 6 |
|
7 | 7 | use Chubbyphp\Api\Dto\Model\ModelResponseInterface; |
8 | 8 |
|
9 | | -final class PetResponse implements ModelResponseInterface |
| 9 | +/** |
| 10 | + * @implements \IteratorAggregate<string, mixed> |
| 11 | + */ |
| 12 | +final readonly class PetResponse implements \IteratorAggregate, ModelResponseInterface |
10 | 13 | { |
11 | | - public string $id; |
12 | | - |
13 | | - public string $createdAt; |
14 | | - |
15 | | - public ?string $updatedAt = null; |
16 | | - |
17 | | - public string $name; |
18 | | - |
19 | | - public ?string $tag = null; |
20 | | - |
21 | 14 | /** |
22 | | - * @var array<VaccinationResponse> |
23 | | - */ |
24 | | - public array $vaccinations; |
25 | | - |
26 | | - public string $_type; |
27 | | - |
28 | | - /** |
29 | | - * @var array<string, array{ |
| 15 | + * @param array<VaccinationResponse> $vaccinations |
| 16 | + * @param array<string, array{ |
30 | 17 | * href: string, |
31 | 18 | * templated: bool, |
32 | 19 | * rel: array<string>, |
33 | 20 | * attributes: array<string, string> |
34 | | - * }> |
| 21 | + * }> $_links |
35 | 22 | */ |
36 | | - public array $_links; |
| 23 | + public function __construct( |
| 24 | + public string $id, |
| 25 | + public string $createdAt, |
| 26 | + public ?string $updatedAt, |
| 27 | + public string $name, |
| 28 | + public ?string $tag, |
| 29 | + public array $vaccinations, |
| 30 | + public string $_type, |
| 31 | + public array $_links = [], |
| 32 | + ) {} |
37 | 33 |
|
38 | 34 | /** |
39 | 35 | * @return array{ |
40 | 36 | * id: string, |
41 | 37 | * createdAt: string, |
42 | 38 | * updatedAt: null|string, |
43 | | - * name: string, tag: null|string, |
| 39 | + * name: string, |
| 40 | + * tag: null|string, |
44 | 41 | * vaccinations: array<array{name: string, _type: string}>, |
45 | 42 | * _type: string, |
46 | 43 | * _links: array<string, array{ |
47 | 44 | * href: string, |
48 | 45 | * templated: bool, |
49 | 46 | * rel: array<string>, |
50 | 47 | * attributes: array<string, string> |
51 | | - * }> |
| 48 | + * }>, |
| 49 | + * ... |
52 | 50 | * } |
53 | 51 | */ |
54 | 52 | public function jsonSerialize(): array |
55 | 53 | { |
56 | | - $vaccinations = []; |
57 | | - foreach ($this->vaccinations as $vaccination) { |
58 | | - $vaccinations[] = $vaccination->jsonSerialize(); |
59 | | - } |
60 | | - |
61 | 54 | return [ |
62 | 55 | 'id' => $this->id, |
63 | 56 | 'createdAt' => $this->createdAt, |
64 | 57 | 'updatedAt' => $this->updatedAt, |
65 | 58 | 'name' => $this->name, |
66 | 59 | 'tag' => $this->tag, |
67 | | - 'vaccinations' => $vaccinations, |
| 60 | + 'vaccinations' => array_map( |
| 61 | + static fn (VaccinationResponse $vaccination) => $vaccination->jsonSerialize(), |
| 62 | + $this->vaccinations |
| 63 | + ), |
68 | 64 | '_type' => $this->_type, |
69 | 65 | '_links' => $this->_links, |
70 | 66 | ]; |
71 | 67 | } |
| 68 | + |
| 69 | + public function getIterator(): \Traversable |
| 70 | + { |
| 71 | + return new \ArrayIterator(get_object_vars($this)); |
| 72 | + } |
72 | 73 | } |
0 commit comments