Skip to content

Commit

Permalink
Reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
bentleyo committed Dec 3, 2022
1 parent dffc46e commit c89dde3
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions src/Resolvers/DataPropertyValidationRulesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,35 +69,27 @@ protected function getNestedRules(
$inferrer->handle($property, $toplevelRules);
}

if (! $this->dataConfig->usesRelativeRuleGeneration()) {
$prefix = match (true) {
$property->type->isDataObject => "{$propertyName}",
$property->type->isDataCollectable => "{$propertyName}.*",
};

return $this->dataValidationRulesResolver
->execute(
$property->type->dataClass,
$payload,
$this->isNestedDataNullable($nullable, $property)
)
->mapWithKeys(fn (array $rules, string $name) => [
"{$prefix}.{$name}" => $rules,
])
->prepend($toplevelRules->normalize(), $propertyName);
$relativeRuleGeneration = $this->dataConfig->usesRelativeRuleGeneration();
if ($relativeRuleGeneration) {
$payload = $payload[$propertyName] ?? [];
}

if ($property->type->isDataCollectable) {
return collect($payload[$propertyName] ?? [])
->flatMap(function ($item, $key) use ($nullable, $property, $propertyName) {
$prefix = match (true) {
$property->type->isDataObject || $relativeRuleGeneration => "{$propertyName}.",
$property->type->isDataCollectable => "{$propertyName}.*.",
};

if ($relativeRuleGeneration && $property->type->isDataCollectable) {
return collect($payload)
->flatMap(function ($item, $key) use ($prefix, $nullable, $property) {
return $this->dataValidationRulesResolver
->execute(
$property->type->dataClass,
$item,
$this->isNestedDataNullable($nullable, $property)
)
->mapWithKeys(fn (array $rules, string $name) => [
"{$propertyName}.{$key}.{$name}" => $rules,
"{$prefix}{$key}.{$name}" => $rules,
]);
})
->prepend($toplevelRules->normalize(), $propertyName);
Expand All @@ -106,11 +98,11 @@ protected function getNestedRules(
return $this->dataValidationRulesResolver
->execute(
$property->type->dataClass,
$payload[$propertyName] ?? [],
$payload,
$this->isNestedDataNullable($nullable, $property)
)
->mapWithKeys(fn (array $rules, string $name) => [
"{$propertyName}.{$name}" => $rules,
"{$prefix}{$name}" => $rules,
])
->prepend($toplevelRules->normalize(), $propertyName);
}
Expand Down

0 comments on commit c89dde3

Please sign in to comment.