filter.php ➔ contains()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 2
nop 1
dl 0
loc 18
ccs 0
cts 0
cp 0
crap 20
rs 9.2
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 35 and the first side effect is on line 18.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Nucleus - XMPP Library for PHP
4
 *
5
 * Copyright (C) 2016, Some rights reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Xmpp\Utils\filter;
17
18
require __DIR__ . '/filter/element.php';
19
require __DIR__ . '/filter/stanza.php';
20
21
/**
22
 * Predicate used to check if argument is equal (loosely) to specified value.
23
 *
24
 * ```php
25
 * $predicate = equals(10);
26
 *
27
 * $predicate(10); // true, as 10 == 10
28
 * $predicate("10abc"); // true, as 10 == "10abc"
29
 * $predicate("abc"); // false, as 10 != "abc"
30
 * ```
31
 *
32
 * @param $value
33
 * @return \Closure
34
 */
35
function equals($value) : \Closure
36
{
37
    return function ($argument) use ($value) {
38 7
        return $argument == $value;
39 11
    };
40
}
41
42
/**
43
 * Predicate used to check if argument is same as specified value. (strict comparision)
44
 *
45
 * ```php
46
 * $predicate = equals(10);
47
 *
48
 * $predicate(10); // true, as 10 === 10
49
 * $predicate("10abc"); // false, as 10 !== "10abc"
50
 * $predicate("abc"); // false, as 10 !== "abc"
51
 * ```
52
 *
53
 * @param $value
54
 * @return \Closure
55
 */
56
function same($value) : \Closure
57
{
58
    return function ($argument) use ($value) {
59 6
        return $argument === $value;
60 8
    };
61
}
62
63
/**
64
 * Predicate used to check if argument is an instance of specified class.
65
 *
66
 * ```php
67
 * $predicate = instance(Foo::class);
68
 *
69
 * $predicate(new Foo); // true
70
 * $predicate(new \DateTime); // false
71
 * $predicate(new class extends Foo {}); // true, as anonymous class extends Foo
72
 * ```
73
 *
74
 * @param string|\Closure $expected Desired class name or predicate to match it
75
 *
76
 * @return \Closure
77
 */
78
function instance($expected) : \Closure
79
{
80
    return function ($object) use ($expected) {
81 6
        return $expected instanceof \Closure ? $expected(get_class($object)) : $object instanceof $expected;
82 12
    };
83
}
84
85
/**
86
 * Predicate used to match value against some regex.
87
 *
88
 * ```php
89
 * $predicate = regex('/^https?:\/\//');
90
 *
91
 * $predicate('http://google.com'); // true
92
 * $predicate('https://google.com'); // true
93
 * $predicate('google.com'); // false
94
 * ```
95
 *
96
 * You can also pass additional arguments for preg_match call (starting from $flags)
97
 *
98
 * @see preg_match()
99
 *
100
 * @param string $regex
101
 * @param array  ...$options
102
 * @return \Closure
103
 */
104
function matches($regex, ...$options) : \Closure
105
{
106
    return function ($value) use ($regex, $options) {
107 1
        return preg_match($regex, $value, $null, ...$options) > 0;
108 1
    };
109
}
110
111
/**
112
 * Predicate used to check if value exists in given array.
113
 *
114
 * ```php
115
 * $predicate = in('foo', 'bar');
116
 *
117
 * $predicate('foo'); // true
118
 * $predicate('nope'); // false
119
 * ```
120
 *
121
 * @param mixed ...$options
122
 * @return \Closure
123
 */
124
function in(...$options) {
125
    return function ($value) use ($options) {
126 1
        return in_array($value, $options);
127 1
    };
128
}
129
130
/**
131
 * Predicate used to check if array contains value.
132
 *
133
 * ```php
134
 * $predicate = contains('foo');
135
 *
136
 * $predicate(['foo', 'bar']); // true
137
 * $predicate(['bar', 'goo']); // false
138
 * ```
139
 *
140
 * @param $value
141
 * @return \Closure
142
 */
