Skip to content

Commit 1683122

Browse files
committed
immutable dto
1 parent 7967a55 commit 1683122

15 files changed

+233
-184
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ A simple skeleton to build api's based on the [chubbyphp-framework][1].
2323
## Requirements
2424

2525
* php: ^8.3
26-
* [chubbyphp/chubbyphp-api][2]: ^1.0
26+
* [chubbyphp/chubbyphp-api][2]: ^1.1
2727
* [chubbyphp/chubbyphp-clean-directories][3]: ^1.5.1
2828
* [chubbyphp/chubbyphp-cors][4]: ^1.7.1
2929
* [chubbyphp/chubbyphp-decode-encode][5]: ^1.4
@@ -34,7 +34,7 @@ A simple skeleton to build api's based on the [chubbyphp-framework][1].
3434
* [chubbyphp/chubbyphp-laminas-config-doctrine][10]: ^3.1.1
3535
* [chubbyphp/chubbyphp-laminas-config-factory][11]: ^1.5.1
3636
* [chubbyphp/chubbyphp-negotiation][12]: ^2.3.1
37-
* [chubbyphp/chubbyphp-parsing][13]: ^2.5
37+
* [chubbyphp/chubbyphp-parsing][13]: ^2.5.2
3838
* [doctrine/mongodb-odm][14]: ^2.15.3
3939
* [monolog/monolog][15]: ^3.10
4040
* [ramsey/uuid][16]: ^4.9.2

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
],
1616
"require": {
1717
"php": "^8.3",
18-
"chubbyphp/chubbyphp-api": "^1.0",
18+
"chubbyphp/chubbyphp-api": "^1.1",
1919
"chubbyphp/chubbyphp-clean-directories": "^1.5.1",
2020
"chubbyphp/chubbyphp-cors": "^1.7.1",
2121
"chubbyphp/chubbyphp-decode-encode": "^1.4",
@@ -26,7 +26,7 @@
2626
"chubbyphp/chubbyphp-laminas-config-doctrine": "^3.1.1",
2727
"chubbyphp/chubbyphp-laminas-config-factory": "^1.5.1",
2828
"chubbyphp/chubbyphp-negotiation": "^2.3.1",
29-
"chubbyphp/chubbyphp-parsing": "^2.5",
29+
"chubbyphp/chubbyphp-parsing": "^2.5.2",
3030
"doctrine/mongodb-odm": "^2.15.3",
3131
"monolog/monolog": "^3.10",
3232
"ramsey/uuid": "^4.9.2",

src/Pet/Dto/Collection/PetCollectionFilters.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
use Chubbyphp\Api\Dto\Collection\CollectionFiltersInterface;
88

9-
final class PetCollectionFilters implements CollectionFiltersInterface
9+
final readonly class PetCollectionFilters implements CollectionFiltersInterface
1010
{
11-
public ?string $name = null;
11+
public function __construct(public ?string $name = null) {}
1212

1313
/**
1414
* @return array{name: null|string}

src/Pet/Dto/Collection/PetCollectionRequest.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
use Chubbyphp\Api\Collection\CollectionInterface;
99
use Chubbyphp\Api\Dto\Collection\CollectionRequestInterface;
1010

11-
final class PetCollectionRequest implements CollectionRequestInterface
11+
final readonly class PetCollectionRequest implements CollectionRequestInterface
1212
{
13-
public int $offset;
14-
15-
public int $limit;
16-
17-
public PetCollectionFilters $filters;
18-
19-
public PetCollectionSort $sort;
13+
public function __construct(
14+
public int $offset,
15+
public int $limit,
16+
public PetCollectionFilters $filters,
17+
public PetCollectionSort $sort
18+
) {}
2019

2120
public function createCollection(): CollectionInterface
2221
{

src/Pet/Dto/Collection/PetCollectionResponse.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55
namespace App\Pet\Dto\Collection;
66

77
use App\Pet\Dto\Model\PetResponse;
8-
use Chubbyphp\Api\Dto\Collection\AbstractCollectionResponse;
8+
use Chubbyphp\Api\Dto\Collection\AbstractReadonlyCollectionResponse;
99

1010
/**
11+
* @property PetCollectionFilters $filters
12+
* @property PetCollectionSort $sort
13+
* @property array<PetResponse> $items
14+
*
15+
* @implements \IteratorAggregate<string, mixed>
16+
*
1117
* @phpstan-type JsonSerializedResult array{
1218
* offset: int,
1319
* limit: int,
@@ -44,24 +50,32 @@
4450
*
4551
* @method JsonSerializedResult jsonSerialize()
4652
*/
47-
final class PetCollectionResponse extends AbstractCollectionResponse
53+
final readonly class PetCollectionResponse extends AbstractReadonlyCollectionResponse implements \IteratorAggregate
4854
{
49-
public PetCollectionFilters $filters;
50-
51-
public PetCollectionSort $sort;
52-
53-
/**
54-
* @var array<PetResponse>
55-
*/
56-
public array $items;
57-
58-
protected function getFilters(): PetCollectionFilters
59-
{
60-
return $this->filters;
55+
public function __construct(
56+
int $offset,
57+
int $limit,
58+
PetCollectionFilters $filters,
59+
PetCollectionSort $sort,
60+
array $items,
61+
int $count,
62+
string $_type,
63+
array $_links = [],
64+
) {
65+
parent::__construct(
66+
$offset,
67+
$limit,
68+
$filters,
69+
$sort,
70+
$items,
71+
$count,
72+
$_type,
73+
$_links,
74+
);
6175
}
6276

63-
protected function getSort(): PetCollectionSort
77+
public function getIterator(): \Traversable
6478
{
65-
return $this->sort;
79+
return new \ArrayIterator(get_object_vars($this));
6680
}
6781
}

src/Pet/Dto/Collection/PetCollectionSort.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
use Chubbyphp\Api\Dto\Collection\CollectionSortInterface;
88

9-
final class PetCollectionSort implements CollectionSortInterface
9+
final readonly class PetCollectionSort implements CollectionSortInterface
1010
{
11-
public ?string $name = null;
11+
public function __construct(public ?string $name = null) {}
1212

1313
/**
1414
* @return array{name: null|string}

src/Pet/Dto/Model/PetRequest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
use Chubbyphp\Api\Dto\Model\ModelRequestInterface;
1010
use Chubbyphp\Api\Model\ModelInterface;
1111

12-
final class PetRequest implements ModelRequestInterface
12+
final readonly class PetRequest implements ModelRequestInterface
1313
{
14-
public string $name;
15-
16-
public ?string $tag = null;
17-
1814
/**
19-
* @var array<VaccinationRequest>
15+
* @param array<VaccinationRequest> $vaccinations
2016
*/
21-
public array $vaccinations;
17+
public function __construct(
18+
public string $name,
19+
public ?string $tag,
20+
public array $vaccinations,
21+
) {}
2222

2323
public function createModel(): ModelInterface
2424
{

src/Pet/Dto/Model/PetResponse.php

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,67 +6,68 @@
66

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

9-
final class PetResponse implements ModelResponseInterface
9+
/**
10+
* @implements \IteratorAggregate<string, mixed>
11+
*/
12+
final readonly class PetResponse implements \IteratorAggregate, ModelResponseInterface
1013
{
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-
2114
/**
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{
3017
* href: string,
3118
* templated: bool,
3219
* rel: array<string>,
3320
* attributes: array<string, string>
34-
* }>
21+
* }> $_links
3522
*/
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+
) {}
3733

3834
/**
3935
* @return array{
4036
* id: string,
4137
* createdAt: string,
4238
* updatedAt: null|string,
43-
* name: string, tag: null|string,
39+
* name: string,
40+
* tag: null|string,
4441
* vaccinations: array<array{name: string, _type: string}>,
4542
* _type: string,
4643
* _links: array<string, array{
4744
* href: string,
4845
* templated: bool,
4946
* rel: array<string>,
5047
* attributes: array<string, string>
51-
* }>
48+
* }>,
49+
* ...
5250
* }
5351
*/
5452
public function jsonSerialize(): array
5553
{
56-
$vaccinations = [];
57-
foreach ($this->vaccinations as $vaccination) {
58-
$vaccinations[] = $vaccination->jsonSerialize();
59-
}
60-
6154
return [
6255
'id' => $this->id,
6356
'createdAt' => $this->createdAt,
6457
'updatedAt' => $this->updatedAt,
6558
'name' => $this->name,
6659
'tag' => $this->tag,
67-
'vaccinations' => $vaccinations,
60+
'vaccinations' => array_map(
61+
static fn (VaccinationResponse $vaccination) => $vaccination->jsonSerialize(),
62+
$this->vaccinations
63+
),
6864
'_type' => $this->_type,
6965
'_links' => $this->_links,
7066
];
7167
}
68+
69+
public function getIterator(): \Traversable
70+
{
71+
return new \ArrayIterator(get_object_vars($this));
72+
}
7273
}

src/Pet/Dto/Model/VaccinationRequest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace App\Pet\Dto\Model;
66

7-
final class VaccinationRequest
7+
final readonly class VaccinationRequest
88
{
9-
public string $name;
9+
public function __construct(
10+
public string $name,
11+
) {}
1012
}

src/Pet/Dto/Model/VaccinationResponse.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
namespace App\Pet\Dto\Model;
66

7-
final class VaccinationResponse implements \JsonSerializable
7+
final readonly class VaccinationResponse implements \JsonSerializable
88
{
9-
public string $name;
10-
11-
public string $_type;
9+
public function __construct(
10+
public string $name,
11+
public string $_type,
12+
) {}
1213

1314
/**
1415
* @return array{

0 commit comments

Comments
 (0)