Skip to content

Commit 9a4f302

Browse files
committed
added Arrays::every(), some() & map() [Closes #36]
1 parent a93abc7 commit 9a4f302

File tree

4 files changed

+245
-0
lines changed

4 files changed

+245
-0
lines changed

src/Utils/Arrays.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,48 @@ public static function pick(array & $arr, $key, $default = NULL)
250250
}
251251
}
252252

253+
254+
/**
255+
* Tests whether some element in the array passes the callback test.
256+
* @return bool
257+
*/
258+
public static function some(array $arr, callable $callback)
259+
{
260+
foreach ($arr as $k => $v) {
261+
if ($callback($v, $k, $arr)) {
262+
return TRUE;
263+
}
264+
}
265+
return FALSE;
266+
}
267+
268+
269+
/**
270+
* Tests whether all elements in the array pass the callback test.
271+
* @return bool
272+
*/
273+
public static function every(array $arr, callable $callback)
274+
{
275+
foreach ($arr as $k => $v) {
276+
if (!$callback($v, $k, $arr)) {
277+
return FALSE;
278+
}
279+
}
280+
return TRUE;
281+
}
282+
283+
284+
/**
285+
* Applies the callback to the elements of the array.
286+
* @return array
287+
*/
288+
public static function map(array $arr, callable $callback)
289+
{
290+
$res = [];
291+
foreach ($arr as $k => $v) {
292+
$res[$k] = $callback($v, $k, $arr);
293+
}
294+
return $res;
295+
}
296+
253297
}

tests/Utils/Arrays.every().phpt

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Utils\Arrays::every()
5+
*/
6+
7+
use Nette\Utils\Arrays;
8+
use Tester\Assert;
9+
10+
11+
require __DIR__ . '/../bootstrap.php';
12+
13+
14+
test(function () {
15+
$arr = [];
16+
$log = [];
17+
$res = Arrays::every(
18+
$arr,
19+
function ($v, $k, $arr) use (& $log) { $log[] = func_get_args(); return FALSE; }
20+
);
21+
Assert::true($res);
22+
Assert::same([], $log);
23+
});
24+
25+
test(function () {
26+
$arr = [];
27+
$log = [];
28+
$res = Arrays::every(
29+
$arr,
30+
function ($v, $k, $arr) use (& $log) { $log[] = func_get_args(); return TRUE; }
31+
);
32+
Assert::true($res);
33+
Assert::same([], $log);
34+
});
35+
36+
test(function () {
37+
$arr = ['a', 'b'];
38+
$log = [];
39+
$res = Arrays::every(
40+
$arr,
41+
function ($v, $k, $arr) use (& $log) { $log[] = func_get_args(); return FALSE; }
42+
);
43+
Assert::false($res);
44+
Assert::same([['a', 0, $arr]], $log);
45+
});
46+
47+
test(function () {
48+
$arr = ['a', 'b'];
49+
$log = [];
50+
$res = Arrays::every(
51+
$arr,
52+
function ($v, $k, $arr) use (& $log) { $log[] = func_get_args(); return TRUE; }
53+
);
54+
Assert::true($res);
55+
Assert::same([['a', 0, $arr], ['b', 1, $arr]], $log);
56+
});
57+
58+
test(function () {
59+
$arr = ['a', 'b'];
60+
$log = [];
61+
$res = Arrays::every(
62+
$arr,
63+
function ($v, $k, $arr) use (& $log) { $log[] = func_get_args(); return $v === 'a'; }
64+
);
65+
Assert::false($res);
66+
Assert::same([['a', 0, $arr], ['b', 1, $arr]], $log);
67+
});
68+
69+
test(function () {
70+
$arr = ['x' => 'a', 'y' => 'b'];
71+
$log = [];
72+
$res = Arrays::every(
73+
$arr,
74+
function ($v, $k, $arr) use (& $log) { $log[] = func_get_args(); return TRUE; }
75+
);
76+
Assert::true($res);
77+
Assert::same([['a', 'x', $arr], ['b', 'y', $arr]], $log);
78+
});

