Skip to content

Commit 8d61583

Browse files
committed
Reduce reused logic
1 parent 1c8b231 commit 8d61583

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

src/Resolvers/DataFromSomethingResolver.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Spatie\LaravelData\Resolvers;
44

55
use Illuminate\Http\Request;
6-
use Illuminate\Support\Arr;
76
use Spatie\LaravelData\Contracts\BaseData;
87
use Spatie\LaravelData\Contracts\PropertyMorphableData;
98
use Spatie\LaravelData\Enums\CustomCreationMethodType;
@@ -128,14 +127,13 @@ protected function dataFromArray(
128127
$dataClass = $this->dataConfig->getDataClass($class);
129128

130129
if ($dataClass->isAbstract && $dataClass->propertyMorphable) {
131-
$morphableProperties = Arr::only($properties, $dataClass->propertyMorphablePropertyNames);
130+
$morphableProperties = $dataClass->propertiesForMorph($properties);
132131

133132
/**
134133
* @var class-string<PropertyMorphableData> $class
135134
*/
136135
if (
137-
count($morphableProperties) === count($dataClass->propertyMorphablePropertyNames)
138-
&& $morph = $class::morph($morphableProperties)
136+
$morphableProperties && $morph = $class::morph($morphableProperties)
139137
) {
140138
return $this->execute($morph, $creationContext, ...$payloads);
141139
}

src/Resolvers/DataValidationRulesResolver.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,8 @@ protected function propertyMorphableDataClass(
119119
}
120120

121121
// 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) {
126124
return null;
127125
}
128126

src/Support/DataClass.php

+17-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ public function __construct(
3535
public DataStructureProperty $allowedRequestOnly,
3636
public DataStructureProperty $allowedRequestExcept,
3737
public DataStructureProperty $outputMappedProperties,
38-
public DataStructureProperty $transformationFields,
39-
public readonly array $propertyMorphablePropertyNames
38+
public DataStructureProperty $transformationFields
4039
) {
4140
}
4241

@@ -59,4 +58,20 @@ public function prepareForCache(): void
5958
}
6059
}
6160
}
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+
}
6277
}

src/Support/Factories/DataClassFactory.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,7 @@ public function build(ReflectionClass $reflectionClass): DataClass
103103
allowedRequestOnly: new LazyDataStructureProperty(fn (): ?array => $responsable ? $name::allowedRequestOnly() : null),
104104
allowedRequestExcept: new LazyDataStructureProperty(fn (): ?array => $responsable ? $name::allowedRequestExcept() : null),
105105
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)
111107
);
112108
}
113109

0 commit comments

Comments
 (0)