1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Maxim Sokolovsky |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace WS\Utils\Collections\Functions; |
7
|
|
|
|
8
|
|
|
use Closure; |
9
|
|
|
use WS\Utils\Collections\HashSet; |
10
|
|
|
|
11
|
|
|
class Predicates |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Returns <Fn($el: mixed): bool> blocked all tries |
16
|
|
|
* @return Closure |
17
|
|
|
*/ |
18
|
1 |
|
public static function lock(): Closure |
19
|
|
|
{ |
20
|
|
|
return static function (): bool { |
21
|
1 |
|
return false; |
22
|
1 |
|
}; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Returns <Fn($el: mixed): bool> passed all not null elements |
27
|
|
|
* @return Closure |
28
|
|
|
*/ |
29
|
1 |
|
public static function notNull(): Closure |
30
|
|
|
{ |
31
|
|
|
return static function ($el): bool { |
32
|
1 |
|
return $el !== null; |
33
|
1 |
|
}; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns <Fn($el: mixed): bool> passed all tries |
38
|
|
|
* @return Closure |
39
|
|
|
*/ |
40
|
3 |
|
public static function notResistance(): Closure |
41
|
|
|
{ |
42
|
|
|
return static function (): bool { |
43
|
3 |
|
return true; |
44
|
3 |
|
}; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns <Fn($el: mixed): bool> passed each even element of call |
49
|
|
|
* @return Closure |
50
|
|
|
*/ |
51
|
2 |
|
public static function eachEven(): Closure |
52
|
|
|
{ |
53
|
2 |
|
$isEven = false; |
54
|
|
|
return static function () use (& $isEven) { |
55
|
2 |
|
$res = $isEven; |
56
|
2 |
|
$isEven = !$isEven; |
57
|
|
|
|
58
|
2 |
|
return $res; |
59
|
2 |
|
}; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns <Fn($el: mixed): bool> passed each nth element of call |
64
|
|
|
* @param $number |
65
|
|
|
* @return Closure |
66
|
|
|
*/ |
67
|
1 |
|
public static function nth($number): Closure |
68
|
|
|
{ |
69
|
1 |
|
$counter = 0; |
70
|
|
|
return static function () use ($number, & $counter) { |
71
|
1 |
|
$res = ++$counter % $number === 0; |
72
|
1 |
|
if ($res) { |
73
|
1 |
|
$counter = 0; |
74
|
|
|
} |
75
|
1 |
|
return $res; |
76
|
1 |
|
}; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns <Fn($el: mixed): bool> passed with equal value |
81
|
|
|
* @param $value |
82
|
|
|
* @return Closure |
83
|
|
|
*/ |
84
|
1 |
|
public static function equal($value): Closure |
85
|
|
|
{ |
86
|
|
|
return static function ($el) use ($value): bool { |
87
|
1 |
|
return $el === $value; |
88
|
1 |
|
}; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns <Fn($el: mixed): bool> passed unique element at once |
93
|
|
|
*/ |
94
|
1 |
|
public static function lockDuplicated(): Closure |
95
|
|
|
{ |
96
|
1 |
|
$set = new HashSet(); |
97
|
|
|
return static function ($el) use ($set): bool { |
98
|
1 |
|
$res = !$set->contains($el); |
99
|
1 |
|
$set->add($el); |
100
|
1 |
|
return $res; |
101
|
1 |
|
}; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns <Fn($el: mixed): bool> passed with less than value comparing |
106
|
|
|
* @param $value |
107
|
|
|
* @return Closure |
108
|
|
|
*/ |
109
|
2 |
|
public static function lessThan($value): Closure |
110
|
|
|
{ |
111
|
|
|
return static function ($el) use ($value): bool { |
112
|
1 |
|
return $el < $value; |
113
|
2 |
|
}; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns <Fn($el: mixed): bool> passed with less or equal value comparing |
118
|
|
|
* @param $value |
119
|
|
|
* @return Closure |
120
|
|
|
*/ |
121
|
5 |
|
public static function lessOrEqual($value): Closure |
122
|
|
|
{ |
123
|
|
|
return static function ($el) use ($value): bool { |
124
|
2 |
|
return $el <= $value; |
125
|
5 |
|
}; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns <Fn($el: mixed): bool> passed with more than value comparing |
130
|
|
|
* @param $value |
131
|
|
|
* @return Closure |
132
|
|
|
*/ |
133
|
1 |
|
public static function greaterThan($value): Closure |
134
|
|
|
{ |
135
|
|
|
return static function ($el) use ($value): bool { |
136
|
1 |
|
return $el > $value; |
137
|
1 |
|
}; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns <Fn($el: mixed): bool> passed with more or equal value comparing |
142
|
|
|
* @param $value |
143
|
|
|
* @return Closure |
144
|
|
|
*/ |
145
|
5 |
|
public static function greaterOrEqual($value): Closure |
146
|
|
|
{ |
147
|
|
|
return static function ($el) use ($value): bool { |
148
|
4 |
|
return $el >= $value; |
149
|
5 |
|
}; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Returns <Fn($el: mixed): bool> passed with not value comparing |
154
|
|
|
* @param $value |
155
|
|
|
* @return Closure |
156
|
|
|
*/ |
157
|
1 |
|
public static function not($value): Closure |
158
|
|
|
{ |
159
|
|
|
return static function ($el) use ($value): bool { |
160
|
1 |
|
return $el !== $value; |
161
|
1 |
|
}; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Returns <Fn($el: mixed): bool> passed with include of set value comparing |
166
|
|
|
* @param array $values |
167
|
|
|
* @return Closure |
168
|
|
|
*/ |
169
|
1 |
|
public static function in(array $values): Closure |
170
|
|
|
{ |
171
|
|
|
return static function ($el) use ($values): bool { |
172
|
1 |
|
return in_array($el, $values, true); |
173
|
1 |
|
}; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Returns <Fn($el: mixed): bool> passed with not include of set value comparing |
178
|
|
|
* @param array $values |
179
|
|
|
* @return Closure |
180
|
|
|
*/ |
181
|
1 |
|
public static function notIn(array $values): Closure |
182
|
|
|
{ |
183
|
|
|
return static function ($el) use ($values): bool { |
184
|
1 |
|
return !in_array($el, $values, true); |
185
|
1 |
|
}; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Returns <Fn($el: mixed): bool> passed with where object property value comparing |
190
|
|
|
* @param string $property |
191
|
|
|
* @param $value |
192
|
|
|
* @return Closure |
193
|
|
|
*/ |
194
|
1 |
|
public static function where(string $property, $value): Closure |
195
|
|
|
{ |
196
|
|
|
return static function ($ob) use ($property, $value) { |
197
|
1 |
|
return $value === ObjectFunctions::getPropertyValue($ob, $property); |
198
|
1 |
|
}; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Returns <Fn($el: mixed): bool> passed with where not object property value comparing |
203
|
|
|
* @param string $property |
204
|
|
|
* @param $value |
205
|
|
|
* @return Closure |
206
|
|
|
*/ |
207
|
1 |
|
public static function whereNot(string $property, $value): Closure |
208
|
|
|
{ |
209
|
|
|
return static function ($ob) use ($property, $value) { |
210
|
1 |
|
return $value !== ObjectFunctions::getPropertyValue($ob, $property); |
211
|
1 |
|
}; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Returns <Fn($el: mixed): bool> passed with where present in a set of object property value comparing |
216
|
|
|
* @param string $property |
217
|
|
|
* @param array $values |
218
|
|
|
* @return Closure |
219
|
|
|
*/ |
220
|
1 |
|
public static function whereIn(string $property, array $values): Closure |
221
|
|
|
{ |
222
|
|
|
return static function ($ob) use ($property, $values) { |
223
|
1 |
|
return in_array(ObjectFunctions::getPropertyValue($ob, $property), $values, true); |
224
|
1 |
|
}; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Returns <Fn($el: mixed): bool> passed with where not present in a set of object property value comparing |
229
|
|
|
* @param string $property |
230
|
|
|
* @param array $values |
231
|
|
|
* @return Closure |
232
|
|
|
*/ |
233
|
1 |
|
public static function whereNotIn(string $property, array $values): Closure |
234
|
|
|
{ |
235
|
|
|
return static function ($ob) use ($property, $values) { |
236
|
1 |
|
return !in_array(ObjectFunctions::getPropertyValue($ob, $property), $values, true); |
237
|
1 |
|
}; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Returns <Fn($el: mixed): bool> passed with more than object property value comparing |
242
|
|
|
* @param string $property |
243
|
|
|
* @param $value |
244
|
|
|
* @return Closure |
245
|
|
|
*/ |
246
|
1 |
|
public static function whereGreaterThan(string $property, $value): Closure |
247
|
|
|
{ |
248
|
|
|
return static function ($ob) use ($property, $value) { |
249
|
1 |
|
return ObjectFunctions::getPropertyValue($ob, $property) > $value; |
250
|
1 |
|
}; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Returns <Fn($el: mixed): bool> passed with less than object property value comparing |
255
|
|
|
* @param string $property |
256
|
|
|
* @param $value |
257
|
|
|
* @return Closure |
258
|
|
|
*/ |
259
|
1 |
|
public static function whereLessThan(string $property, $value): Closure |
260
|
|
|
{ |
261
|
|
|
return static function ($ob) use ($property, $value) { |
262
|
1 |
|
return ObjectFunctions::getPropertyValue($ob, $property) < $value; |
263
|
1 |
|
}; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Returns <Fn($el: mixed): bool> passed with more or equal object property value comparing |
268
|
|
|
* @param string $property |
269
|
|
|
* @param $value |
270
|
|
|
* @return Closure |
271
|
|
|
*/ |
272
|
1 |
|
public static function whereGreaterOrEqual(string $property, $value): Closure |
273
|
|
|
{ |
274
|
|
|
return static function ($ob) use ($property, $value) { |
275
|
1 |
|
return ObjectFunctions::getPropertyValue($ob, $property) >= $value; |
276
|
1 |
|
}; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Returns <Fn($el: mixed): bool> passed with less or equal object property value comparing |
281
|
|
|
* @param string $property |
282
|
|
|
* @param $value |
283
|
|
|
* @return Closure |
284
|
|
|
*/ |
285
|
1 |
|
public static function whereLessOrEqual(string $property, $value): Closure |
286
|
|
|
{ |
287
|
|
|
return static function ($ob) use ($property, $value) { |
288
|
1 |
|
return ObjectFunctions::getPropertyValue($ob, $property) <= $value; |
289
|
1 |
|
}; |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|