1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Improved\IteratorPipeline\Traits; |
6
|
|
|
|
7
|
|
|
use Improved as i; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Pipeline methods for finding an element in an iterable. |
11
|
|
|
*/ |
12
|
|
|
trait FindingTrait |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var iterable |
16
|
|
|
*/ |
17
|
|
|
protected $iterable; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Get the first value of an iterable. |
22
|
|
|
* |
23
|
|
|
* @param bool $required Throw RangeException instead of returning null for empty iterable |
24
|
|
|
* @return mixed |
25
|
|
|
*/ |
26
|
2 |
|
public function first(bool $required = false) |
27
|
|
|
{ |
28
|
2 |
|
return i\iterable_first($this->iterable, $required); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get the last value of an iterable. |
33
|
|
|
* |
34
|
|
|
* @param bool $required Throw RangeException instead of returning null for empty iterable |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
2 |
|
public function last(bool $required = false) |
38
|
|
|
{ |
39
|
2 |
|
return i\iterable_last($this->iterable, $required); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the first value that matches a condition. |
45
|
|
|
* Returns null if no element is found. |
46
|
|
|
* |
47
|
|
|
* @param callable $matcher |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
1 |
|
public function find(callable $matcher) |
51
|
|
|
{ |
52
|
1 |
|
return i\iterable_find($this->iterable, $matcher); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the first value that matches a condition and return the key. |
57
|
|
|
* Returns null if no element is found. |
58
|
|
|
* |
59
|
|
|
* @param callable $matcher |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
1 |
|
public function findKey(callable $matcher) |
63
|
|
|
{ |
64
|
1 |
|
return i\iterable_find_key($this->iterable, $matcher); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Check if any element matches the condition. |
70
|
|
|
* |
71
|
|
|
* @param callable $matcher |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
1 |
|
public function hasAny(callable $matcher): bool |
75
|
|
|
{ |
76
|
1 |
|
return i\iterable_has_any($this->iterable, $matcher); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Check if all elements match the condition. |
81
|
|
|
* |
82
|
|
|
* @param callable $matcher |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
1 |
|
public function hasAll(callable $matcher): bool |
86
|
|
|
{ |
87
|
1 |
|
return i\iterable_has_all($this->iterable, $matcher); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Check that no elements match the condition. |
92
|
|
|
* |
93
|
|
|
* @param callable $matcher |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
1 |
|
public function hasNone(callable $matcher): bool |
97
|
|
|
{ |
98
|
1 |
|
return i\iterable_has_none($this->iterable, $matcher); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the minimal value according to a given comparator. |
104
|
|
|
* Returns null for an empty iterable. |
105
|
|
|
* |
106
|
|
|
* @param callable|null $compare |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
2 |
|
public function min(?callable $compare = null) |
110
|
|
|
{ |
111
|
2 |
|
return i\iterable_min($this->iterable, $compare); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get the maximal value according to a given comparator. |
116
|
|
|
* Returns null for an empty iterable. |
117
|
|
|
* |
118
|
|
|
* @param callable|null $compare |
119
|
|
|
* @return mixed |
120
|
|
|
*/ |
121
|
2 |
|
public function max(?callable $compare = null) |
122
|
|
|
{ |
123
|
2 |
|
return i\iterable_max($this->iterable, $compare); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|