Skip to content

Commit 8372827

Browse files
authored
fix(support): support keys with dots in ArrayHelper#get (#832)
1 parent 1cb4b22 commit 8372827

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/Tempest/Support/src/ArrayHelper.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,12 @@ public function get(int|string $key, mixed $default = null): mixed
675675
{
676676
$value = $this->array;
677677

678+
if (isset($value[$key])) {
679+
return is_array($value[$key])
680+
? new self($value[$key])
681+
: $value[$key];
682+
}
683+
678684
$keys = is_int($key)
679685
? [$key]
680686
: explode('.', $key);
@@ -701,6 +707,10 @@ public function has(int|string $key): bool
701707
{
702708
$array = $this->array;
703709

710+
if (isset($array[$key])) {
711+
return true;
712+
}
713+
704714
$keys = is_int($key)
705715
? [$key]
706716
: explode('.', $key);

src/Tempest/Support/tests/ArrayHelperTest.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function test_array_access(): void
6666
$this->assertFalse(isset($array['a']));
6767
}
6868

69-
public function test_arr_get(): void
69+
public function test_get_dot(): void
7070
{
7171
$array = [
7272
'a' => [
@@ -80,6 +80,17 @@ public function test_arr_get(): void
8080
$this->assertSame('default', arr($array)->get('a.x', 'default'));
8181
}
8282

83+
public function test_get(): void
84+
{
85+
$array = [
86+
'b.c' => 'd',
87+
'a' => 'b',
88+
];
89+
90+
$this->assertSame('d', arr($array)->get('b.c'));
91+
$this->assertSame('b', arr($array)->get('a'));
92+
}
93+
8394
public function test_arr_has(): void
8495
{
8596
$array = [
@@ -888,10 +899,10 @@ public function test_add(): void
888899
);
889900

890901
$this->assertSame(
891-
$collection
902+
actual: $collection
892903
->add('name')
893904
->toArray(),
894-
[1, 2, '', null, false, [], 'name'],
905+
expected: [1, 2, '', null, false, [], 'name'],
895906
);
896907
}
897908

0 commit comments

Comments
 (0)