Skip to content

Commit 405a06a

Browse files
committed
[Support] Add throwOnNotFound flag to Arr::array
1 parent 51f3af6 commit 405a06a

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

src/Illuminate/Collections/Arr.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,34 @@ public static function add($array, $key, $value)
6363

6464
/**
6565
* Get an array item from an array using "dot" notation.
66+
*
67+
* @param \ArrayAccess|array $array
68+
* @param string|int|null $key
69+
* @param array|null $default
70+
* @param bool $throwOnNotFound
71+
* @return array
72+
*
73+
* @throws \InvalidArgumentException
74+
* @throws \Illuminate\Support\ItemNotFoundException
6675
*/
67-
public static function array(ArrayAccess|array $array, string|int|null $key, ?array $default = []): array
68-
{
76+
public static function array(
77+
ArrayAccess|array $array,
78+
string|int|null $key,
79+
array $default = [],
80+
bool $throwOnNotFound = false
81+
): array {
82+
83+
if(! Arr::has($array, $key)){
84+
85+
if ($throwOnNotFound) {
86+
throw new ItemNotFoundException(
87+
sprintf('Array key [%s] not found.', $key)
88+
);
89+
}
90+
91+
return $default;
92+
}
93+
6994
$value = Arr::get($array, $key, $default);
7095

7196
if (! is_array($value)) {

tests/Support/SupportArrTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,61 @@ public function testItReturnsEmptyArrayForMissingKeyByDefault()
671671
$this->assertSame([], Arr::array($data, 'missing_key'));
672672
}
673673

674+
public function test_it_throws_item_not_found_exception_when_array_is_empty_and_throw_on_not_found_is_true()
675+
{
676+
$this->expectException(ItemNotFoundException::class);
677+
$this->expectExceptionMessage('Array key [roles] not found.');
678+
679+
$data = [];
680+
681+
Arr::array($data, 'roles', [], true);
682+
}
683+
684+
public function test_it_throws_item_not_found_exception_when_key_is_missing_and_throw_on_not_found_is_true()
685+
{
686+
$this->expectException(ItemNotFoundException::class);
687+
$this->expectExceptionMessage('Array key [roles] not found.');
688+
689+
$data = ['name' => 'Taylor'];
690+
691+
Arr::array($data, 'roles', [], true);
692+
}
693+
694+
public function test_it_supports_nested_keys_with_dot_notation()
695+
{
696+
$data = ['user' => ['roles' => ['admin']]];
697+
698+
$result = Arr::array($data, 'user.roles');
699+
700+
$this->assertSame(['admin'], $result);
701+
}
702+
703+
public function test_it_throws_for_nested_non_array_value()
704+
{
705+
$this->expectException(InvalidArgumentException::class);
706+
$this->expectExceptionMessageMatches(
707+
'#^Array value for key \[user.name\] must be an array, string found.#'
708+
);
709+
710+
$data = ['user' => ['name' => 'Taylor']];
711+
712+
Arr::array($data, 'user.name');
713+
}
714+
715+
public function test_it_should_not_throw_when_key_exists_even_if_equal_to_default()
716+
{
717+
$data = ['roles' => []];
718+
719+
// In the current (buggy) version without Arr::has(),
720+
// this will wrongly throw ItemNotFoundException
721+
// because $value === $default ([] === []).
722+
$result = Arr::array($data, 'roles', [], true);
723+
724+
$this->assertSame([], $result); // expected to return the actual empty array
725+
}
726+
727+
728+
674729
public function testHas()
675730
{
676731
$array = ['products.desk' => ['price' => 100]];

0 commit comments

Comments
 (0)