Skip to content

Commit

Permalink
ArrayOfRule, ListOfRule: pass values with keys included to phase 2 (p…
Browse files Browse the repository at this point in the history
…reviously keys were stripped)
  • Loading branch information
mabar committed Jan 19, 2025
1 parent b003277 commit 10651d9
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- 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
- pass values with keys included to phase 2 (previously keys were stripped)
- `FieldContext`, `MappedObjectContext`
- initializes `Type` lazily (performance optimization)
- `ArrayShapeType`
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/ArrayOfRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Orisai\ObjectMapper\Processing\Value;
use Orisai\ObjectMapper\Types\GenericArrayType;
use Orisai\Utils\Arrays\ArrayMerger;
use function array_values;
use function count;
use function get_debug_type;
use function is_array;
Expand Down Expand Up @@ -169,7 +168,7 @@ public function processValue($value, Args $args, FieldContext $context): array
}

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

foreach ($value as $key => $item) {
try {
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/ListOfRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Orisai\ObjectMapper\Types\GenericArrayType;
use Orisai\ObjectMapper\Types\SimpleValueType;
use Orisai\Utils\Arrays\ArrayMerger;
use function array_values;
use function count;
use function is_array;
use function is_int;
Expand Down Expand Up @@ -140,7 +139,7 @@ public function processValue($value, Args $args, FieldContext $context): array
}

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

foreach ($value as $key => $item) {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/PhasedRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface PhasedRule extends Rule
public function processValuePhase1($value, Args $args, FieldContext $context);

/**
* @param list<mixed> $values
* @param array<int|string, mixed> $values
* @param T_ARGS $args
*/
public function processValuePhase2(array $values, Args $args, FieldContext $context): void;
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Rules/ArrayOfRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public function testProcessMultiStepCallOrderWithErrors(): void
$rule = new PhasedTestRule();
$this->ruleManager->addRule($rule);

$value = [$rule::Fail1, 'string' => $rule::Fail3, 'baz', 123];
$value = [0 => $rule::Fail1, 'string' => $rule::Fail3, 1 => 'baz', 2 => 123];
$exception = null;

try {
Expand Down Expand Up @@ -216,7 +216,7 @@ public function testProcessMultiStepCallOrderWithErrors(): void
],
[
'phase' => 2,
'value' => [$rule::Fail3, 'baz', 123],
'value' => ['string' => $rule::Fail3, 1 => 'baz', 2 => 123],
],
[
'phase' => 3,
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Rules/ListOfRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function testProcessMultiStepCallOrderWithErrors(): void
],
[
'phase' => 2,
'value' => [$rule::Fail3, 'baz', 123],
'value' => [42 => $rule::Fail3, 'baz', 123],
],
[
'phase' => 3,
Expand Down

0 comments on commit 10651d9

Please sign in to comment.