33namespace Holiq \ActionData \Foundation \DataTransferObject ;
44
55use CuyZ \Valinor \Mapper \MappingError ;
6- use CuyZ \Valinor \MapperBuilder ;
6+ use CuyZ \Valinor \Mapper \ TreeMapper ;
77use Holiq \ActionData \Exceptions \InvalidArgumentException ;
88use Illuminate \Database \Eloquent \Model ;
99use Illuminate \Foundation \Http \FormRequest ;
@@ -37,7 +37,7 @@ public static function resolveFrom(mixed $abstract): static
3737 }
3838
3939 throw new InvalidArgumentException (
40- 'Unsupported data type for DTO resolution. Expected FormRequest, Model, or array, got: ' . get_debug_type ($ abstract )
40+ 'Unsupported data type for DTO resolution. Expected FormRequest, Model, or array, got: ' . get_debug_type ($ abstract ),
4141 );
4242 }
4343
@@ -56,13 +56,20 @@ public static function resolve(array $data): static
5656 $ data = static ::applyTransforms ($ data );
5757
5858 /** @var static $instance */
59- $ instance = (new MapperBuilder ())
60- ->mapper ()
59+ $ instance = static ::mapper ()
6160 ->map (signature: static ::class, source: static ::resolveTheArrayKeyForm (data: $ data ));
6261
6362 return $ instance ;
6463 }
6564
65+ /**
66+ * Get the mapper instance
67+ */
68+ protected static function mapper (): TreeMapper
69+ {
70+ return MapperRegistry::getMapper ();
71+ }
72+
6673 /**
6774 * Apply data transformations
6875 *
@@ -77,8 +84,25 @@ protected static function applyTransforms(array $data): array
7784 $ transforms = static ::transforms ();
7885
7986 foreach ($ transforms as $ key => $ transform ) {
87+ // Try exact key match first
8088 if (array_key_exists ($ key , $ data )) {
8189 $ data [$ key ] = $ transform ($ data [$ key ]);
90+
91+ continue ;
92+ }
93+
94+ // Try snake_case version of the key (for camelCase transform keys)
95+ $ snakeKey = Str::snake ($ key );
96+ if ($ snakeKey !== $ key && array_key_exists ($ snakeKey , $ data )) {
97+ $ data [$ snakeKey ] = $ transform ($ data [$ snakeKey ]);
98+
99+ continue ;
100+ }
101+
102+ // Try camelCase version of the key (for snake_case transform keys)
103+ $ camelKey = Str::camel ($ key );
104+ if ($ camelKey !== $ key && array_key_exists ($ camelKey , $ data )) {
105+ $ data [$ camelKey ] = $ transform ($ data [$ camelKey ]);
82106 }
83107 }
84108
0 commit comments