Skip to content

Commit 115746e

Browse files
test flatten, callback args
1 parent bdbc0e2 commit 115746e

File tree

6 files changed

+154
-26
lines changed

6 files changed

+154
-26
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@
2727
"BrenoRoosevelt\\": "src/"
2828
},
2929
"files": [
30-
"src/Constants.php",
30+
"src/CallbackArgs.php",
3131
"src/IndexOf.php",
3232
"src/Contains.php",
3333
"src/Add.php",
3434
"src/Set.php",
3535
"src/Pipe.php",
36+
"src/Flatten.php",
3637
"src/DotNotation.php",
3738
"src/Remove.php",
3839
"src/array_functions.php"
File renamed without changes.

src/DotNotation.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,6 @@
33

44
namespace BrenoRoosevelt;
55

6-
/**
7-
* @param array $items
8-
* @param string|null $separator
9-
* @return array
10-
*/
11-
function flatten(array $items, ?string $separator = null)
12-
{
13-
$result = [];
14-
$iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($items));
15-
foreach ($iterator as $leafValue) {
16-
$keys = [];
17-
foreach (range(0, $iterator->getDepth()) as $depth) {
18-
$keys[] = $iterator->getSubIterator($depth)->key();
19-
}
20-
21-
if (! empty($separator)) {
22-
$result[join($separator, $keys) ] = $leafValue;
23-
} else {
24-
$result[] = $leafValue;
25-
}
26-
}
27-
28-
return $result;
29-
}
30-
316
/**
327
* @param array $items
338
* @param string $separator

src/Flatten.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace BrenoRoosevelt;
5+
6+
/**
7+
* @param array $items
8+
* @param string|null $separator
9+
* @return array
10+
*/
11+
function flatten(array $items, ?string $separator = null)
12+
{
13+
$result = [];
14+
$iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($items));
15+
foreach ($iterator as $leafValue) {
16+
$keys = [];
17+
foreach (range(0, $iterator->getDepth()) as $depth) {
18+
$keys[] = $iterator->getSubIterator($depth)->key();
19+
}
20+
21+
if (! empty($separator)) {
22+
$result[join($separator, $keys) ] = $leafValue;
23+
} else {
24+
$result[] = $leafValue;
25+
}
26+
}
27+
28+
return $result;
29+
}

tests/CallbackArgsTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace BrenoRoosevelt\Tests;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use function BrenoRoosevelt\__args;
8+
use const BrenoRoosevelt\CALLBACK_USE_BOTH;
9+
use const BrenoRoosevelt\CALLBACK_USE_KEY;
10+
use const BrenoRoosevelt\CALLBACK_USE_VALUE;
11+
12+
class CallbackArgsTest extends TestCase
13+
{
14+
public function testCallbackArgsUseValue()
15+
{
16+
$args = __args(CALLBACK_USE_VALUE, 1, 2);
17+
$this->assertEquals([2], $args);
18+
}
19+
20+
public function testCallbackArgsUseKey()
21+
{
22+
$args = __args(CALLBACK_USE_KEY, 1, 2);
23+
$this->assertEquals([1], $args);
24+
}
25+
26+
public function testCallbackArgsUseBoth()
27+
{
28+
$args = __args(CALLBACK_USE_BOTH, 1, 2);
29+
$this->assertEquals([2, 1], $args);
30+
}
31+
}

tests/FlattenTest.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace BrenoRoosevelt\Tests;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use function BrenoRoosevelt\flatten;
8+
9+
class FlattenTest extends TestCase
10+
{
11+
public function flattenProvider(): array
12+
{
13+
return [
14+
'case_1' => [
15+
['a', 'b', 'c'],
16+
null,
17+
['a', 'b', 'c'],
18+
],
19+
'case_2' => [
20+
['a', ['b', ['c'], 'd']],
21+
null,
22+
['a', 'b', 'c', 'd'],
23+
],
24+
'case_3' => [
25+
['x' => 'a', 'y' => 'b', 'z' => 'c'],
26+
null,
27+
['a', 'b', 'c']
28+
],
29+
'case_4' => [
30+
['x' => 'a', 'y' => ['b', ['z' => 'c'], 'd']],
31+
'.',
32+
['x' => 'a', 'y.0' => 'b', 'y.1.z' => 'c', 'y.2' => 'd']
33+
],
34+
'case_5' => [
35+
['x' => 'a', 'y' => ['b', ['z' => 'c'], 'd']],
36+
'\\',
37+
['x' => 'a', 'y\\0' => 'b', 'y\\1\\z' => 'c', 'y\\2' => 'd']
38+
],
39+
'case_6' => [
40+
[
41+
[
42+
'Post' => ['id' => '1', 'author_id' => '1', 'title' => '1st post'],
43+
'Author' => ['id' => '1', 'user' => 'spencer', 'password' => 'pwd'],
44+
],
45+
[
46+
'Post' => ['id' => '2', 'author_id' => '3', 'title' => '2nd Post', 'body' => '2nd post body'],
47+
'Author' => ['id' => '3', 'user' => 'mary', 'password' => null],
48+
],
49+
],
50+
'.',
51+
[
52+
'0.Post.id' => '1',
53+
'0.Post.author_id' => '1',
54+
'0.Post.title' => '1st post',
55+
'0.Author.id' => '1',
56+
'0.Author.user' => 'spencer',
57+
'0.Author.password' => 'pwd',
58+
'1.Post.id' => '2',
59+
'1.Post.author_id' => '3',
60+
'1.Post.title' => '2nd Post',
61+
'1.Post.body' => '2nd post body',
62+
'1.Author.id' => '3',
63+
'1.Author.user' => 'mary',
64+
'1.Author.password' => null,
65+
]
66+
],
67+
'case_7' => [
68+
[],
69+
null,
70+
[]
71+
],
72+
'case_8' => [
73+
[],
74+
'.',
75+
[]
76+
],
77+
];
78+
}
79+
80+
/**
81+
* @param array $items
82+
* @param string|null $separator
83+
* @param array $expected
84+
* @return void
85+
* @dataProvider flattenProvider
86+
*/
87+
public function testFlatten(array $items, ?string $separator, array $expected): void
88+
{
89+
$result = flatten($items, $separator);
90+
$this->assertEquals($expected, $result);
91+
}
92+
}

0 commit comments

Comments
 (0)