Skip to content

Commit 466b4a3

Browse files
test accept
1 parent fc02999 commit 466b4a3

File tree

4 files changed

+70
-12
lines changed

4 files changed

+70
-12
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"BrenoRoosevelt\\": "src/"
2828
},
2929
"files": [
30+
"src/Accept.php",
3031
"src/Add.php",
3132
"src/All.php",
3233
"src/AtLeast.php",

src/Accept.php

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

src/array_functions.php

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

44
namespace BrenoRoosevelt;
55

6-
function accept(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): array
7-
{
8-
$result = [];
9-
foreach ($items as $key => $value) {
10-
if (true === call_user_func_array($callback, __args($mode, $key, $value))) {
11-
$result[$key] = $value;
12-
}
13-
}
14-
15-
return $result;
16-
}
17-
186
function reject(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): array
197
{
208
$result = [];

tests/AcceptTest.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\accept;
8+
use const BrenoRoosevelt\CALLBACK_USE_BOTH;
9+
use const BrenoRoosevelt\CALLBACK_USE_KEY;
10+
11+
class AcceptTest extends TestCase
12+
{
13+
public function testAccept()
14+
{
15+
$elements = [ 1, 'b', 'c', 'd'];
16+
$this->assertEquals([ 1 => 'b', 2 => 'c', 3 => 'd'], accept($elements, 'is_string'));
17+
}
18+
19+
public function testAcceptKey()
20+
{
21+
$elements = [1 => 1, 'b' => 2, 'c'];
22+
$this->assertEquals(['b' => 2], accept($elements, 'is_string', CALLBACK_USE_KEY));
23+
}
24+
25+
public function testAcceptBoth()
26+
{
27+
$elements = [1, 2, 3];
28+
$this->assertEquals([1 => 2], accept($elements, fn($v, $k) => $v + $k === 3, CALLBACK_USE_BOTH));
29+
}
30+
}

0 commit comments

Comments
 (0)