Skip to content

Commit 75d5400

Browse files
authored
DotArrayFilter replace rather than merge
The use of array_merge_recursive rewrites numerically indexed values when attempting to retrieve validated data. Use of the array_replace_recursive function preserves their ordering. Unit test demonstrates the failure of array_merge_recursive's use rather than array_replace_recursive
1 parent 6f045bf commit 75d5400

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

system/Validation/DotArrayFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static function run(array $indexes, array $array): array
4444
$segments
4545
);
4646

47-
$result = array_merge_recursive($result, self::filter($segments, $array));
47+
$result = array_replace_recursive($result, self::filter($segments, $array));
4848
}
4949

5050
return $result;

tests/system/Validation/DotArrayFilterTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,19 @@ public function testRunNestedArray()
180180
];
181181
$this->assertSame($expected, $result);
182182
}
183+
184+
public function testRunReturnOrderedIndices()
185+
{
186+
$data = [
187+
'foo' => [
188+
2 => 'bar',
189+
0 => 'baz',
190+
1 => 'biz',
191+
],
192+
];
193+
194+
$result = DotArrayFilter::run(['foo.2', 'foo.0', 'foo.1'], $data);
195+
196+
$this->assertSame($data, $result);
197+
}
183198
}

0 commit comments

Comments
 (0)