Completed
Push — master ( 7a5aed...4799e1 )
by Kacper
06:25
created

Filter.php ➔ element()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * XMPP Library
4
 *
5
 * Copyright (C) 2016, Some right 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
    use Kadet\Xmpp\Exception\InvalidArgumentException;
18
    use Kadet\Xmpp\Xml\XmlElement;
19
    use Kadet\Xmpp\Utils\helper;
20
21
    function element(string $name, string $uri)
22
    {
23
        return all(tag($name), xmlns($uri));
24
    }
25 1
26 1
    function xmlns($uri)
27
    {
28
        return function ($element) use ($uri) {
29
            if(!$element instanceof XmlElement) {
30
                return false;
31
            }
32 2
33 2
            return $element->namespace === $uri;
34
        };
35
    }
36
37
    function tag($name)
38
    {
39
        return function ($element) use ($name) {
40
            if(!$element instanceof XmlElement) {
41
                return false;
42
            }
43
44
            return $element->localName === $name;
45
        };
46 1
    }
47 1
48 1
    function ofType($class)
49
    {
50
        return function ($object) use ($class) {
51
            return $object instanceof $class;
52 1
        };
53 1
    }
54
55 View Code Duplication
    function all(callable ...$functions)
56
    {
57
        return function (...$args) use ($functions) {
58
            foreach ($functions as $function) {
59
                if (!$function(...$args)) {
60
                    return false;
61
                }
62
            }
63
64
            return true;
65
        };
66
    }
67
68 View Code Duplication
    function any(callable ...$functions)
69
    {
70
        return function (...$args) use ($functions) {
71 3
            foreach ($functions as $function) {
72 3
                if ($function(...$args)) {
73
                    return true;
74
                }
75
            }
76
77
            return false;
78
        };
79
    }
80
81
    function predicate($predicate) : callable
82
    {
83
        if (is_callable($predicate)) {
84
            return $predicate;
85
        } elseif (class_exists($predicate)) {
86
            return ofType($predicate);
87
        } else {
88
            throw new InvalidArgumentException('$condition must be either class-name or callable, ' . helper\typeof($predicate) . ' given.');
89
        }
90
    }
91
92
    function argument($name, $value) {
93
        return function($element) use ($name, $value) {
94
            if(!$element instanceof XmlElement) {
95
                return false;
96
            }
97
98
            return is_callable($value) ? $value($element->getAttribute($name)) : $element->getAttribute($name) === $value;
99
        };
100
    }
101
}
102
103
namespace Kadet\Xmpp\Utils\filter\stanza {
104
    use Kadet\Xmpp\Utils\filter;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
105
106
    function id($id) {
107
        return filter\argument('id', $id);
108
    }
109
110
    function type($type) {
111
        return filter\argument('type', $type);
112
    }
113
}
114