Completed
Push — master ( fcd09b...a3140b )
by Kacper
08:42
created

Filter.php ➔ equals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 9.4285
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 1
        return $argument == $value;
39 1
    };
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 $class Desired class name.
75
 *
76
 * @return \Closure
77
 */
78
function instance($class) : \Closure
79
{
80
    return function ($object) use ($class) {
81 3
        return $object instanceof $class;
82 3
    };
83
}
84
85
/**
86
 * Returns constant function, that always returns specified value.
87
 *
88
 * ```php
89
 * $true   = constant(true);
90
 * $string = constant("foo");
91
 *
92
 * $true();   // true
93
 * $string(); // string(3) "foo"
94
 * ```
95
 *
96
 * @param mixed $return
97
 * @return \Closure
98
 */
99
function constant($return) : \Closure
100
{
101
    return function() use ($return) {
102 4
        return $return;
103 4
    };
104
}
105
106
/**
107
 * Predicate used to check if arguments matches all specified predicates. It mimics and operator behaviour.
108
 *
109
 * ```php
110
 * $instance = instance(Foo::class);
111
 * $true     = constant(true);
112
 *
113
 * $foo = new Foo;
114
 *
115
 * $predicate = all($instance, $true);
116
 * $predicate($foo); // true, it's virtually same as $instance($foo) && $true($foo)
117
 * ```
118
 *
119
 * @param \callable[] ...$functions
120
 * @return \Closure
121
 */
122
function all(callable ...$functions) : \Closure
123
{
124
    return function (...$args) use ($functions) {
125 2
        foreach ($functions as $function) {
126 2
            if (!$function(...$args)) {
127 2
                return false;
128
            }
129
        }
130
131 2
        return true;
132 2
    };
133
}
134
135
/**
136
 * Predicate used to check if arguments matches any of specified predicates. It mimics or operator behaviour.
137
 *
138
 * ```php
139
 * $instance = instance(Foo::class);
140
 * $false    = constant(false);
141
 *
142
 * $foo = new Foo;
143
 *
144
 * $predicate = any($instance, $false);
145
 * $predicate($foo); // true, it's virtually same as $instance($foo) || $false($foo)
146
 * ```
147
 *
148
 * @param \callable[] ...$functions
149
 * @return \Closure
150
 */
151
function any(callable ...$functions) : \Closure
152
{
153
    return function (...$args) use ($functions) {
154 2
        foreach ($functions as $function) {
155 2
            if ($function(...$args)) {
156 2
                return true;
157
            }
158
        }
159
160 2
        return false;
161 2
    };
162
}
163
164
/**
165
 * Tries to figure best predicate for specified argument.
166
 *
167
 * For predicates returns that predicate, for class names returns `instance($predicate)`, and for other values returns
168
 * `equals($predicate)` or `same($predicate)`, depending on $strict argument.
169
 *
170
 * @param      $predicate
171
 * @param bool $strict    Set to true if value has to be matched strictly.
172
 * @return \Closure
173
 */
174
function predicate($predicate, bool $strict = false) : \Closure
175
{
176
    if ($predicate instanceof \Closure) {
177
        return $predicate;
178
    } elseif (class_exists($predicate)) {
179
        return instance($predicate);
180
    } else {
181
        return $strict ? same($predicate) : equals($predicate);
182
    }
183
}
184
185
/**
186
 * Negates predicate specified in argument.
187
 *
188
 * ```php
189
 * not(constant(false))() // true, as !false === true
190
 * ```
191
 *
192
 * @param callable $predicate
193
 * @return \Closure
194
 */
195
function not(callable $predicate) : \Closure
196
{
197
    return function (...$arguments) use ($predicate) {
198 1
        return !$predicate(...$arguments);
199 1
    };
200
}
201
202
/**
203
 * Helper function used to bind argument to predicate. It can be used when called arguments order do not match arguments
204
 * that expected by predicate.
205
 *
206
 * For example, `instance` predicate checks if first argument is instance of specified class, but argument we need to
207
 * check is the second one, so we need to wrap it with `argument` helper:
208
 *
209
 * ```php
210
 * $predicate = argument(instance(Foo::class), 2);
211
 * var_dump($predicate("smth", new Foo)); // true as second argument matches instance predicate
212
 * var_dump($predicate(new Foo, "smth")); // true as second argument does not match instance predicate
213
 * ```
214
 *
215
 * @param callable $predicate Predicate to match on specified offset
216
 * @param int      $offset    Argument offset
217
 * @param bool|int $length    [optional]
218
 *                            `true`:  will return ONLY argument at $offset
219
 *                            `false`: will return all arguments from $offset
220
 *                            int:     will return $length arguments from $offset
221
 *
222
 * @return \Closure
223
 */
224
function argument(callable $predicate, int $offset, $length = true) : \Closure
225
{
226
    if($length === true) {
227
        $length = 1;
228
    } elseif($length === false) {
229
        $length = null;
230
    }
231
232
    return function (...$arguments) use ($predicate, $offset, $length) {
233 3
        return $predicate(...array_slice($arguments, $offset, $length, false));
234
    };
235
}
236
237
/**
238
 * Shorthand for calling
239
 *
240
 * ```php
241
 * all(element\name($name), element\xmlns($uri))
242
 * ```
243
 *
244
 * @see \Kadet\Xmpp\Utils\filter\element\name($name)
245
 * @see \Kadet\Xmpp\Utils\filter\element\xmlns($uri)
246
 *
247
 * @param string $name Element name
248
 * @param string $uri  Element namespace
249
 * @return \Closure
250
 */
251
function element(string $name, string $uri)
252
{
253
    return all(element\name($name), element\xmlns($uri));
254
}
255
256