55use Illuminate \Database \Eloquent \Collection as EloquentCollection ;
66use Illuminate \Support \Collection ;
77use OpenSoutheners \LaravelDataMapper \MappingValue ;
8- use Symfony \Component \TypeInfo \Type ;
98
109use function OpenSoutheners \ExtendedPhp \Strings \is_json_structure ;
1110use function OpenSoutheners \LaravelDataMapper \map ;
@@ -17,11 +16,11 @@ final class CollectionDataMapper extends DataMapper
1716 */
1817 public function assert (MappingValue $ mappingValue ): bool
1918 {
20- return ($ mappingValue ->collectClass === Collection::class && is_array ($ mappingValue ->data ))
19+ return $ mappingValue ->collectClass === 'array '
20+ || ($ mappingValue ->collectClass === Collection::class && is_array ($ mappingValue ->data ))
2121 || ($ mappingValue ->collectClass === Collection::class && is_string ($ mappingValue ->data ) && str_contains ($ mappingValue ->data , ', ' ))
22- || $ mappingValue ->preferredType instanceof Type \CollectionType
23- || $ mappingValue ->preferredTypeClass === Collection::class
24- || $ mappingValue ->preferredTypeClass === EloquentCollection::class;
22+ || $ mappingValue ->objectClass === Collection::class
23+ || $ mappingValue ->objectClass === EloquentCollection::class;
2524 }
2625
2726 /**
@@ -40,14 +39,18 @@ public function resolve(MappingValue $mappingValue): void
4039 is_string ($ mappingValue ->data ) => Collection::make (explode (', ' , $ mappingValue ->data )),
4140 default => Collection::make ($ mappingValue ->data ),
4241 };
43-
44- if ($ mappingValue ->preferredTypeClass ) {
45- $ collection = $ collection ->map (fn ($ value ) => map ($ value )->to ($ mappingValue ->preferredTypeClass ));
42+
43+ $ collection = $ collection ->filter ();
44+
45+ if ($ mappingValue ->collectClass === 'array ' ) {
46+ $ mappingValue ->data = $ collection ->all ();
47+
48+ return ;
4649 }
4750
48- // if ($mappingValue->preferredType->getBuiltinType() === Type::BUILTIN_TYPE_ARRAY ) {
49- // $collection = $collection->all( );
50- // }
51+ if ($ mappingValue ->objectClass && $ mappingValue -> objectClass !== Collection::class ) {
52+ $ collection = $ collection ->map ( fn ( $ value ) => map ( $ value )-> to ( $ mappingValue -> objectClass ) );
53+ }
5154
5255 $ mappingValue ->data = $ collection ;
5356 }
0 commit comments