Skip to content

Commit

Permalink
Added option to use ArrayAccess object in set method
Browse files Browse the repository at this point in the history
  • Loading branch information
minwork committed Apr 21, 2020
1 parent 7e0296b commit 42e97a5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,27 +230,27 @@ public static function getNestedElement($array, $keys, $default = null)
/**
* Alias of Arr::setNestedElement
*
* @param array $array
* @param array|ArrayAccess $array Array or object implementing array access to set element on
* @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
* @param mixed $value Value to set
* @return array Copy of an array with element set
* @see Arr::setNestedElement()
*/
public static function set(array $array, $keys, $value): array
public static function set($array, $keys, $value): array
{
return self::setNestedElement($array, $keys, $value);
}

/**
* Set array element specified by keys to the desired value (create missing keys if necessary)
*
* @param array $array
* @param array|ArrayAccess $array Array or object implementing array access to set element on
* @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
* @param mixed $value Value to set
* @return array Copy of an array with element set
* @see Arr::getKeysArray()
*/
public static function setNestedElement(array $array, $keys, $value): array
public static function setNestedElement($array, $keys, $value): array
{
$result = $array;
$keysArray = self::getKeysArray($keys);
Expand Down
29 changes: 29 additions & 0 deletions test/Arr/ArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,35 @@ public function testSetNestedElement($class)
$this->assertSame(Arr::setNestedElement([], '[].[].[]', 'test'), Arr::set([], '[].[].[]', 'test'));
$this->assertSame(Arr::setNestedElement($array, 'test.test2.test3', 'abc'), Arr::set($array, 'test.test2.test3', 'abc'));
$this->assertSame(Arr::setNestedElement($array, 'key1.key2', ['key3' => 'test']), Arr::set($array, 'key1.key2', ['key3' => 'test']));

// Test ArrayAccess

$obj3 = new ArrayObject([
'c' => [
'd' => 1,
],
]);
$array = [
'test' => [
'a' => [
'b' => $obj3,
'test2'
],
],
];

$array = $this->callMethod([$class, 'set'], $array, 'test.a.b.c.d', 2);
$this->assertSame(2, $array['test']['a']['b']['c']['d']);

$array = $this->callMethod([$class, 'set'], $array, 'test.a.b.foo', 'bar');
$this->assertSame('bar', $array['test']['a']['b']['foo']);

$array = $this->callMethod([$class, 'set'], $array, 'test.a.b.x.[]', 'xyz');
$this->assertSame('xyz', $array['test']['a']['b']['x'][0]);

// Test pure array object set
$obj3 = $this->callMethod([$class, 'set'], $obj3, 'c.[].test', true);
$this->assertSame(true, $obj3['c'][0]['test']);
}

/**
Expand Down

0 comments on commit 42e97a5

Please sign in to comment.