Skip to content

Commit bfdcc5d

Browse files
committed
Add withLinks
1 parent bbcfae8 commit bfdcc5d

File tree

4 files changed

+90
-82
lines changed

4 files changed

+90
-82
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
@@ -109,18 +109,16 @@ public function getVaccinations(): array
109109
*/
110110
public function jsonSerialize(): array
111111
{
112-
$vaccinations = [];
113-
foreach ($this->vaccinations as $vaccination) {
114-
$vaccinations[] = $vaccination->jsonSerialize();
115-
}
116-
117112
return [
118113
'id' => $this->id,
119114
'createdAt' => $this->createdAt,
120115
'updatedAt' => $this->updatedAt,
121116
'name' => $this->name,
122117
'tag' => $this->tag,
123-
'vaccinations' => $vaccinations,
118+
'vaccinations' => array_map(
119+
static fn (Vaccination $model) => $model->jsonSerialize(),
120+
$this->getVaccinations()
121+
),
124122
];
125123
}
126124
}

src/Pet/Parsing/PetParsing.php

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

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

0 commit comments

Comments
 (0)