Skip to content

Commit 4911378

Browse files
committed
Reduce reused logic
1 parent 1c8b231 commit 4911378

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

src/Resolvers/DataFromSomethingResolver.php

+2-6
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,15 +127,12 @@ 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
*/
136-
if (
137-
count($morphableProperties) === count($dataClass->propertyMorphablePropertyNames)
138-
&& $morph = $class::morph($morphableProperties)
139-
) {
135+
if ($morphableProperties && $morph = $class::morph($morphableProperties)) {
140136
return $this->execute($morph, $creationContext, ...$payloads);
141137
}
142138
}

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

+15-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Spatie\LaravelData\Support;
44

5+
use Illuminate\Support\Arr;
56
use Illuminate\Support\Collection;
67

78
/**
@@ -35,8 +36,7 @@ public function __construct(
3536
public DataStructureProperty $allowedRequestOnly,
3637
public DataStructureProperty $allowedRequestExcept,
3738
public DataStructureProperty $outputMappedProperties,
38-
public DataStructureProperty $transformationFields,
39-
public readonly array $propertyMorphablePropertyNames
39+
public DataStructureProperty $transformationFields
4040
) {
4141
}
4242

@@ -59,4 +59,17 @@ public function prepareForCache(): void
5959
}
6060
}
6161
}
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+
}
6275
}

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)