Skip to content

Commit 6aadf52

Browse files
fix set_path
1 parent 86f9303 commit 6aadf52

File tree

2 files changed

+64
-35
lines changed

2 files changed

+64
-35
lines changed

src/DotNotation.php

Lines changed: 61 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,53 +12,82 @@
1212
*/
1313
function set_path(array &$haystack, string $path, $value, string $separator = '.'): void
1414
{
15-
$key = trim($path, $separator);
16-
$keys = explode($separator, $key);
17-
if (empty($keys)) {
18-
return;
19-
}
20-
21-
$target = &$haystack;
22-
foreach ($keys as $innerKey) {
23-
if (! is_array($target)) {
24-
break;
25-
}
15+
$path = explode($separator, $path);
16+
$temp =& $haystack;
2617

27-
if (! array_key_exists($innerKey, $target)) {
28-
$target[$innerKey] = [];
18+
foreach ($path as $key) {
19+
if (!is_array($temp)) {
20+
$temp = [];
2921
}
3022

31-
$target = &$target[$innerKey];
23+
$temp =& $temp[$key];
3224
}
3325

34-
$target = $value;
26+
$temp = $value;
27+
//
28+
// $key = trim($path, $separator);
29+
// $keys = explode($separator, $key);
30+
// if (empty($keys)) {
31+
// return;
32+
// }
33+
//
34+
// $target = &$haystack;
35+
// foreach ($keys as $innerKey) {
36+
// if (! is_array($target)) {
37+
// break;
38+
// }
39+
//
40+
// if (! array_key_exists($innerKey, $target)) {
41+
// $target[$innerKey] = [];
42+
// }
43+
//
44+
// $target = &$target[$innerKey];
45+
// }
46+
//
47+
// $target = $value;
3548
}
3649

3750
/**
3851
* @param array $haystack
3952
* @param string $path
4053
* @param $default
4154
* @param string $separator
42-
* @return array|mixed|null
55+
* @return mixed
4356
*/
4457
function get_path(array $haystack, string $path, $default = null, string $separator = '.')
4558
{
46-
$key = trim($path, $separator);
47-
$keys = explode($separator, $key);
48-
if (empty($keys)) {
49-
return $default;
50-
}
51-
52-
$target = $haystack;
53-
foreach ($keys as $innerKey) {
54-
if (! is_array($target) || ! array_key_exists($innerKey, $target)) {
55-
return $default;
56-
}
57-
58-
$target = $target[$innerKey];
59-
}
60-
61-
return $target;
59+
return
60+
array_reduce(
61+
explode($separator, $path),
62+
fn ($arr, $p) => is_array($arr) && array_key_exists($p, $arr) ? $arr[$p] : $default,
63+
$haystack
64+
);
65+
66+
// $path = explode($separator, $path); //if needed
67+
// $temp =& $haystack;
68+
//
69+
// foreach($path as $key) {
70+
// $temp =& $temp[$key];
71+
// }
72+
//
73+
// return $temp;
74+
//
75+
// $key = trim($path, $separator);
76+
// $keys = explode($separator, $key);
77+
// if (empty($keys)) {
78+
// return $default;
79+
// }
80+
//
81+
// $target = $haystack;
82+
// foreach ($keys as $innerKey) {
83+
// if (! is_array($target) || ! array_key_exists($innerKey, $target)) {
84+
// return $default;
85+
// }
86+
//
87+
// $target = $target[$innerKey];
88+
// }
89+
//
90+
// return $target;
6291
}
6392

6493
/**

tests/DotNotationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ public function setPathProvider(): array
4040
['a' => [9, 1]]
4141
],
4242
'case_5' => [
43-
['a' => [0, 1, 'b' => ['c' => 9]]],
44-
'a.b.c.2',
43+
['a' => [0, 1, 'b' => ['c' => 1]]],
44+
'a.b.c.d',
4545
9,
4646
'.',
47-
['a' => [0, 1, 'b' => ['c' => [2 => 9]]]],
47+
['a' => [0, 1, 'b' => ['c' => ['d' => 9]]]],
4848
]
4949
];
5050
}

0 commit comments

Comments
 (0)