Skip to content

Commit aba25a8

Browse files
committed
add support for array shapes
1 parent dc33c9d commit aba25a8

8 files changed

Lines changed: 666 additions & 275 deletions

File tree

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,44 @@ use Patchlevel\Hydrator\Normalizer\DateTimeImmutableNormalizer;
155155

156156
final class DTO
157157
{
158-
#[ArrayNormalizer(new DateTimeImmutableNormalizer())]
158+
/**
159+
* @var list<DateTimeImmutable>
160+
*/
161+
#[ArrayNormalizer]
159162
public array $dates;
163+
164+
#[ArrayNormalizer(new DateTimeImmutableNormalizer())]
165+
public array $explicitDates;
160166
}
161167
```
162168

163169
> [!NOTE]
164170
> The keys from the arrays are taken over here.
165171
172+
#### ArrayShape
173+
174+
If you have an array with a specific shape, you can use the `ArrayShapeNormalizer`.
175+
176+
```php
177+
use Patchlevel\Hydrator\Normalizer\ArrayShapeNormalizer;
178+
use Patchlevel\Hydrator\Normalizer\DateTimeImmutableNormalizer;
179+
180+
final class DTO
181+
{
182+
/**
183+
* @var array{
184+
* date: DateTimeImmutable,
185+
* otherField: string
186+
* }
187+
*/
188+
#[ArrayShapeNormalizer]
189+
public array $meta;
190+
191+
#[ArrayShapeNormalizer(['date' => new DateTimeImmutableNormalizer()])]
192+
public array $explicitMeta;
193+
}
194+
```
195+
166196
#### DateTimeImmutable
167197

168198
With the `DateTimeImmutable` Normalizer, as the name suggests,

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"psr/cache": "^2.0.0 || ^3.0.0",
2525
"psr/simple-cache": "^2.0.0 || ^3.0.0",
2626
"symfony/event-dispatcher": "^5.4.29 || ^6.4.0 || ^7.0.0",
27-
"symfony/type-info": "^7.2.4"
27+
"symfony/type-info": "^7.3.0"
2828
},
2929
"require-dev": {
3030
"infection/infection": "^0.29.10",

0 commit comments

Comments
 (0)