Skip to content

Commit

Permalink
ArrayOfRule, ListOfRule: skip 2nd and 3rd validation phase for item r…
Browse files Browse the repository at this point in the history
…ules that don't require it
  • Loading branch information
mabar committed Jan 19, 2025
1 parent 7f92fb1 commit 0ed26b3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- all rules initialize `Type` lazily (performance optimization)
- `ArrayOfRule`
- check during metadata parsing that default value is an array when `mergeDefaults` is enabled
- `ArrayOfRule`, `ListOfRule`
- skip 2nd and 3rd validation phase for item rules that don't require it
- `FieldContext`, `MappedObjectContext`
- initializes `Type` lazily (performance optimization)
- `ArrayShapeType`
Expand Down
27 changes: 16 additions & 11 deletions src/Rules/ArrayOfRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ public function processValue($value, Args $args, FieldContext $context): array
$itemArgs = $itemMeta->getArgs();
if (!$itemRule instanceof MultiValueEfficientRule) {
$itemRule = new MultiValueEfficientRuleAdapter($itemRule);
$phasedRule = false;
} else {
$phasedRule = true;
}

$keyMeta = $args->keyRuleMeta;
Expand Down Expand Up @@ -165,18 +168,20 @@ public function processValue($value, Args $args, FieldContext $context): array
}
}

$itemRule->processValuePhase2(array_values($value), $args, $context->createClone());
if ($phasedRule) {
$itemRule->processValuePhase2(array_values($value), $args, $context->createClone());

foreach ($value as $key => $item) {
try {
$value[$key] = $itemRule->processValuePhase3(
$item,
$itemArgs,
$context->createClone(),
);
} catch (ValueDoesNotMatch | InvalidData $exception) {
$type ??= $this->createType($args, $context);
$type->addInvalidValue($key, $exception);
foreach ($value as $key => $item) {
try {
$value[$key] = $itemRule->processValuePhase3(
$item,
$itemArgs,
$context->createClone(),
);
} catch (ValueDoesNotMatch | InvalidData $exception) {
$type ??= $this->createType($args, $context);
$type->addInvalidValue($key, $exception);
}
}
}

Expand Down
29 changes: 17 additions & 12 deletions src/Rules/ListOfRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ public function processValue($value, Args $args, FieldContext $context): array
$itemArgs = $itemMeta->getArgs();
if (!$itemRule instanceof MultiValueEfficientRule) {
$itemRule = new MultiValueEfficientRuleAdapter($itemRule);
$phasedRule = false;
} else {
$phasedRule = true;
}

$lastIntKey = -1; // List starts from 0
Expand Down Expand Up @@ -136,18 +139,20 @@ public function processValue($value, Args $args, FieldContext $context): array
}
}

$itemRule->processValuePhase2(array_values($value), $args, $context->createClone());

foreach ($value as $key => $item) {
try {
$value[$key] = $itemRule->processValuePhase3(
$item,
$itemArgs,
$context->createClone(),
);
} catch (ValueDoesNotMatch | InvalidData $exception) {
$type ??= $this->createType($args, $context);
$type->addInvalidValue($key, $exception);
if ($phasedRule) {
$itemRule->processValuePhase2(array_values($value), $args, $context->createClone());

foreach ($value as $key => $item) {
try {
$value[$key] = $itemRule->processValuePhase3(
$item,
$itemArgs,
$context->createClone(),
);
} catch (ValueDoesNotMatch | InvalidData $exception) {
$type ??= $this->createType($args, $context);
$type->addInvalidValue($key, $exception);
}
}
}

Expand Down

0 comments on commit 0ed26b3

Please sign in to comment.