Skip to content

Commit a5f6393

Browse files
test all
1 parent 4b30152 commit a5f6393

File tree

4 files changed

+124
-18
lines changed

4 files changed

+124
-18
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"src/CallbackArgs.php",
3131
"src/IndexOf.php",
3232
"src/Contains.php",
33+
"src/All.php",
3334
"src/Add.php",
3435
"src/Set.php",
3536
"src/Pipe.php",

src/All.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace BrenoRoosevelt;
5+
6+
/**
7+
* Evaluates whether ALL elements match the specification
8+
*
9+
* @param iterable $items The collection
10+
* @param callable $callback Callable must return a boolean
11+
* @param bool $empty_is_valid When the collection is empty, indicates whether the function should return true/false
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 bool
28+
*/
29+
function all(iterable $items, callable $callback, bool $empty_is_valid = true, int $mode = CALLBACK_USE_VALUE): bool
30+
{
31+
$count = 0;
32+
foreach ($items as $key => $value) {
33+
$count++;
34+
if (true !== call_user_func_array($callback, __args($mode, $key, $value))) {
35+
return false;
36+
}
37+
}
38+
39+
return $empty_is_valid || $count > 0;
40+
}

src/array_functions.php

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

44
namespace BrenoRoosevelt;
55

6-
function reindex(array &$items): void
7-
{
8-
$items = array_values($items);
9-
}
10-
11-
function all(iterable $items, callable $callback, bool $empty_is_valid = true, int $mode = CALLBACK_USE_VALUE): bool
12-
{
13-
$count = 0;
14-
foreach ($items as $key => $value) {
15-
$count++;
16-
if (true !== call_user_func_array($callback, __args($mode, $key, $value))) {
17-
return false;
18-
}
19-
}
20-
21-
return $empty_is_valid || $count > 0;
22-
}
23-
246
function some(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): bool
257
{
268
return at_least(1, $items, $callback, $mode);

tests/AllTest.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace BrenoRoosevelt\Tests;
5+
6+
use function BrenoRoosevelt\all;
7+
use PHPUnit\Framework\TestCase;
8+
use const BrenoRoosevelt\CALLBACK_USE_BOTH;
9+
use const BrenoRoosevelt\CALLBACK_USE_KEY;
10+
use const BrenoRoosevelt\CALLBACK_USE_VALUE;
11+
12+
class AllTest extends TestCase
13+
{
14+
public function allProvider(): array
15+
{
16+
return [
17+
'case_1' => [
18+
['a', 'b', 'c'], // collection
19+
fn($el) => false, // callback
20+
true, // empty_is_valid
21+
CALLBACK_USE_VALUE, // mode
22+
false, // expected
23+
],
24+
'case_2' => [
25+
['a', 'b', 'c'],
26+
fn($el) => true,
27+
true,
28+
CALLBACK_USE_VALUE,
29+
true,
30+
],
31+
'case_3' => [
32+
[],
33+
fn($el) => false,
34+
true,
35+
CALLBACK_USE_VALUE,
36+
true,
37+
],
38+
'case_4' => [
39+
[],
40+
fn($el) => true,
41+
true,
42+
CALLBACK_USE_VALUE,
43+
true,
44+
],
45+
'case_5' => [
46+
[1, 2, 3],
47+
fn($el) => $el % 2 === 0,
48+
true,
49+
CALLBACK_USE_VALUE,
50+
false,
51+
],
52+
'case_6' => [
53+
['a' => 1, 'b' => 2, 3],
54+
fn($key) => is_integer($key),
55+
true,
56+
CALLBACK_USE_KEY,
57+
false,
58+
],
59+
'case_7' => [
60+
['a' => 'x', 'b' => 'y'],
61+
fn($el, $key) => is_string($key) && is_string($el),
62+
true,
63+
CALLBACK_USE_BOTH,
64+
true,
65+
]
66+
];
67+
}
68+
69+
/**
70+
* @param iterable $items
71+
* @param callable $callback
72+
* @param bool $emptyIsValid
73+
* @param int $mode
74+
* @param bool $expected
75+
* @return void
76+
* @dataProvider allProvider
77+
*/
78+
public function testAdd(iterable $items, callable $callback, bool $emptyIsValid, int $mode, bool $expected): void
79+
{
80+
$actual = all($items, $callback, $emptyIsValid, $mode);
81+
$this->assertEquals($expected, $actual);
82+
}
83+
}

0 commit comments

Comments
 (0)