1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Zicht\Itertools\util; |
7
|
|
|
|
8
|
|
|
use function Zicht\Itertools\iterable; |
9
|
|
|
|
10
|
|
|
class Filters |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Returns a filter closure that only accepts values that are instances of $class. |
14
|
|
|
* |
15
|
|
|
* For example, the following will return a list where all items |
16
|
|
|
* are instances of class Bar: |
17
|
|
|
* > $list = iterable([new Foo(), new Bar(), new Moo()]); |
18
|
|
|
* > $result = $list->filter(Filters::type(Bar::class)); |
19
|
|
|
* > // {1: Bar} |
20
|
|
|
* |
21
|
|
|
* For example, the following will return a list where all items |
22
|
|
|
* have a property or array index 'key' that is an instance |
23
|
|
|
* of class Foo: |
24
|
|
|
* > $list = iterable([['key' => new Foo()], ['key' => new Bar()], ['key' => new Moo()]]); |
25
|
|
|
* > $result = $list->filter(Filters::type(Bar::class, 'key')); |
26
|
|
|
* > // {1: ['key' => Bar]} |
27
|
|
|
* |
28
|
|
|
* @param string $class |
29
|
|
|
* @param null|string|\Closure $strategy |
30
|
|
|
* @return \Closure |
31
|
|
|
*/ |
32
|
4 |
View Code Duplication |
public static function type($class, $strategy = null) |
|
|
|
|
33
|
|
|
{ |
34
|
4 |
|
$strategy = Conversions::mixedToValueGetter($strategy); |
35
|
|
|
return function ($value, $key = null) use ($class, $strategy) { |
36
|
3 |
|
return $strategy($value, $key) instanceof $class; |
37
|
4 |
|
}; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns a filter closure that only accepts values that are in $haystack. |
42
|
|
|
* |
43
|
|
|
* For example, the following will return a list where all items |
44
|
|
|
* are either 'b' or 'c': |
45
|
|
|
* > $list = iterable(['a', 'b', 'c', 'd', 'e']); |
46
|
|
|
* > $result = $list->filter(Filters::in(['b', 'c'])); |
47
|
|
|
* > // {1: 'b', 2: 'c'} |
48
|
|
|
* |
49
|
|
|
* For example, the following will return a list where all items |
50
|
|
|
* have a property or array index 'key' that is either 'b' or 'c': |
51
|
|
|
* > $list = iterable([['key' => 'a'], ['key' => 'b'], ['key' => 'c'], ['key' => 'd'], ['key' => 'e']]); |
52
|
|
|
* > $result = $list->filter(Filters::in(['b', 'c'], 'key')); |
53
|
|
|
* > // {1: ['key' => 'b'], 2: ['key' => 'c']} |
54
|
|
|
* |
55
|
|
|
* @param null|array|string|\Iterator $haystack |
56
|
|
|
* @param null|string|\Closure $strategy |
57
|
|
|
* @param boolean $strict |
58
|
|
|
* @return \Closure |
59
|
|
|
*/ |
60
|
14 |
View Code Duplication |
public static function in($haystack, $strategy = null, $strict = false) |
|
|
|
|
61
|
|
|
{ |
62
|
14 |
|
if (!is_bool($strict)) { |
63
|
5 |
|
throw new \InvalidArgumentException('$STRICT must be a boolean'); |
64
|
|
|
} |
65
|
9 |
|
if (!is_array($haystack)) { |
66
|
5 |
|
$haystack = iterable($haystack)->values(); |
|
|
|
|
67
|
|
|
} |
68
|
6 |
|
$strategy = Conversions::mixedToValueGetter($strategy); |
69
|
|
|
return function ($value, $key = null) use ($haystack, $strategy, $strict) { |
70
|
6 |
|
return in_array($strategy($value, $key), $haystack, $strict); |
71
|
6 |
|
}; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns a filter closure that only accepts values that are equal to $expected |
76
|
|
|
* |
77
|
|
|
* For example, the following will return a list where all items |
78
|
|
|
* equal 'bar': |
79
|
|
|
* > $list = iterable(['foo', 'bar']); |
80
|
|
|
* > $result = $list->filter(Filters::equals('bar')); |
81
|
|
|
* > // {1: 'bar'} |
82
|
|
|
* |
83
|
|
|
* For example, the following will return a list where all items |
84
|
|
|
* have a property or array index 'foo' that equals 'bar': |
85
|
|
|
* > $list = iterable([['key' => 'foo'], ['key' => 'bar']]); |
86
|
|
|
* > $result = $list->filter(Filters::equals('bar', 'key')); |
87
|
|
|
* > // {1: ['key' => 'bar']} |
88
|
|
|
* |
89
|
|
|
* @param mixed $expected |
90
|
|
|
* @param null|string|\Closure $strategy |
91
|
|
|
* @param boolean $strict |
92
|
|
|
* @return \Closure |
93
|
|
|
*/ |
94
|
11 |
|
public static function equals($expected, $strategy = null, $strict = false) |
95
|
|
|
{ |
96
|
11 |
|
if (!is_bool($strict)) { |
97
|
5 |
|
throw new \InvalidArgumentException('$strict must be a boolean'); |
98
|
|
|
} |
99
|
6 |
|
$strategy = Conversions::mixedToValueGetter($strategy); |
100
|
6 |
|
if ($strict) { |
101
|
|
|
return function ($value, $key = null) use ($expected, $strategy) { |
102
|
1 |
|
return $expected === $strategy($value, $key); |
103
|
1 |
|
}; |
104
|
|
|
} else { |
105
|
|
|
return function ($value, $key = null) use ($expected, $strategy) { |
106
|
6 |
|
return $expected == $strategy($value, $key); |
107
|
6 |
|
}; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns a filter closure that only accepts values that are after $expected |
113
|
|
|
* |
114
|
|
|
* For example, the following will return a list where only |
115
|
|
|
* returns entries that are after 2020-04-01: |
116
|
|
|
* > $list = iterable([new \DateTime('2020-01-01'), new \DateTime('2020-03-01'), new \DateTime('2020-05-01')]) |
117
|
|
|
* > $result = $list->filer(Filters::after(new \DateTimeImmutable('2020-04-01'))); |
118
|
|
|
* > // [new \DateTime('2020-05-01')] |
119
|
|
|
* |
120
|
|
|
* @param \DateTimeInterface|int|float $expected |
121
|
|
|
* @param null|string|\Closure $strategy |
122
|
|
|
* @param bool $orEqual |
123
|
|
|
* @return \Closure |
124
|
|
|
*/ |
125
|
26 |
|
public static function after($expected, $strategy = null, $orEqual = false) |
126
|
|
|
{ |
127
|
26 |
|
$strategy = Conversions::mixedToValueGetter($strategy); |
128
|
|
|
|
129
|
|
|
// Support DateTimeInterface |
130
|
26 |
View Code Duplication |
if ($expected instanceof \DateTimeInterface) { |
|
|
|
|
131
|
|
|
return function ($value, $key = null) use ($expected, $strategy, $orEqual) { |
132
|
14 |
|
$value = $strategy($value, $key); |
133
|
|
|
// Try to convert strings that look like ISO date format |
134
|
14 |
|
if (is_string($value) && preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}/', $value)) { |
135
|
|
|
try { |
136
|
4 |
|
$value = new \DateTimeImmutable($value); |
137
|
2 |
|
} catch (\Exception $exception) { |
|
|
|
|
138
|
|
|
} |
139
|
|
|
} |
140
|
14 |
|
return $value instanceof \DateTimeInterface && ($orEqual ? $expected <= $value : $expected < $value); |
141
|
14 |
|
}; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// Support numbers |
145
|
12 |
View Code Duplication |
if (is_int($expected) || is_float($expected)) { |
|
|
|
|
146
|
|
|
return function ($value, $key = null) use ($expected, $strategy, $orEqual) { |
147
|
11 |
|
$value = $strategy($value, $key); |
148
|
11 |
|
return (is_int($value) || is_float($value)) && ($orEqual ? $expected <= $value : $expected < $value); |
149
|
11 |
|
}; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// Everything else fails |
153
|
|
|
return function () { |
154
|
1 |
|
return false; |
155
|
1 |
|
}; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Returns a filter closure that only accepts values that are before $expected |
160
|
|
|
* |
161
|
|
|
* For example, the following will return a list where only |
162
|
|
|
* returns entries that are before 2020-04-01: |
163
|
|
|
* > $list = iterable([new \DateTime('2020-01-01'), new \DateTime('2020-03-01'), new \DateTime('2020-05-01')]) |
164
|
|
|
* > $result = $list->filer(Filters::before(new \DateTimeImmutable('2020-04-01'))); |
165
|
|
|
* > // [new \DateTime('2020-01-01'), new \DateTime('2020-03-01')] |
166
|
|
|
* |
167
|
|
|
* @param \DateTimeInterface|int|float $expected |
168
|
|
|
* @param null|string|\Closure $strategy |
169
|
|
|
* @param bool $orEqual |
170
|
|
|
* @return \Closure |
171
|
|
|
*/ |
172
|
26 |
|
public static function before($expected, $strategy = null, $orEqual = false) |
173
|
|
|
{ |
174
|
26 |
|
$strategy = Conversions::mixedToValueGetter($strategy); |
175
|
|
|
|
176
|
|
|
// Support DateTimeInterface |
177
|
26 |
View Code Duplication |
if ($expected instanceof \DateTimeInterface) { |
|
|
|
|
178
|
|
|
return function ($value, $key = null) use ($expected, $strategy, $orEqual) { |
179
|
14 |
|
$value = $strategy($value, $key); |
180
|
|
|
// Try to convert strings that look like ISO date format |
181
|
14 |
|
if (is_string($value) && preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}/', $value)) { |
182
|
|
|
try { |
183
|
4 |
|
$value = new \DateTimeImmutable($value); |
184
|
2 |
|
} catch (\Exception $exception) { |
|
|
|
|
185
|
|
|
} |
186
|
|
|
} |
187
|
14 |
|
return $value instanceof \DateTimeInterface && ($orEqual ? $expected >= $value : $expected > $value); |
188
|
14 |
|
}; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
// Support numbers |
192
|
12 |
View Code Duplication |
if (is_int($expected) || is_float($expected)) { |
|
|
|
|
193
|
|
|
return function ($value, $key = null) use ($expected, $strategy, $orEqual) { |
194
|
11 |
|
$value = $strategy($value, $key); |
195
|
11 |
|
return (is_int($value) || is_float($value)) && ($orEqual ? $expected >= $value : $expected > $value); |
196
|
11 |
|
}; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
// Everything else fails |
200
|
|
|
return function () { |
201
|
1 |
|
return false; |
202
|
1 |
|
}; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Returns a filter closure that inverts the value |
207
|
|
|
* |
208
|
|
|
* For example, the following will return a list where none |
209
|
|
|
* of the items equal 'bar' |
210
|
|
|
* > $list = iterable(['foo', 'bar']); |
211
|
|
|
* > $result = $list->filter(Filters::not(equals('bar'))); |
212
|
|
|
* > // {0: 'foo'} |
213
|
|
|
* |
214
|
|
|
* @param null|string|\Closure $strategy |
215
|
|
|
* @return \Closure |
216
|
|
|
*/ |
217
|
2 |
View Code Duplication |
public static function not($strategy = null) |
|
|
|
|
218
|
|
|
{ |
219
|
2 |
|
$strategy = Conversions::mixedToValueGetter($strategy); |
220
|
|
|
return function ($value, $key = null) use ($strategy) { |
221
|
2 |
|
return !($strategy($value, $key)); |
222
|
2 |
|
}; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Returns a filter closure that only accepts values that match the given regular expression. |
227
|
|
|
* |
228
|
|
|
* For example, the following will return a list where all items |
229
|
|
|
* match 'bar': |
230
|
|
|
* > $list = iterable(['-= foo =-', '-= bar =-']); |
231
|
|
|
* > $result = $list->filter(Filters::match('/bar/i')); |
232
|
|
|
* > // {1: '-= bar =-'} |
233
|
|
|
* |
234
|
|
|
* @param string $pattern |
235
|
|
|
* @param null|string|\Closure $strategy |
236
|
|
|
* @return \Closure |
237
|
|
|
*/ |
238
|
7 |
|
public static function match($pattern, $strategy = null) |
239
|
|
|
{ |
240
|
7 |
|
if (!is_string($pattern)) { |
241
|
4 |
|
throw new \InvalidArgumentException('$PATTERN must be a string'); |
242
|
|
|
} |
243
|
|
|
|
244
|
3 |
|
$strategy = Conversions::mixedToValueGetter($strategy); |
245
|
3 |
|
return function ($value, $key = null) use ($pattern, $strategy) { |
246
|
3 |
|
return (bool)preg_match($pattern, $strategy($value, $key)); |
247
|
3 |
|
}; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.