55use Illuminate \Database \Eloquent \Model ;
66use Illuminate \Foundation \Http \FormRequest ;
77use Illuminate \Http \Request ;
8+ use Illuminate \Pipeline \Pipeline ;
89use Illuminate \Support \Collection ;
910use OpenSoutheners \LaravelDto \DataTransferObjects \MappingValue ;
1011use OpenSoutheners \LaravelDto \Enums \BuiltInType ;
@@ -17,8 +18,10 @@ final class Mapper
1718
1819 protected ?string $ dataClass = null ;
1920
21+ protected ?string $ throughClass = null ;
22+
2023 protected ?MappingValue $ fromMappingValue = null ;
21-
24+
2225 protected ?string $ property = null ;
2326
2427 protected array $ propertyTypes = [];
@@ -27,6 +30,10 @@ final class Mapper
2730
2831 public function __construct (mixed $ input )
2932 {
33+ if (is_array ($ input ) && count ($ input ) === 1 ) {
34+ $ input = reset ($ input );
35+ }
36+
3037 if (is_object ($ input )) {
3138 $ this ->dataClass = get_class ($ input );
3239 }
@@ -62,20 +69,12 @@ protected function takeDataFrom(mixed $input): mixed
6269 }
6370
6471 /**
65- * @param array<\Symfony\Component\PropertyInfo\Type> $types
66- *
67- * @internal
72+ * Map values through class.
6873 */
69- public function through (MappingValue $ mappingValue , string $ property , array $ types ): static
74+ public function through (string $ class ): static
7075 {
71- $ this ->fromMappingValue = $ mappingValue ;
72-
73- $ this ->property = $ property ;
74-
75- $ this ->propertyTypes = $ types ;
76+ $ this ->throughClass = $ class ;
7677
77- $ this ->runningFromMapper = true ;
78-
7978 return $ this ;
8079 }
8180
@@ -87,50 +86,25 @@ public function through(MappingValue $mappingValue, string $property, array $typ
8786 */
8887 public function to (?string $ output = null )
8988 {
90- // TODO: Move to ModelMapper class
91- // if ($output && is_a($output, Model::class, true)) {
92- // /** @var Model $model */
93- // $model = new $output;
94-
95- // foreach ($this->data as $key => $value) {
96- // if ($model->isRelation($key) && $model->$key() instanceof BelongsTo) {
97- // $model->$key()->associate($value);
98- // }
99-
100- // $model->fill([$key => $value]);
101- // }
102-
103- // return $model;
104- // }
105-
10689 $ output ??= $ this ->dataClass ;
10790
108- $ mappedValue = $ this ->data ;
109-
110- if (is_array ($ mappedValue )) {
111- $ reflectionClass = $ this ->fromMappingValue ?->class ?? $ output ? new ReflectionClass ($ output ) : null ;
112- } else {
113- $ reflectionClass = $ output ? new ReflectionClass ($ output ) : null ;
114- }
91+ // if (is_array($this->data)) {
92+ // $reflectionClass = $this->fromMappingValue?->class ?? $output ? new ReflectionClass($output) : null;
93+ // } else {
94+ // $reflectionClass = $output ? new ReflectionClass($output) : null;
95+ // }
11596
11697 $ mappingDataValue = new MappingValue (
11798 data: $ this ->data ,
118- allMappingData: ($ this ->runningFromMapper ? $ this ->fromMappingValue ?->allMappingData : $ this ->data ) ?? [],
119- typeFromData: BuiltInType::guess ($ this ->data ),
99+ allMappingData: (!$ this ->runningFromMapper ? $ this ->fromMappingValue ?->allMappingData : $ this ->data ) ?? [],
120100 types: $ this ->propertyTypes ,
121101 objectClass: $ output ,
122- class: $ reflectionClass ,
123- property: $ this ->fromMappingValue ?->property ?? ($ this ->property ? $ reflectionClass ->getProperty ($ this ->property ) : null )
102+ collectClass: $ this ->throughClass ,
124103 );
125104
126- foreach (ServiceProvider::getMappers () as $ mapper ) {
127- if ($ mapper ->assert ($ mappingDataValue )) {
128- $ mappedValue = $ mapper ->resolve ($ mappingDataValue );
129-
130- break ;
131- }
132- }
133-
134- return $ mappedValue ;
105+ return app (Pipeline::class)
106+ ->through (ServiceProvider::getMappers ())
107+ ->send ($ mappingDataValue )
108+ ->then (fn (MappingValue $ mappingValue ) => $ mappingValue ->data );
135109 }
136110}
0 commit comments