Skip to content

Commit

Permalink
Reduce reused logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bentleyo committed Jan 29, 2025
1 parent 1c8b231 commit 8d61583
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
6 changes: 2 additions & 4 deletions src/Resolvers/DataFromSomethingResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Spatie\LaravelData\Resolvers;

use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Spatie\LaravelData\Contracts\BaseData;
use Spatie\LaravelData\Contracts\PropertyMorphableData;
use Spatie\LaravelData\Enums\CustomCreationMethodType;
Expand Down Expand Up @@ -128,14 +127,13 @@ protected function dataFromArray(
$dataClass = $this->dataConfig->getDataClass($class);

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

/**
* @var class-string<PropertyMorphableData> $class
*/
if (
count($morphableProperties) === count($dataClass->propertyMorphablePropertyNames)
&& $morph = $class::morph($morphableProperties)
$morphableProperties && $morph = $class::morph($morphableProperties)
) {
return $this->execute($morph, $creationContext, ...$payloads);
}
Expand Down
6 changes: 2 additions & 4 deletions src/Resolvers/DataValidationRulesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,8 @@ protected function propertyMorphableDataClass(
}

// Restrict to only morphable properties
$properties = Arr::only($properties, $dataClass->propertyMorphablePropertyNames);

// Only morph if all properties are present
if (count($properties) !== count($dataClass->propertyMorphablePropertyNames)) {
$properties = $dataClass->propertiesForMorph($properties);
if ($properties === null) {
return null;
}

Expand Down
19 changes: 17 additions & 2 deletions src/Support/DataClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public function __construct(
public DataStructureProperty $allowedRequestOnly,
public DataStructureProperty $allowedRequestExcept,
public DataStructureProperty $outputMappedProperties,
public DataStructureProperty $transformationFields,
public readonly array $propertyMorphablePropertyNames
public DataStructureProperty $transformationFields
) {
}

Expand All @@ -59,4 +58,20 @@ public function prepareForCache(): void
}
}
}

public function propertiesForMorph(array $properties): ?array
{
$requiredPropertyNames = $this->properties->filter(fn (DataProperty $property) => $property->isForMorph)
->pluck('name');

$forMorph = collect($properties)
->only($requiredPropertyNames);

// If all required properties are not present, return null
if ($forMorph->count() !== $requiredPropertyNames->count()) {
return null;
}

return $forMorph->all();
}
}
6 changes: 1 addition & 5 deletions src/Support/Factories/DataClassFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,7 @@ public function build(ReflectionClass $reflectionClass): DataClass
allowedRequestOnly: new LazyDataStructureProperty(fn (): ?array => $responsable ? $name::allowedRequestOnly() : null),
allowedRequestExcept: new LazyDataStructureProperty(fn (): ?array => $responsable ? $name::allowedRequestExcept() : null),
outputMappedProperties: $outputMappedProperties,
transformationFields: static::resolveTransformationFields($properties),
propertyMorphablePropertyNames: $properties->filter(fn (DataProperty $property) => $property->isForMorph)
->map(fn (DataProperty $property) => $property->name)
->values()
->all(),
transformationFields: static::resolveTransformationFields($properties)
);
}

Expand Down

0 comments on commit 8d61583

Please sign in to comment.