Skip to content

Commit afa6ddf

Browse files
committed
Add withLinks
1 parent cf9889b commit afa6ddf

File tree

4 files changed

+89
-81
lines changed

4 files changed

+89
-81
lines changed

src/Pet/Dto/Collection/PetCollectionResponse.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
* @property PetCollectionSort $sort
1313
* @property array<PetResponse> $items
1414
*
15+
* @phpstan-type Links array<string, array{
16+
* href: string,
17+
* templated: bool,
18+
* rel: array<string>,
19+
* attributes: array<string, string>
20+
* }>
1521
* @phpstan-type JsonSerializedResult array{
1622
* offset: int,
1723
* limit: int,
@@ -28,21 +34,11 @@
2834
* _type: string
2935
* }>,
3036
* _type: string,
31-
* _links: array<string, array{
32-
* href: string,
33-
* templated: bool,
34-
* rel: array<string>,
35-
* attributes: array<string, string>
36-
* }>,
37+
* _links: Links,
3738
* ...
3839
* }>,
3940
* count: int,
40-
* _links: array<string, array{
41-
* href: string,
42-
* templated: bool,
43-
* rel: array<string>,
44-
* attributes: array<string, string>
45-
* }>,
41+
* _links: Links,
4642
* _type: string
4743
* }
4844
*
@@ -71,4 +67,21 @@ public function __construct(
7167
$_links,
7268
);
7369
}
70+
71+
/**
72+
* @param Links $_links
73+
*/
74+
public function withLinks(array $_links): self
75+
{
76+
return new self(
77+
$this->offset,
78+
$this->limit,
79+
$this->filters,
80+
$this->sort,
81+
$this->items,
82+
$this->count,
83+
$this->_type,
84+
$_links
85+
);
86+
}
7487
}

src/Pet/Dto/Model/PetResponse.php

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66

77
use Chubbyphp\Api\Dto\Model\ModelResponseInterface;
88

9+
/**
10+
* @phpstan-type Links array<string, array{
11+
* href: string,
12+
* templated: bool,
13+
* rel: array<string>,
14+
* attributes: array<string, string>
15+
* }>
16+
*/
917
final readonly class PetResponse implements ModelResponseInterface
1018
{
1119
/**
1220
* @param array<VaccinationResponse> $vaccinations
13-
* @param array<string, array{
14-
* href: string,
15-
* templated: bool,
16-
* rel: array<string>,
17-
* attributes: array<string, string>
18-
* }> $_links
21+
* @param Links $_links
1922
*/
2023
public function __construct(
2124
public string $id,
@@ -37,12 +40,7 @@ public function __construct(
3740
* tag: null|string,
3841
* vaccinations: array<array{name: string, _type: string}>,
3942
* _type: string,
40-
* _links: array<string, array{
41-
* href: string,
42-
* templated: bool,
43-
* rel: array<string>,
44-
* attributes: array<string, string>
45-
* }>,
43+
* _links: Links,
4644
* ...
4745
* }
4846
*/
@@ -62,4 +60,21 @@ public function jsonSerialize(): array
6260
'_links' => $this->_links,
6361
];
6462
}
63+
64+
/**
65+
* @param Links $_links
66+
*/
67+
public function withLinks(array $_links): self
68+
{
69+
return new self(
70+
$this->id,
71+
$this->createdAt,
72+
$this->updatedAt,
73+
$this->name,
74+
$this->tag,
75+
$this->vaccinations,
76+
$this->_type,
77+
$_links
78+
);
79+
}
6580
}

src/Pet/Model/Pet.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,16 @@ public function getVaccinations(): array
104104
*/
105105
public function jsonSerialize(): array
106106
{
107-
$vaccinations = [];
108-
foreach ($this->vaccinations as $vaccination) {
109-
$vaccinations[] = $vaccination->jsonSerialize();
110-
}
111-
112107
return [
113108
'id' => $this->id,
114109
'createdAt' => $this->createdAt,
115110
'updatedAt' => $this->updatedAt,
116111
'name' => $this->name,
117112
'tag' => $this->tag,
118-
'vaccinations' => $vaccinations,
113+
'vaccinations' => array_map(
114+
static fn (Vaccination $model) => $model->jsonSerialize(),
115+
$this->getVaccinations()
116+
),
119117
];
120118
}
121119
}