tests/Utils/Arrays.map().phpt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Utils\Arrays::map()
5+
*/
6+
7+
use Nette\Utils\Arrays;
8+
use Tester\Assert;
9+
10+
11+
require __DIR__ . '/../bootstrap.php';
12+
13+
14+
test(function () {
15+
$arr = [];
16+
$log = [];
17+
$res = Arrays::map(
18+
$arr,
19+
function ($v, $k, $arr) use (& $log) { $log[] = func_get_args(); return TRUE; }
20+
);
21+
Assert::same([], $res);
22+
Assert::same([], $log);
23+
});
24+
25+
test(function () {
26+
$arr = ['a', 'b'];
27+
$log = [];
28+
$res = Arrays::map(
29+
$arr,
30+
function ($v, $k, $arr) use (& $log) { $log[] = func_get_args(); return $v . $v; }
31+
);
32+
Assert::same(['aa', 'bb'], $res);
33+
Assert::same([['a', 0, $arr], ['b', 1, $arr]], $log);
34+
});
35+
36+
test(function () {
37+
$arr = ['x' => 'a', 'y' => 'b'];
38+
$log = [];
39+
$res = Arrays::map(
40+
$arr,
41+
function ($v, $k, $arr) use (& $log) { $log[] = func_get_args(); return $v . $v; }
42+
);
43+
Assert::same(['x' => 'aa', 'y' => 'bb'], $res);
44+
Assert::same([['a', 'x', $arr], ['b', 'y', $arr]], $log);
45+
});

tests/Utils/Arrays.some().phpt

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Utils\Arrays::some()
5+
*/
6+
7+
use Nette\Utils\Arrays;
8+
use Tester\Assert;
9+
10+
11+
require __DIR__ . '/../bootstrap.php';
12+
13+
14+
test(function () {
15+
$arr = [];
16+
$log = [];
17+
$res = Arrays::some(
18+
$arr,
19+
function ($v, $k, $arr) use (& $log) { $log[] = func_get_args(); return FALSE; }
20+
);
21+
Assert::false($res);
22+
Assert::same([], $log);
23+
});
24+
25+
test(function () {
26+
$arr = [];
27+
$log = [];
28+
$res = Arrays::some(
29+
$arr,
30+
function ($v, $k, $arr) use (& $log) { $log[] = func_get_args(); return TRUE; }
31+
);
32+
Assert::false($res);
33+
Assert::same([], $log);
34+
});
35+
36+
test(function () {
37+
$arr = ['a', 'b'];
38+
$log = [];
39+
$res = Arrays::some(
40+
$arr,
41+
function ($v, $k, $arr) use (& $log) { $log[] = func_get_args(); return FALSE; }
42+
);
43+
Assert::false($res);
44+
Assert::same([['a', 0, $arr], ['b', 1, $arr]], $log);
45+
});
46+
47+
test(function () {
48+
$arr = ['a', 'b'];
49+
$log = [];
50+
$res = Arrays::some(
51+
$arr,
52+
function ($v, $k, $arr) use (& $log) { $log[] = func_get_args(); return TRUE; }
53+
);
54+
Assert::true($res);
55+
Assert::same([['a', 0, $arr]], $log);
56+
});
57+
58+
test(function () {
59+
$arr = ['a', 'b'];
60+
$log = [];
61+
$res = Arrays::some(
62+
$arr,
63+
function ($v, $k, $arr) use (& $log) { $log[] = func_get_args(); return $v === 'a'; }
64+
);
65+
Assert::true($res);
66+
Assert::same([['a', 0, $arr]], $log);
67+
});
68+
69+
test(function () {
70+
$arr = ['x' => 'a', 'y' => 'b'];
71+
$log = [];
72+
$res = Arrays::some(
73+
$arr,
74+
function ($v, $k, $arr) use (& $log) { $log[] = func_get_args(); return $v === 'a'; }
75+
);
76+
Assert::true($res);
77+
Assert::same([['a', 'x', $arr]], $log);
78+
});

0 commit comments

Comments
 (0)