Skip to content

Commit

Permalink
Allow Hash::remove() to remove from ArrayAccess objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Corey Taylor committed Jan 29, 2024
1 parent 02f7c1e commit 18c87fd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Utility/Hash.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ public static function remove(array $data, string $path): array

foreach ($data as $k => $v) {
$match = static::_matchToken($k, $token);
if ($match && is_array($v)) {
if ($match && (is_array($v) || $v instanceof ArrayAccess)) {
if ($conditions) {
if (static::_matches($v, $conditions)) {
if ($nextPath !== '') {
Expand Down
25 changes: 25 additions & 0 deletions tests/TestCase/Utility/HashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2146,6 +2146,31 @@ public function testRemoveMulti(): void
$this->assertSame($expected, $result);
}

public function testRemoveArrayAccess(): void
{
$testObject = new ArrayObject([
'name' => 'about',
'vars' => ['title' => 'page title'],
]);

$a = [
'pages' => [
0 => ['name' => 'main'],
1 => $testObject,
],
];

$result = Hash::remove($a, 'pages.1.vars');
$expected = [
'pages' => [
0 => ['name' => 'main'],
1 => $testObject,
],
];
$this->assertSame($expected, $result);
$this->assertSame(['name' => 'about'], $testObject->getArrayCopy());
}

/**
* testCheck method
*/
Expand Down

0 comments on commit 18c87fd

Please sign in to comment.