src/Pet/Parsing/PetParsing.php

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -91,29 +91,20 @@ public function getCollectionResponseSchema(ServerRequestInterface $request): Ob
9191
'sort' => $petCollectionResponse->sort->jsonSerialize(),
9292
];
9393

94-
return new PetCollectionResponse(
95-
$petCollectionResponse->offset,
96-
$petCollectionResponse->limit,
97-
$petCollectionResponse->filters,
98-
$petCollectionResponse->sort,
99-
$petCollectionResponse->items,
100-
$petCollectionResponse->count,
101-
$petCollectionResponse->_type,
102-
[
103-
'list' => [
104-
'href' => $this->urlGenerator->generatePath('pet_list', [], $queryParams),
105-
'templated' => false,
106-
'rel' => [],
107-
'attributes' => ['method' => 'GET'],
108-
],
109-
'create' => [
110-
'href' => $this->urlGenerator->generatePath('pet_create'),
111-
'templated' => false,
112-
'rel' => [],
113-
'attributes' => ['method' => 'POST'],
114-
],
94+
return $petCollectionResponse->withLinks([
95+
'list' => [
96+
'href' => $this->urlGenerator->generatePath('pet_list', [], $queryParams),
97+
'templated' => false,
98+
'rel' => [],
99+
'attributes' => ['method' => 'GET'],
115100
],
116-
);
101+
'create' => [
102+
'href' => $this->urlGenerator->generatePath('pet_create'),
103+
'templated' => false,
104+
'rel' => [],
105+
'attributes' => ['method' => 'POST'],
106+
],
107+
]);
117108
})
118109
;
119110
}
@@ -156,35 +147,26 @@ public function getModelResponseSchema(ServerRequestInterface $request): ObjectS
156147
'_type' => $p->const('pet')->default('pet'),
157148
], PetResponse::class, true)->strict()
158149
->postParse(
159-
fn (PetResponse $petResponse) => new PetResponse(
160-
$petResponse->id,
161-
$petResponse->createdAt,
162-
$petResponse->updatedAt,
163-
$petResponse->name,
164-
$petResponse->tag,
165-
$petResponse->vaccinations,
166-
$petResponse->_type,
167-
[
168-
'read' => [
169-
'href' => $this->urlGenerator->generatePath('pet_read', ['id' => $petResponse->id]),
170-
'templated' => false,
171-
'rel' => [],
172-
'attributes' => ['method' => 'GET'],
173-
],
174-
'update' => [
175-
'href' => $this->urlGenerator->generatePath('pet_update', ['id' => $petResponse->id]),
176-
'templated' => false,
177-
'rel' => [],
178-
'attributes' => ['method' => 'PUT'],
179-
],
180-
'delete' => [
181-
'href' => $this->urlGenerator->generatePath('pet_delete', ['id' => $petResponse->id]),
182-
'templated' => false,
183-
'rel' => [],
184-
'attributes' => ['method' => 'DELETE'],
185-
],
186-
]
187-
)
150+
fn (PetResponse $petResponse) => $petResponse->withLinks([
151+
'read' => [
152+
'href' => $this->urlGenerator->generatePath('pet_read', ['id' => $petResponse->id]),
153+
'templated' => false,
154+
'rel' => [],
155+
'attributes' => ['method' => 'GET'],
156+
],
157+
'update' => [
158+
'href' => $this->urlGenerator->generatePath('pet_update', ['id' => $petResponse->id]),
159+
'templated' => false,
160+
'rel' => [],
161+
'attributes' => ['method' => 'PUT'],
162+
],
163+
'delete' => [
164+
'href' => $this->urlGenerator->generatePath('pet_delete', ['id' => $petResponse->id]),
165+
'templated' => false,
166+
'rel' => [],
167+
'attributes' => ['method' => 'DELETE'],
168+
],
169+
]),
188170
)
189171
;
190172
}

0 commit comments

Comments
 (0)