143
function contains($value)
144
{
145
    if($value instanceof \Closure) {
146
        return function (array $array) use ($value) {
147
            foreach ($array as $item) {
148
                if($value($item)) {
149
                    return true;
150
                }
151
            }
152
153
            return false;
154
        };
155
    }
156
157
    return function (array $array) use ($value) {
158
        return array_search($value, $array) !== false;
159
    };
160
}
161
162
/**
163
 * Predicate used to check if array has key.
164
 *
165
 * ```php
166
 * $predicate = has('foo');
167
 *
168
 * $predicate(['foo' => '?', 'bar' => '#']); // true
169
 * $predicate(['bar' => '?', 'goo' => '#']); // false
170
 * ```
171
 *
172
 * @param $key
173
 * @return \Closure
174
 */
175
function has($key)
176
{
177
    return function (array $array) use ($key) {
178
        return (contains($key))(array_keys($array));
179
    };
180
}
181
182
/**
183
 * Returns constant function, that always returns specified value.
184
 *
185
 * ```php
186
 * $true   = constant(true);
187
 * $string = constant("foo");
188
 *
189
 * $true();   // true
190
 * $string(); // string(3) "foo"
191
 * ```
192
 *
193
 * @param mixed $return
194
 * @return \Closure
195
 */
196
function constant($return) : \Closure
197
{
198
    return function() use ($return) {
199 7
        return $return;
200 7
    };
201
}
202
203
/**
204
 * Returns always true predicate.
205
 *
206
 * ```php
207
 * $predicate = pass();
208
 * $predicate(); // true
209
 * ```
210
 *
211
 * @return \Closure
212
 */
213
function pass()
214
{
215 2
    return constant(true);
216
}
217
218
/**
219
 * Returns always false predicate.
220
 *
221
 * ```php
222
 * $predicate = fail();
223
 * $predicate(); // false
224
 * ```
225
 *
226
 * @return \Closure
227
 */
228
function fail()
229
{
230 1
    return constant(false);
231
}
232
233
/**
234
 * Predicate used to check if arguments matches all specified predicates. It mimics and operator behaviour.
235
 *
236
 * ```php
237
 * $instance = instance(Foo::class);
238
 * $true     = constant(true);
239
 *
240
 * $foo = new Foo;
241
 *
242
 * $predicate = all($instance, $true);
243
 * $predicate($foo); // true, it's virtually same as $instance($foo) && $true($foo)
244
 * ```
245
 *
246
 * @param \callable[] ...$functions
247
 * @return \Closure
248
 */
249
function all(callable ...$functions) : \Closure
250
{
251
    return function (...$args) use ($functions) {
252 4
        foreach ($functions as $function) {
253 4
            if (!$function(...$args)) {
254 4
                return false;
255
            }
256
        }
257
258 4
        return true;
259 10
    };
260
}
261
262
/**
263
 * Predicate used to check if arguments matches any of specified predicates. It mimics or operator behaviour.
264
 *
265
 * ```php
266
 * $instance = instance(Foo::class);
267
 * $false    = constant(false);
268
 *
269
 * $foo = new Foo;
270
 *
271
 * $predicate = any($instance, $false);
272
 * $predicate($foo); // true, it's virtually same as $instance($foo) || $false($foo)
273
 * ```
274
 *
275
 * @param \callable[] ...$functions
276
 * @return \Closure
277
 */
278
function any(callable ...$functions) : \Closure
279
{
280
    return function (...$args) use ($functions) {
281 2
        foreach ($functions as $function) {
282 2
            if ($function(...$args)) {
283 2
                return true;
284
            }
285
        }
286
287 2
        return false;
288 2
    };
289
}
290
291
/**
292
 * Tries to figure best predicate for specified argument.
293
 *
294
 * For predicates returns that predicate, for class names returns `instance($predicate)`, and for other values returns
295
 * `equals($predicate)` or `same($predicate)`, depending on $strict argument.
296
 *
297
 * @param      $predicate
298
 * @param bool $strict    Set to true if value has to be matched strictly.
299
 * @return \Closure
300
 */
