Skip to content

Commit a864e56

Browse files
test reject
1 parent 466b4a3 commit a864e56

File tree

4 files changed

+72
-12
lines changed

4 files changed

+72
-12
lines changed

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
"src/None.php",
4545
"src/Pipe.php",
4646
"src/Pull.php",
47+
"src/Pull.php",
48+
"src/Reject.php",
4749
"src/Remove.php",
4850
"src/RemoveKey.php",
4951
"src/Set.php",

src/Reject.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 <b>do not match</b> 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 reject(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 reject(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 has(array $items, ...$keys): bool
197
{
208
return ! array_diff($keys, array_keys($items));

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

0 commit comments

Comments
 (0)