forked from maciejczyzewski/bottomline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
every.php
43 lines (41 loc) · 949 Bytes
/
every.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
namespace collections;
/**
* Checks if predicate returns truthy for all elements of collection.
*
* Iteration is stopped once predicate returns falsey.
*
* **Usage**
*
* ```php
* __::every([1, 3, 4], function ($value, $key, $collection) {
* return is_int($v);
* });
* ```
*
* **Result**
*
* ```
* true
* ```
*
* @param array|object $collection The collection to iterate over.
* @param \Closure $iteratee The function to call for each value.
*
* @return bool
*/
function every($collection, \Closure $iteratee)
{
$truthy = true;
// We could use __::reduce(), but it won't allow us to return preliminarily.
\__::doForEach(
$collection,
function ($value, $key, $collection) use (&$truthy, $iteratee) {
$truthy = $truthy && $iteratee($value, $key, $collection);
if (!$truthy) {
return false;
}
}
);
return $truthy;
}