|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Ipl\IteratorPipeline\Traits; |
|
6
|
|
|
|
|
7
|
|
|
use Ipl 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
|
|
|
* Check if any element matches the condition. |
|
57
|
|
|
* |
|
58
|
|
|
* @param callable $matcher |
|
59
|
|
|
* @return bool |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function hasAny(callable $matcher): bool |
|
62
|
|
|
{ |
|
63
|
1 |
|
return i\iterable_has_any($this->iterable, $matcher); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Check if all elements match the condition. |
|
68
|
|
|
* |
|
69
|
|
|
* @param callable $matcher |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function hasAll(callable $matcher): bool |
|
73
|
|
|
{ |
|
74
|
1 |
|
return i\iterable_has_all($this->iterable, $matcher); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Check that no elements match the condition. |
|
79
|
|
|
* |
|
80
|
|
|
* @param callable $matcher |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function hasNone(callable $matcher): bool |
|
84
|
|
|
{ |
|
85
|
1 |
|
return i\iterable_has_none($this->iterable, $matcher); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get the minimal value according to a given comparator. |
|
91
|
|
|
* Returns null for an empty iterable. |
|
92
|
|
|
* |
|
93
|
|
|
* @param callable|null $compare |
|
94
|
|
|
* @return mixed |
|
95
|
|
|
*/ |
|
96
|
2 |
|
public function min(?callable $compare = null) |
|
97
|
|
|
{ |
|
98
|
2 |
|
return i\iterable_min($this->iterable, $compare); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get the maximal value according to a given comparator. |
|
103
|
|
|
* Returns null for an empty iterable. |
|
104
|
|
|
* |
|
105
|
|
|
* @param callable|null $compare |
|
106
|
|
|
* @return mixed |
|
107
|
|
|
*/ |
|
108
|
2 |
|
public function max(?callable $compare = null) |
|
109
|
|
|
{ |
|
110
|
2 |
|
return i\iterable_max($this->iterable, $compare); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|