|
1 | | -<?php |
2 | | - |
3 | | -declare (strict_types = 1); |
4 | | - |
5 | | -namespace ReallifeKip\ImmutableBase\Attributes; |
6 | | - |
7 | | -use ReallifeKip\ImmutableBase\Enums\Native; |
8 | | - |
9 | | -/** |
10 | | - * Declares that an array property contains typed elements of a specific |
11 | | - * ImmutableBase subclass, or validated PHP scalar values via Native. |
12 | | - * |
13 | | - * Accepts either a class-string of an ImmutableBase subclass, or a |
14 | | - * Native enum case (Native::string, Native::int, Native::float, Native::bool) |
15 | | - * for primitive typed arrays. |
16 | | - * |
17 | | - * Must be applied to properties typed exactly as `array`. Union types |
18 | | - * or non-array types will trigger InvalidArrayOfUsageException at scan time. |
19 | | - * |
20 | | - * @example #[ArrayOf(OrderItemDTO::class)] public array $items |
21 | | - * @example #[ArrayOf(Native::string)] public array $tags |
22 | | - * @example #[ArrayOf(Native::int)] public array $scores |
23 | | - */ |
24 | | -#[\Attribute(\Attribute::TARGET_PROPERTY)] |
25 | | -final class ArrayOf |
26 | | -{ |
27 | | - /** |
28 | | - * @param class-string|Native $class FQCN of an ImmutableBase subclass, or a Native enum case for primitive typed arrays. |
29 | | - */ |
30 | | - private function __construct( |
31 | | - public string $class |
32 | | - ) {} |
33 | | -} |
| 1 | +<?php |
| 2 | + |
| 3 | +declare (strict_types = 1); |
| 4 | + |
| 5 | +namespace ReallifeKip\ImmutableBase\Attributes; |
| 6 | + |
| 7 | +use ReallifeKip\ImmutableBase\Enums\Native; |
| 8 | + |
| 9 | +/** |
| 10 | + * Declares that an array property contains typed elements of one or more |
| 11 | + * ImmutableBase subclasses, or validated PHP scalar values via Native. |
| 12 | + * |
| 13 | + * Accepts one or more class-strings of ImmutableBase subclasses, or Native |
| 14 | + * enum cases (Native::string, Native::int, Native::float, Native::bool) |
| 15 | + * for primitive typed arrays. When multiple types are given, each element |
| 16 | + * is resolved against the declared types in order (first match wins). |
| 17 | + * |
| 18 | + * Must be applied to properties typed exactly as `array`. Union types |
| 19 | + * or non-array types will trigger InvalidArrayOfUsageException at scan time. |
| 20 | + * |
| 21 | + * @example #[ArrayOf(OrderItemDTO::class)] public array $items |
| 22 | + * @example #[ArrayOf(Native::string)] public array $tags |
| 23 | + * @example #[ArrayOf(Native::int)] public array $scores |
| 24 | + * @example #[ArrayOf(ShippingDTO::class, PickupDTO::class)] public array $deliveries |
| 25 | + */ |
| 26 | +#[\Attribute(\Attribute::TARGET_PROPERTY)] |
| 27 | +final class ArrayOf |
| 28 | +{ |
| 29 | + /** @var list<string> */ |
| 30 | + public readonly array $classes; |
| 31 | + |
| 32 | + /** |
| 33 | + * @param class-string|Native ...$classes One or more FQCNs of ImmutableBase subclasses, or Native enum cases. |
| 34 | + */ |
| 35 | + private function __construct(string | Native ...$classes) |
| 36 | + { |
| 37 | + $this->classes = array_values($classes); |
| 38 | + } |
| 39 | +} |
0 commit comments