Skip to content

Commit 42e97a5

Browse files
committed
Added option to use ArrayAccess object in set method
1 parent 7e0296b commit 42e97a5

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/Arr.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,27 +230,27 @@ public static function getNestedElement($array, $keys, $default = null)
230230
/**
231231
* Alias of Arr::setNestedElement
232232
*
233-
* @param array $array
233+
* @param array|ArrayAccess $array Array or object implementing array access to set element on
234234
* @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
235235
* @param mixed $value Value to set
236236
* @return array Copy of an array with element set
237237
* @see Arr::setNestedElement()
238238
*/
239-
public static function set(array $array, $keys, $value): array
239+
public static function set($array, $keys, $value): array
240240
{
241241
return self::setNestedElement($array, $keys, $value);
242242
}
243243

244244
/**
245245
* Set array element specified by keys to the desired value (create missing keys if necessary)
246246
*
247-
* @param array $array
247+
* @param array|ArrayAccess $array Array or object implementing array access to set element on
248248
* @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
249249
* @param mixed $value Value to set
250250
* @return array Copy of an array with element set
251251
* @see Arr::getKeysArray()
252252
*/
253-
public static function setNestedElement(array $array, $keys, $value): array
253+
public static function setNestedElement($array, $keys, $value): array
254254
{
255255
$result = $array;
256256
$keysArray = self::getKeysArray($keys);

test/Arr/ArrTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,35 @@ public function testSetNestedElement($class)
186186
$this->assertSame(Arr::setNestedElement([], '[].[].[]', 'test'), Arr::set([], '[].[].[]', 'test'));
187187
$this->assertSame(Arr::setNestedElement($array, 'test.test2.test3', 'abc'), Arr::set($array, 'test.test2.test3', 'abc'));
188188
$this->assertSame(Arr::setNestedElement($array, 'key1.key2', ['key3' => 'test']), Arr::set($array, 'key1.key2', ['key3' => 'test']));
189+
190+
// Test ArrayAccess
191+
192+
$obj3 = new ArrayObject([
193+
'c' => [
194+
'd' => 1,
195+
],
196+
]);
197+
$array = [
198+
'test' => [
199+
'a' => [
200+
'b' => $obj3,
201+
'test2'
202+
],
203+
],
204+
];
205+
206+
$array = $this->callMethod([$class, 'set'], $array, 'test.a.b.c.d', 2);
207+
$this->assertSame(2, $array['test']['a']['b']['c']['d']);
208+
209+
$array = $this->callMethod([$class, 'set'], $array, 'test.a.b.foo', 'bar');
210+
$this->assertSame('bar', $array['test']['a']['b']['foo']);
211+
212+
$array = $this->callMethod([$class, 'set'], $array, 'test.a.b.x.[]', 'xyz');
213+
$this->assertSame('xyz', $array['test']['a']['b']['x'][0]);
214+
215+
// Test pure array object set
216+
$obj3 = $this->callMethod([$class, 'set'], $obj3, 'c.[].test', true);
217+
$this->assertSame(true, $obj3['c'][0]['test']);
189218
}
190219

191220
/**

0 commit comments

Comments
 (0)