Skip to content

Commit fc02999

Browse files
test map
1 parent 13fbcb1 commit fc02999

File tree

4 files changed

+77
-20
lines changed

4 files changed

+77
-20
lines changed

composer.json

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,25 @@
2727
"BrenoRoosevelt\\": "src/"
2828
},
2929
"files": [
30-
"src/CallbackArgs.php",
31-
"src/IndexOf.php",
32-
"src/None.php",
33-
"src/Contains.php",
30+
"src/Add.php",
3431
"src/All.php",
3532
"src/AtLeast.php",
3633
"src/AtMost.php",
34+
"src/CallbackArgs.php",
35+
"src/Contains.php",
36+
"src/DotNotation.php",
3737
"src/Exactly.php",
38-
"src/Add.php",
39-
"src/Set.php",
40-
"src/Pipe.php",
38+
"src/Expand.php",
4139
"src/First.php",
4240
"src/Flatten.php",
43-
"src/Expand.php",
44-
"src/DotNotation.php",
41+
"src/IndexOf.php",
42+
"src/Map.php",
43+
"src/None.php",
44+
"src/Pipe.php",
45+
"src/Pull.php",
4546
"src/Remove.php",
4647
"src/RemoveKey.php",
47-
"src/Pull.php",
48+
"src/Set.php",
4849
"src/Some.php",
4950
"src/array_functions.php"
5051
]

src/Map.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace BrenoRoosevelt;
5+
6+
/**
7+
* Maps collection elements using a callback function
8+
*
9+
* @param iterable $items The collection
10+
* @param callable $callback The callback
11+
* @param int $mode [optional] <p>
12+
* Flag determining what arguments are sent to <i>callback</i>:
13+
* </p><ul>
14+
* <li>
15+
* <b>CALLBACK_USE_VALUE</b> - <b>default</b> pass value as the only argument
16+
* </li>
17+
* <li>
18+
* <b>CALLBACK_USE_KEY</b> - pass key as the only argument
19+
* to <i>callback</i> instead of the value</span>
20+
* </li>
21+
* <li>
22+
* <b>CALLBACK_USE_BOTH</b> - pass both value and key as
23+
* arguments to <i>callback</i></span>
24+
* </li>
25+
* </ul>
26+
* @return array Mapped values
27+
*/
28+
function map(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): array
29+
{
30+
$result = [];
31+
foreach ($items as $key => $value) {
32+
$result[$key] = call_user_func_array($callback, __args($mode, $key, $value));
33+
}
34+
35+
return $result;
36+
}

src/array_functions.php

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

44
namespace BrenoRoosevelt;
55

6-
function map(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): array
7-
{
8-
$result = [];
9-
foreach ($items as $key => $value) {
10-
$result[$key] = call_user_func_array($callback, __args($mode, $key, $value));
11-
}
12-
13-
return $result;
14-
}
15-
166
function accept(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): array
177
{
188
$result = [];

tests/MapTest.php

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

0 commit comments

Comments
 (0)