Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bentleyo committed Jan 3, 2025
1 parent b978297 commit 17be5be
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
27 changes: 14 additions & 13 deletions src/Resolvers/DataFromSomethingResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,23 @@ public function execute(

if ($payloadCount === 0 || $payloadCount === 1) {
$properties = $pipeline->execute($payloads[0] ?? [], $creationContext);
} else {
$properties = [];

foreach ($payloads as $payload) {
foreach ($pipeline->execute($payload, $creationContext) as $key => $value) {
if (array_key_exists($key, $properties) && ($value === null || $value instanceof Optional)) {
continue;
}
return $this->dataFromArray($class, $creationContext, $payloads, $properties);
}

$properties = [];

$properties[$key] = $value;
foreach ($payloads as $payload) {
foreach ($pipeline->execute($payload, $creationContext) as $key => $value) {
if (array_key_exists($key, $properties) && ($value === null || $value instanceof Optional)) {
continue;
}

$properties[$key] = $value;
}
}

return $this->propertyMorphedData($class, $creationContext, $payloads, $properties)
?? $this->dataFromArrayResolver->execute($class, $properties);
return $this->dataFromArray($class, $creationContext, $payloads, $properties);
}

protected function createFromCustomCreationMethod(
Expand Down Expand Up @@ -117,12 +118,12 @@ protected function createFromCustomCreationMethod(
return $class::$methodName(...$payloads);
}

protected function propertyMorphedData(
protected function dataFromArray(
string $class,
CreationContext $creationContext,
array $payloads,
array $properties,
): ?BaseData {
): BaseData {
$dataClass = $this->dataConfig->getDataClass($class);

if ($dataClass->isAbstract && $dataClass->propertyMorphable) {
Expand All @@ -134,6 +135,6 @@ protected function propertyMorphedData(
}
}

return null;
return $this->dataFromArrayResolver->execute($class, $properties);
}
}
16 changes: 7 additions & 9 deletions src/Resolvers/DataValidationRulesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ public function execute(
): array {
$dataClass = $this->dataConfig->getDataClass($class);

if ($propertyMorphableClass = $this->propertyMorphableClass($dataClass)) {
if ($this->isPropertyMorphable($dataClass)) {
/**
* @var class-string<PropertyMorphableData> $class
*/
$payload = $path->isRoot() ? $fullPayload : Arr::get($fullPayload, $path->get(), []);
$class = $propertyMorphableClass::morph($payload) ?? $class;
$class = $class::morph($payload) ?? $class;
$dataClass = $this->dataConfig->getDataClass($class);
}

Expand Down Expand Up @@ -286,13 +289,8 @@ protected function inferRulesForDataProperty(
);
}

/**
* @return class-string<PropertyMorphableData>
*/
protected function propertyMorphableClass(DataClass $dataClass): ?string
protected function isPropertyMorphable(DataClass $dataClass): bool
{
return $dataClass->isAbstract && $dataClass->propertyMorphable
? $dataClass->name
: null;
return $dataClass->isAbstract && $dataClass->propertyMorphable;
}
}

0 comments on commit 17be5be

Please sign in to comment.