Predicates::greaterThan()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 2
    public static function lockDuplicated(?callable $caster = null): Closure
95
    {
96 2
        $set = new HashSet();
97
        return static function ($el) use ($set, $caster): bool {
98 2
            $el = isset($caster) ? $caster($el) : $el;
99 2
            $res = !$set->contains($el);
100 2
            $set->add($el);
101 2
            return $res;
102 2
        };
103
    }
104
105
    /**
106
     * Returns <Fn($el: mixed): bool> passed with less than value comparing
107
     * @param $value
108
     * @return Closure
109
     */
110 2
    public static function lessThan($value): Closure
111
    {
112
        return static function ($el) use ($value): bool {
113 1
            return $el < $value;
114 2
        };
115
    }
116
117
    /**
118
     * Returns <Fn($el: mixed): bool> passed with less or equal value comparing
119
     * @param $value
120
     * @return Closure
121
     */
122 5
    public static function lessOrEqual($value): Closure
123
    {
124
        return static function ($el) use ($value): bool {
125 2
            return $el <= $value;
126 5
        };
127
    }
128
129
    /**
130
     * Returns <Fn($el: mixed): bool> passed with more than value comparing
131
     * @param $value
132
     * @return Closure
133
     */
134 1
    public static function greaterThan($value): Closure
135
    {
136
        return static function ($el) use ($value): bool {
137 1
            return $el > $value;
138 1
        };
139
    }
140
141
    /**
142
     * Returns <Fn($el: mixed): bool> passed with more or equal value comparing
143
     * @param $value
144
     * @return Closure
145
     */
146 5
    public static function greaterOrEqual($value): Closure
147
    {
148
        return static function ($el) use ($value): bool {
149 4
            return $el >= $value;
150 5
        };
151
    }
152
153
    /**
154
     * Returns <Fn($el: mixed): bool> passed with not value comparing
155
     * @param $value
156
     * @return Closure
157
     */
158 1
    public static function not($value): Closure
159
    {
160
        return static function ($el) use ($value): bool {
161 1
            return $el !== $value;
162 1
        };
163
    }
164
165
    /**
166
     * Returns <Fn($el: mixed): bool> passed with include of set value comparing
167
     * @param array $values
168
     * @return Closure
169
     */
170 1
    public static function in(array $values): Closure
171
    {
172
        return static function ($el) use ($values): bool {
173 1
            return in_array($el, $values, true);
174 1
        };
175
    }
176
177
    /**
178
     * Returns <Fn($el: mixed): bool> passed with not include of set value comparing
179
     * @param array $values
180
     * @return Closure
181
     */
182 1
    public static function notIn(array $values): Closure
183
    {
184
        return static function ($el) use ($values): bool {
185 1
            return !in_array($el, $values, true);
186 1
        };
187
    }
188
189
    /**
190
     * Returns <Fn($el: mixed): bool> passed with where object property value comparing
191
     * @param string $property
192
     * @param $value
193
     * @return Closure
194
     */
195 1
    public static function where(string $property, $value): Closure
196
    {
197
        return static function ($ob) use ($property, $value) {
198 1
            return $value === ObjectFunctions::getPropertyValue($ob, $property);
199 1
        };
200
    }
201
202
    /**
203
     * Returns <Fn($el: mixed): bool> passed with where not object property value comparing
204
     * @param string $property
205
     * @param $value
206
     * @return Closure
207
     */
208 1
    public static function whereNot(string $property, $value): Closure
209
    {
210
        return static function ($ob) use ($property, $value) {
211 1
            return $value !== ObjectFunctions::getPropertyValue($ob, $property);
212 1
        };
213
    }
214
215
    /**
216
     * Returns <Fn($el: mixed): bool> passed with where present in a set of object property value comparing
217
     * @param string $property
218
     * @param array $values
219
     * @return Closure
220
     */
221 1
    public static function whereIn(string $property, array $values): Closure
222
    {
223
        return static function ($ob) use ($property, $values) {
224 1
            return in_array(ObjectFunctions::getPropertyValue($ob, $property), $values, true);
225 1
        };
226
    }
227
228
    /**
229
     * Returns <Fn($el: mixed): bool> passed with where not present in a set of object property value comparing
230
     * @param string $property
231
     * @param array $values
232
     * @return Closure
233
     */
234 1
    public static function whereNotIn(string $property, array $values): Closure
235
    {
236
        return static function ($ob) use ($property, $values) {
237 1
            return !in_array(ObjectFunctions::getPropertyValue($ob, $property), $values, true);
238 1
        };
239
    }
240
241
    /**
242
     * Returns <Fn($el: mixed): bool> passed with more than object property value comparing
243
     * @param string $property
244
     * @param $value
245
     * @return Closure
246
     */
247 1
    public static function whereGreaterThan(string $property, $value): Closure
248
    {
249
        return static function ($ob) use ($property, $value) {
250 1
            return ObjectFunctions::getPropertyValue($ob, $property) > $value;
251 1
        };
252
    }
253
254
    /**
255
     * Returns <Fn($el: mixed): bool> passed with less than object property value comparing
256
     * @param string $property
257
     * @param $value
258
     * @return Closure
259
     */
260 1
    public static function whereLessThan(string $property, $value): Closure
261
    {
262
        return static function ($ob) use ($property, $value) {
263 1
            return ObjectFunctions::getPropertyValue($ob, $property) < $value;
264 1
        };
265
    }
266
267
    /**
268
     * Returns <Fn($el: mixed): bool> passed with more or equal object property value comparing
269
     * @param string $property
270
     * @param $value
271
     * @return Closure
272
     */
273 1
    public static function whereGreaterOrEqual(string $property, $value): Closure
274
    {
275
        return static function ($ob) use ($property, $value) {
276 1
            return ObjectFunctions::getPropertyValue($ob, $property) >= $value;
277 1
        };
278
    }
279
280
    /**
281
     * Returns <Fn($el: mixed): bool> passed with less or equal object property value comparing
282
     * @param string $property
283
     * @param $value
284
     * @return Closure
285
     */
286 1
    public static function whereLessOrEqual(string $property, $value): Closure
287
    {
288
        return static function ($ob) use ($property, $value) {
289 1
            return ObjectFunctions::getPropertyValue($ob, $property) <= $value;
290 1
        };
291
    }
292
293
    /**
294
     * Returns <Fn($el: mixed): bool> passed only one time with first element
295
     * @return Closure
296
     */
297 1
    public static function first(): Closure
298
    {
299 1
        $runOut = false;
300
        return static function () use (& $runOut) {
301 1
            if ($runOut) {
302 1
                return false;
303
            }
304 1
            $runOut = true;
305 1
            return true;
306 1
        };
307
    }
308
}
309