301
function predicate($predicate, bool $strict = false) : \Closure
302
{
303 12
    if ($predicate instanceof \Closure) {
304 12
        return $predicate;
305 8
    } elseif (class_exists($predicate)) {
306 8
        return instance($predicate);
307
    } else {
308 3
        return $strict ? same($predicate) : equals($predicate);
309
    }
310
}
311
312
/**
313
 * Negates predicate specified in argument.
314
 *
315
 * ```php
316
 * not(constant(false))() // true, as !false === true
317
 * ```
318
 *
319
 * @param callable $predicate
320
 * @return \Closure
321
 */
322
function not($predicate) : \Closure
323
{
324 7
    $predicate = predicate($predicate);
325
    return function (...$arguments) use ($predicate) {
326 1
        return !$predicate(...$arguments);
327 7
    };
328
}
329
330
/**
331
 * Helper function used to bind argument to predicate. It can be used when called arguments order do not match arguments
332
 * that expected by predicate.
333
 *
334
 * For example, `instance` predicate checks if first argument is instance of specified class, but argument we need to
335
 * check is the second one, so we need to wrap it with `argument` helper:
336
 *
337
 * ```php
338
 * $predicate = argument(instance(Foo::class), 1);
339
 * var_dump($predicate("smth", new Foo)); // true as second argument matches instance predicate
340
 * var_dump($predicate(new Foo, "smth")); // true as second argument does not match instance predicate
341
 * ```
342
 *
343
 * @param int      $offset    Argument offset, 0 based
344
 * @param callable $predicate Predicate to match on specified offset
345
 * @param bool|int $length    [optional]
346
 *                            `true`:  will return ONLY argument at $offset
347
 *                            `false`: will return all arguments from $offset
348
 *                            int:     will return $length arguments from $offset
349
 * @return \Closure
350
 */
351
function argument(int $offset, callable $predicate, $length = true) : \Closure
352
{
353 8
    if($length === true) {
354 8
        $length = 1;
355
    } elseif($length === false) {
356
        $length = null;
357
    }
358
359
    return function (...$arguments) use ($predicate, $offset, $length) {
360 3
        return $predicate(...array_slice($arguments, $offset, $length, false));
361 8
    };
362
}
363
364
/**
365
 * Assigns predicates to arguments in relation one to one.
366
 *
367
 * ```php
368
 * $first = equals("foo");
369
 * $second = equals("bar");
370
 *
371
 * $predicate = consecutive($first, $second);
372
 *
373
 * $predicate("foo", "bar"); // true, as "foo" matches $first predicate, and "bar" matches $second predicate
374
 * $predicate("foo", "foo"); // true, as "foo" matches $first predicate, but "foo" doesn't match $second predicate
375
 * ```
376
 *
377
 * @param \callable[] ...$predicates Predicates matching order of call
378
 * @return \Closure
379
 */
380
function consecutive(callable ...$predicates)
381
{
382
    return function (...$arguments) use ($predicates) {
383 1
        foreach ($arguments as $index => $value) {
384 1
            if(!$predicates[$index]($value)) {
385 1
                return false;
386
            }
387
        }
388
389 1
        return true;
390 1
    };
391
}
392
393
function property($name, $value) {
394 4
    $predicate = $value instanceof \Closure ? $value : \Kadet\Xmpp\Utils\filter\equals($value);
395
396
    return function ($element) use ($name, $predicate) {
397 2
        return $predicate($element->$name);
398 4
    };
399
}
400
401
/**
402
 * Shorthand for calling
403
 *
404
 * ```php
405
 * all(element\name($name), element\xmlns($uri))
406
 * ```
407
 *
408
 * @see \Kadet\Xmpp\Utils\filter\element\name($name)
409
 * @see \Kadet\Xmpp\Utils\filter\element\xmlns($uri)
410
 *
411
 * @param string|\Closure $name Element name
412
 * @param string|\Closure $uri  Element namespace
413
 * @return \Closure
414
 */
415
function element($name, $uri)
416
{
417 4
    return all(element\name($name), element\xmlns($uri));
418
}
419