Completed
Push — master ( 939cef...4aee11 )
by Kacper
03:59
created

Utils/Filter.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 2
        return $argument == $value;
39 2
    };
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 1
        return $argument === $value;
60 1
    };
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 3
        return $expected instanceof \Closure ? $expected(get_class($object)) : $object instanceof $expected;
82 3
    };
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
 * Returns constant function, that always returns specified value.
132
 *
133
 * ```php
134
 * $true   = constant(true);
135
 * $string = constant("foo");
136
 *
137
 * $true();   // true
138
 * $string(); // string(3) "foo"
139
 * ```
140
 *
141
 * @param mixed $return
142
 * @return \Closure
143
 */
144
function constant($return) : \Closure
145
{
146
    return function() use ($return) {
147 7
        return $return;
148 7
    };
149
}
150
151
/**
152
 * Returns always true predicate.
153
 *
154
 * ```php
155
 * $predicate = pass();
156
 * $predicate(); // true
157
 * ```
158
 *
159
 * @return \Closure
160
 */
161
function pass()
162
{
163 2
    return constant(true);
164
}
165
166
/**
167
 * Returns always false predicate.
168
 *
169
 * ```php
170
 * $predicate = fail();
171
 * $predicate(); // false
172
 * ```
173
 *
174
 * @return \Closure
175
 */
176
function fail()
177
{
178 1
    return constant(false);
179
}
180
181
/**
182
 * Predicate used to check if arguments matches all specified predicates. It mimics and operator behaviour.
183
 *
184
 * ```php
185
 * $instance = instance(Foo::class);
186
 * $true     = constant(true);
187
 *
188
 * $foo = new Foo;
189
 *
190
 * $predicate = all($instance, $true);
191
 * $predicate($foo); // true, it's virtually same as $instance($foo) && $true($foo)
192
 * ```
193
 *
194
 * @param \callable[] ...$functions
195
 * @return \Closure
196
 */
197
function all(callable ...$functions) : \Closure
198
{
199
    return function (...$args) use ($functions) {
200 2
        foreach ($functions as $function) {
201 2
            if (!$function(...$args)) {
202 2
                return false;
203
            }
204
        }
205
206 2
        return true;
207 2
    };
208
}
209
210
/**
211
 * Predicate used to check if arguments matches any of specified predicates. It mimics or operator behaviour.
212
 *
213
 * ```php
214
 * $instance = instance(Foo::class);
215
 * $false    = constant(false);
216
 *
217
 * $foo = new Foo;
218
 *
219
 * $predicate = any($instance, $false);
220
 * $predicate($foo); // true, it's virtually same as $instance($foo) || $false($foo)
221
 * ```
222
 *
223
 * @param \callable[] ...$functions
224
 * @return \Closure
225
 */
226
function any(callable ...$functions) : \Closure
227
{
228
    return function (...$args) use ($functions) {
229 2
        foreach ($functions as $function) {
230 2
            if ($function(...$args)) {
231 2
                return true;
232
            }
233
        }
234
235 2
        return false;
236 2
    };
237
}
238
239
/**
240
 * Tries to figure best predicate for specified argument.
241
 *
242
 * For predicates returns that predicate, for class names returns `instance($predicate)`, and for other values returns
243
 * `equals($predicate)` or `same($predicate)`, depending on $strict argument.
244
 *
245
 * @param      $predicate
246
 * @param bool $strict    Set to true if value has to be matched strictly.
247
 * @return \Closure
248
 */
249
function predicate($predicate, bool $strict = false) : \Closure
250
{
251
    if ($predicate instanceof \Closure) {
252
        return $predicate;
253
    } elseif (class_exists($predicate)) {
254
        return instance($predicate);
255
    } else {
256
        return $strict ? same($predicate) : equals($predicate);
257
    }
258
}
259
260
/**
261
 * Negates predicate specified in argument.
262
 *
263
 * ```php
264
 * not(constant(false))() // true, as !false === true
265
 * ```
266
 *
267
 * @param callable $predicate
268
 * @return \Closure
269
 */
270
function not(callable $predicate) : \Closure
271
{
272
    return function (...$arguments) use ($predicate) {
273 1
        return !$predicate(...$arguments);
274 1
    };
275
}
276
277
/**
278
 * Helper function used to bind argument to predicate. It can be used when called arguments order do not match arguments
279
 * that expected by predicate.
280
 *
281
 * For example, `instance` predicate checks if first argument is instance of specified class, but argument we need to
282
 * check is the second one, so we need to wrap it with `argument` helper:
283
 *
284
 * ```php
285
 * $predicate = argument(instance(Foo::class), 1);
286
 * var_dump($predicate("smth", new Foo)); // true as second argument matches instance predicate
287
 * var_dump($predicate(new Foo, "smth")); // true as second argument does not match instance predicate
288
 * ```
289
 *
290
 * @param int      $offset    Argument offset, 0 based
291
 * @param callable $predicate Predicate to match on specified offset
292
 * @param bool|int $length    [optional]
293
 *                            `true`:  will return ONLY argument at $offset
294
 *                            `false`: will return all arguments from $offset
295
 *                            int:     will return $length arguments from $offset
296
 * @return \Closure
297
 */
298
function argument(int $offset, callable $predicate, $length = true) : \Closure
299
{
300
    if($length === true) {
301
        $length = 1;
302
    } elseif($length === false) {
303
        $length = null;
304
    }
305
306
    return function (...$arguments) use ($predicate, $offset, $length) {
307 3
        return $predicate(...array_slice($arguments, $offset, $length, false));
308
    };
309
}
310
311
/**
312
 * Assigns predicates to arguments in relation one to one.
313
 *
314
 * ```php
315
 * $first = equals("foo");
316
 * $second = equals("bar");
317
 *
318
 * $predicate = consecutive($first, $second);
319
 *
320
 * $predicate("foo", "bar"); // true, as "foo" matches $first predicate, and "bar" matches $second predicate
321
 * $predicate("foo", "foo"); // true, as "foo" matches $first predicate, but "foo" doesn't match $second predicate
322
 * ```
323
 *
324
 * @param \callable[] ...$predicates Predicates matching order of call
325
 * @return \Closure
326
 */
327
function consecutive(callable ...$predicates)
328
{
329
    return function (...$arguments) use ($predicates) {
330 1
        foreach ($arguments as $index => $value) {
331 1
            if(!$predicates[$index]($value)) {
332 1
                return false;
333
            }
334
        }
335
336 1
        return true;
337 1
    };
338
}
339
340
341
/**
342
 * Shorthand for calling
343
 *
344
 * ```php
345
 * all(element\name($name), element\xmlns($uri))
346
 * ```
347
 *
348
 * @see \Kadet\Xmpp\Utils\filter\element\name($name)
349
 * @see \Kadet\Xmpp\Utils\filter\element\xmlns($uri)
350
 *
351
 * @param string|\Closure $name Element name
352
 * @param string|\Closure $uri  Element namespace
353
 * @return \Closure
354
 */
355
function element($name, $uri)
356
{
357
    return all(element\name($name), element\xmlns($uri));
358
}
359