66
77use JsonSerializable ;
88use ReflectionException ;
9+ use Tempest \Mapper \Casters \CasterFactory ;
910use Tempest \Mapper \Mapper ;
10- use Tempest \Mapper \MapTo ;
1111use Tempest \Mapper \MapTo as MapToAttribute ;
1212use Tempest \Reflection \ClassReflector ;
1313use Tempest \Reflection \PropertyReflector ;
1414use function Tempest \Support \arr ;
1515
1616final readonly class ObjectToArrayMapper implements Mapper
1717{
18+ public function __construct (
19+ private CasterFactory $ casterFactory ,
20+ ) {
21+ }
22+
1823 public function canMap (mixed $ from , mixed $ to ): bool
1924 {
2025 return false ;
@@ -28,8 +33,8 @@ public function map(mixed $from, mixed $to): array
2833 foreach ($ properties as $ propertyName => $ propertyValue ) {
2934 try {
3035 $ property = PropertyReflector::fromParts (class: $ from , name: $ propertyName );
31-
3236 $ propertyName = $ this ->resolvePropertyName ($ property );
37+ $ propertyValue = $ this ->resolvePropertyValue ($ property , $ propertyValue );
3338
3439 $ mappedProperties [$ propertyName ] = $ propertyValue ;
3540 } catch (ReflectionException ) {
@@ -40,6 +45,13 @@ public function map(mixed $from, mixed $to): array
4045 return $ mappedProperties ;
4146 }
4247
48+ private function resolvePropertyValue (PropertyReflector $ property , mixed $ currentPropertyValue ): mixed
49+ {
50+ $ caster = $ this ->casterFactory ->forProperty ($ property );
51+
52+ return $ caster ?->serialize($ currentPropertyValue ) ?? $ currentPropertyValue ;
53+ }
54+
4355 /**
4456 * @return array<string, mixed> The properties name and value
4557 */
0 commit comments