File tree 4 files changed +20
-17
lines changed
4 files changed +20
-17
lines changed Original file line number Diff line number Diff line change 3
3
namespace Spatie \LaravelData \Resolvers ;
4
4
5
5
use Illuminate \Http \Request ;
6
- use Illuminate \Support \Arr ;
7
6
use Spatie \LaravelData \Contracts \BaseData ;
8
7
use Spatie \LaravelData \Contracts \PropertyMorphableData ;
9
8
use Spatie \LaravelData \Enums \CustomCreationMethodType ;
@@ -128,15 +127,12 @@ protected function dataFromArray(
128
127
$ dataClass = $ this ->dataConfig ->getDataClass ($ class );
129
128
130
129
if ($ dataClass ->isAbstract && $ dataClass ->propertyMorphable ) {
131
- $ morphableProperties = Arr:: only ( $ properties , $ dataClass ->propertyMorphablePropertyNames );
130
+ $ morphableProperties = $ dataClass ->propertiesForMorph ( $ properties );
132
131
133
132
/**
134
133
* @var class-string<PropertyMorphableData> $class
135
134
*/
136
- if (
137
- count ($ morphableProperties ) === count ($ dataClass ->propertyMorphablePropertyNames )
138
- && $ morph = $ class ::morph ($ morphableProperties )
139
- ) {
135
+ if ($ morphableProperties && $ morph = $ class ::morph ($ morphableProperties )) {
140
136
return $ this ->execute ($ morph , $ creationContext , ...$ payloads );
141
137
}
142
138
}
Original file line number Diff line number Diff line change @@ -119,10 +119,8 @@ protected function propertyMorphableDataClass(
119
119
}
120
120
121
121
// Restrict to only morphable properties
122
- $ properties = Arr::only ($ properties , $ dataClass ->propertyMorphablePropertyNames );
123
-
124
- // Only morph if all properties are present
125
- if (count ($ properties ) !== count ($ dataClass ->propertyMorphablePropertyNames )) {
122
+ $ properties = $ dataClass ->propertiesForMorph ($ properties );
123
+ if ($ properties === null ) {
126
124
return null ;
127
125
}
128
126
Original file line number Diff line number Diff line change 2
2
3
3
namespace Spatie \LaravelData \Support ;
4
4
5
+ use Illuminate \Support \Arr ;
5
6
use Illuminate \Support \Collection ;
6
7
7
8
/**
@@ -35,8 +36,7 @@ public function __construct(
35
36
public DataStructureProperty $ allowedRequestOnly ,
36
37
public DataStructureProperty $ allowedRequestExcept ,
37
38
public DataStructureProperty $ outputMappedProperties ,
38
- public DataStructureProperty $ transformationFields ,
39
- public readonly array $ propertyMorphablePropertyNames
39
+ public DataStructureProperty $ transformationFields
40
40
) {
41
41
}
42
42
@@ -59,4 +59,17 @@ public function prepareForCache(): void
59
59
}
60
60
}
61
61
}
62
+
63
+ public function propertiesForMorph (array $ properties ): ?array
64
+ {
65
+ $ requiredProperties = $ this ->properties ->filter (fn (DataProperty $ property ) => $ property ->isForMorph )->pluck ('name ' );
66
+ $ forMorph = Arr::only ($ properties , $ requiredProperties ->all ());
67
+
68
+ // If all required properties are not present, return null
69
+ if (count ($ forMorph ) !== $ requiredProperties ->count ()) {
70
+ return null ;
71
+ }
72
+
73
+ return $ forMorph ;
74
+ }
62
75
}
Original file line number Diff line number Diff line change @@ -103,11 +103,7 @@ public function build(ReflectionClass $reflectionClass): DataClass
103
103
allowedRequestOnly: new LazyDataStructureProperty (fn (): ?array => $ responsable ? $ name ::allowedRequestOnly () : null ),
104
104
allowedRequestExcept: new LazyDataStructureProperty (fn (): ?array => $ responsable ? $ name ::allowedRequestExcept () : null ),
105
105
outputMappedProperties: $ outputMappedProperties ,
106
- transformationFields: static ::resolveTransformationFields ($ properties ),
107
- propertyMorphablePropertyNames: $ properties ->filter (fn (DataProperty $ property ) => $ property ->isForMorph )
108
- ->map (fn (DataProperty $ property ) => $ property ->name )
109
- ->values ()
110
- ->all (),
106
+ transformationFields: static ::resolveTransformationFields ($ properties )
111
107
);
112
108
}
113
109
You can’t perform that action at this time.
0 commit comments