Skip to content

Commit 3fec3fb

Browse files
test dot notation
1 parent 4dbf6b8 commit 3fec3fb

File tree

2 files changed

+158
-15
lines changed

2 files changed

+158
-15
lines changed

src/DotNotation.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,16 @@ function get_path(array $haystack, string $path, $default = null, string $separa
5252
*/
5353
function unset_path(array &$haystack, string $path, string $separator = '.')
5454
{
55-
$key = trim($path, $separator);
56-
$keys = explode($separator, $key);
57-
if (empty($keys)) {
58-
return;
59-
}
60-
61-
$target = &$haystack;
62-
foreach ($keys as $innerKey) {
63-
if (! is_array($target)) {
64-
break;
65-
}
66-
67-
if (array_key_exists($innerKey, $target)) {
68-
$target = &$target[$innerKey];
55+
$keys = explode($separator, $path);
56+
$temp =& $haystack;
57+
while (count($keys) > 1) {
58+
$key = array_shift($keys);
59+
if (array_key_exists($key, $temp) and is_array($temp[$key])) {
60+
$temp =& $temp[$key];
6961
}
7062
}
7163

72-
unset($target);
64+
unset($temp[array_shift($keys)]);
7365
}
7466

7567
/**

tests/DotNotationTest.php

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
use PHPUnit\Framework\TestCase;
77
use function BrenoRoosevelt\get_path;
8+
use function BrenoRoosevelt\has_path;
89
use function BrenoRoosevelt\set_path;
10+
use function BrenoRoosevelt\unset_path;
911

1012
class DotNotationTest extends TestCase
1113
{
@@ -126,4 +128,153 @@ public function testGetPath(array $items, string $path, $default, $expected): vo
126128
$actual = get_path($items, $path, $default);
127129
$this->assertEquals($expected, $actual);
128130
}
131+
132+
public function hasPathProvider(): array
133+
{
134+
$abcdNull = ['a' => ['b' => ['c' => ['d' => null]]]];
135+
return [
136+
'case_1' => [
137+
[],
138+
'',
139+
false
140+
],
141+
'case_2' => [
142+
[1, 2, 3],
143+
'0',
144+
true
145+
],
146+
'case_3' => [
147+
[1, 2, 3],
148+
'2',
149+
true
150+
],
151+
'case_4' => [
152+
[[[[1, 2, 3]]]],
153+
'0.0.0.2',
154+
true
155+
],
156+
'case_5' => [
157+
[[[[1, 2, 3]]]],
158+
'0.0.0.3',
159+
false
160+
],
161+
'case_6' => [
162+
[[[[1, 2, 3]]]],
163+
'0.0',
164+
true
165+
],
166+
'case_7' => [
167+
[[[0, 1 => [1, 2], [1, 2, 3]]]],
168+
'0.0.1',
169+
true
170+
],
171+
'case_8' => [
172+
$abcdNull,
173+
'0',
174+
false
175+
],
176+
'case_9' => [
177+
$abcdNull,
178+
'a',
179+
true
180+
],
181+
'case_10' => [
182+
$abcdNull,
183+
'a.b',
184+
true
185+
],
186+
'case_11' => [
187+
$abcdNull,
188+
'a.b.c',
189+
true
190+
],
191+
'case_12' => [
192+
$abcdNull,
193+
'a.b.c.d',
194+
true
195+
],
196+
'case_13' => [
197+
['a' => null],
198+
'a',
199+
true
200+
],
201+
'case_14' => [
202+
['a' => ['b' => null]],
203+
'a.b',
204+
true
205+
],
206+
'case_15' => [
207+
['a' => ['b' => false]],
208+
'a.b',
209+
true
210+
],
211+
'case_16' => [
212+
['a' => ['b' => 0]],
213+
'a.b',
214+
true
215+
],
216+
];
217+
}
218+
219+
/**
220+
* @param array $items
221+
* @param string $path
222+
* @param $expected
223+
* @return void
224+
* @dataProvider hasPathProvider
225+
*/
226+
public function testHasPath(array $items, string $path, $expected)
227+
{
228+
$actual = has_path($items, $path);
229+
$this->assertEquals($expected, $actual);
230+
}
231+
232+
public function unsetPathProvider(): array
233+
{
234+
return [
235+
'case_1' => [
236+
[],
237+
'',
238+
[]
239+
],
240+
'case_2' => [
241+
[1, 2, 3],
242+
'3',
243+
[1, 2, 3]
244+
],
245+
'case_3' => [
246+
[1, 2, 3],
247+
'2',
248+
[1, 2]
249+
],
250+
'case_4' => [
251+
[1, 2, 3],
252+
'0',
253+
[1 => 2, 2 => 3]
254+
],
255+
'case_5' => [
256+
[[[[1, 2, 3]]]],
257+
'0.0.0.1',
258+
[[[[1, 2 => 3]]]],
259+
],
260+
'case_6' => [
261+
[['x' => [[1, 2 => 3]]], 9],
262+
'0.x',
263+
[[], 1 => 9],
264+
],
265+
];
266+
}
267+
268+
/**
269+
* @param array $items
270+
* @param string $path
271+
* @param $expected
272+
* @return void
273+
* @dataProvider unsetPathProvider
274+
*/
275+
public function testUnsetPath(array $items, string $path, $expected)
276+
{
277+
unset_path($items, $path);
278+
$this->assertEquals($expected, $items);
279+
}
129280
}

0 commit comments

Comments
 (0)