|
2 | 2 |
|
3 | 3 | namespace Tempest\Database\Casters; |
4 | 4 |
|
5 | | -use Closure; |
6 | 5 | use Tempest\Core\Priority; |
7 | 6 | use Tempest\Database\DatabaseContext; |
8 | 7 | use Tempest\Mapper\Attributes\Context; |
9 | 8 | use Tempest\Mapper\Caster; |
10 | 9 | use Tempest\Mapper\Context as MapperContext; |
| 10 | +use Tempest\Mapper\DynamicCaster; |
11 | 11 | use Tempest\Mapper\Exceptions\ValueCouldNotBeCast; |
12 | 12 | use Tempest\Mapper\MapperConfig; |
13 | 13 | use Tempest\Mapper\SerializeAs; |
| 14 | +use Tempest\Reflection\PropertyReflector; |
14 | 15 | use Tempest\Reflection\TypeReflector; |
15 | 16 | use Tempest\Support\Arr; |
16 | 17 | use Tempest\Support\Json; |
|
19 | 20 |
|
20 | 21 | #[Priority(Priority::HIGHEST)] |
21 | 22 | #[Context(DatabaseContext::class)] |
22 | | -final readonly class DataTransferObjectCaster implements Caster |
| 23 | +final readonly class DataTransferObjectCaster implements Caster, DynamicCaster |
23 | 24 | { |
24 | 25 | public function __construct( |
25 | 26 | private MapperConfig $mapperConfig, |
26 | 27 | private MapperContext $context, |
27 | 28 | ) {} |
28 | 29 |
|
29 | | - public static function for(): Closure |
| 30 | + public static function accepts(PropertyReflector|TypeReflector $type): bool |
30 | 31 | { |
31 | | - return fn (TypeReflector $type) => $type->isClass() && $type->asClass()->getAttribute(SerializeAs::class); |
| 32 | + $type = $type instanceof PropertyReflector |
| 33 | + ? $type->getType() |
| 34 | + : $type; |
| 35 | + |
| 36 | + return $type->isClass() && $type->asClass()->getAttribute(SerializeAs::class) !== null; |
32 | 37 | } |
33 | 38 |
|
34 | 39 | public function cast(mixed $input): mixed |
|
0 commit comments