File tree 4 files changed +22
-15
lines changed
4 files changed +22
-15
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,14 +127,13 @@ 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
135
if (
137
- count ($ morphableProperties ) === count ($ dataClass ->propertyMorphablePropertyNames )
138
- && $ morph = $ class ::morph ($ morphableProperties )
136
+ $ morphableProperties && $ morph = $ class ::morph ($ morphableProperties )
139
137
) {
140
138
return $ this ->execute ($ morph , $ creationContext , ...$ payloads );
141
139
}
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 @@ -35,8 +35,7 @@ public function __construct(
35
35
public DataStructureProperty $ allowedRequestOnly ,
36
36
public DataStructureProperty $ allowedRequestExcept ,
37
37
public DataStructureProperty $ outputMappedProperties ,
38
- public DataStructureProperty $ transformationFields ,
39
- public readonly array $ propertyMorphablePropertyNames
38
+ public DataStructureProperty $ transformationFields
40
39
) {
41
40
}
42
41
@@ -59,4 +58,20 @@ public function prepareForCache(): void
59
58
}
60
59
}
61
60
}
61
+
62
+ public function propertiesForMorph (array $ properties ): ?array
63
+ {
64
+ $ requiredPropertyNames = $ this ->properties ->filter (fn (DataProperty $ property ) => $ property ->isForMorph )
65
+ ->pluck ('name ' );
66
+
67
+ $ forMorph = collect ($ properties )
68
+ ->only ($ requiredPropertyNames );
69
+
70
+ // If all required properties are not present, return null
71
+ if ($ forMorph ->count () !== $ requiredPropertyNames ->count ()) {
72
+ return null ;
73
+ }
74
+
75
+ return $ forMorph ->all ();
76
+ }
62
77
}
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