Skip to content

Commit 4dbf6b8

Browse files
test get_path
1 parent ac95410 commit 4dbf6b8

File tree

2 files changed

+65
-48
lines changed

2 files changed

+65
-48
lines changed

src/DotNotation.php

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,6 @@ function set_path(array &$haystack, string $path, $value, string $separator = '.
2424
}
2525

2626
$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;
4827
}
4928

5029
/**
@@ -59,35 +38,10 @@ function get_path(array $haystack, string $path, $default = null, string $separa
5938
return
6039
array_reduce(
6140
explode($separator, $path),
62-
fn ($arr, $p) => is_array($arr) && array_key_exists($p, $arr) ? $arr[$p] : $default,
41+
fn ($arr, $key) =>
42+
$default!== $arr && is_array($arr) && array_key_exists($key, $arr) ? $arr[$key] : $default,
6343
$haystack
6444
);
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;
9145
}
9246

9347
/**

tests/DotNotationTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace BrenoRoosevelt\Tests;
55

66
use PHPUnit\Framework\TestCase;
7+
use function BrenoRoosevelt\get_path;
78
use function BrenoRoosevelt\set_path;
89

910
class DotNotationTest extends TestCase
@@ -63,4 +64,66 @@ public function testSetPath(array $haystack, string $path, $value, string $separ
6364
set_path($haystack, $path, $value, $separator);
6465
$this->assertEquals($expected, $haystack);
6566
}
67+
68+
public function getPathProvider(): array
69+
{
70+
return [
71+
'case_1' => [
72+
[],
73+
'',
74+
1,
75+
1
76+
],
77+
'case_2' => [
78+
[4],
79+
'0',
80+
null,
81+
4
82+
],
83+
'case_3' => [
84+
[[[[4, 5]]]],
85+
'0.0.0.1',
86+
null,
87+
5
88+
],
89+
'case_4' => [
90+
[['a' => ['b' => ['c']]]],
91+
'0.a.b',
92+
null,
93+
['c']
94+
],
95+
'case_5' => [
96+
[['a' => ['b' => ['c']]]],
97+
'0.a.b.c',
98+
null,
99+
null
100+
],
101+
'case_6' => [
102+
[['a' => ['b' => ['c', 'd' => 9, 3]]], 9, 8, 7],
103+
'0.a.b.d',
104+
null,
105+
9
106+
],
107+
'case_7' => [
108+
['a' => ['b' => []]],
109+
'a.b.c.d',
110+
['c' => ['d' => 7]],
111+
['c' => ['d' => 7]]
112+
]
113+
];
114+
}
115+
116+
/**
117+
* @param array $items
118+
* @param string $path
119+
* @param $default
120+
* @param $expected
121+
* @return void
122+
* @dataProvider getPathProvider
123+
*/
124+
public function testGetPath(array $items, string $path, $default, $expected): void
125+
{
126+
$actual = get_path($items, $path, $default);
127+
$this->assertEquals($expected, $actual);
128+
}
66129
}

0 commit comments

Comments
 (0)