1
|
|
|
<?php |
2
|
|
|
namespace TRex\Collection; |
3
|
|
|
|
4
|
|
|
trait CollectionFilterTrait |
5
|
|
|
{ |
6
|
|
|
/** |
7
|
|
|
* @param callable|null $filter |
8
|
|
|
* @param int $flag (only for php version >= 5.6) |
9
|
|
|
* @return $this |
|
|
|
|
10
|
|
|
*/ |
11
|
1 |
|
public function filter(callable $filter = null, $flag = 0) |
12
|
|
|
{ |
13
|
1 |
|
if (defined('HHVM_VERSION')) { |
14
|
|
|
if (version_compare(HHVM_VERSION, '3.8.1', '>=')) { |
15
|
|
|
return new $this(array_filter((array)$this, $filter, $flag)); |
16
|
|
|
} |
17
|
|
|
} else { |
18
|
1 |
|
if (version_compare(phpversion(), '5.6.0', '>=')) { |
19
|
|
|
return new $this(array_filter((array)$this, $filter, $flag)); |
20
|
|
|
} |
21
|
|
|
} |
22
|
1 |
|
return new $this(array_filter((array)$this, $filter)); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Executes the callback for every value. |
27
|
|
|
* Returns a collection with the result of each callback call. |
28
|
|
|
* If the callback returns nothing, the returned collection will contain same values as the original ones. |
29
|
|
|
* |
30
|
|
|
* @param callable $callback |
31
|
|
|
* @param array $args |
32
|
|
|
* @return $this |
|
|
|
|
33
|
|
|
*/ |
34
|
3 |
|
public function each(callable $callback, array $args = []) |
35
|
|
|
{ |
36
|
3 |
|
return new $this(array_map( |
37
|
3 |
|
function ($value) use ($callback, $args) { |
38
|
3 |
|
return call_user_func($callback, $value, $args) ?: $value; |
39
|
3 |
|
}, |
40
|
|
|
(array)$this |
41
|
3 |
|
)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Executes the callback for every value. |
46
|
|
|
* Returns false if the callback returns once a falsy value. |
47
|
|
|
* |
48
|
|
|
* @param callable $callback |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
2 |
|
public function assert(callable $callback) |
52
|
|
|
{ |
53
|
2 |
|
foreach ($this as $key => $value) { |
|
|
|
|
54
|
2 |
|
if (!call_user_func($callback, $value)) { |
55
|
1 |
|
return false; |
56
|
|
|
} |
57
|
1 |
|
} |
58
|
1 |
|
return true; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Extracts the sequence of elements. |
63
|
|
|
* Starts at index $startIndex and stop after $length keys. |
64
|
|
|
* |
65
|
|
|
* @param int $startIndex |
66
|
|
|
* @param int $length |
67
|
|
|
* @param bool $areKeysPreserved |
68
|
|
|
* @return mixed |
|
|
|
|
69
|
|
|
*/ |
70
|
3 |
|
public function extract($startIndex, $length = 0, $areKeysPreserved = true) |
71
|
|
|
{ |
72
|
3 |
|
$collection = (array)$this; |
73
|
3 |
|
$length = $length ?: count($collection); |
74
|
3 |
|
return new $this(array_slice($collection, $startIndex, $length, $areKeysPreserved)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param int $length |
79
|
|
|
* @return $this |
|
|
|
|
80
|
|
|
*/ |
81
|
1 |
|
public function rand($length = 1) |
82
|
|
|
{ |
83
|
1 |
|
$keys = (array)array_rand((array)$this, $length); |
84
|
1 |
|
$result = new $this; |
85
|
1 |
|
foreach ($this as $key => $value) { |
|
|
|
|
86
|
1 |
|
if (in_array($key, $keys)) { |
87
|
1 |
|
$result[$key] = $value; |
88
|
1 |
|
} |
89
|
1 |
|
} |
90
|
1 |
|
return $result; |
91
|
|
|
} |
92
|
|
|
} |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.