1
|
|
|
<?php |
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\element; |
17
|
|
|
|
18
|
|
|
use Kadet\Xmpp\Xml\XmlElement; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Predicate used to check if given element is from specified namespace. |
22
|
|
|
* |
23
|
|
|
* Assume that `$element` is representation of: |
24
|
|
|
* |
25
|
|
|
* ```xml |
26
|
|
|
* <element xmlns="uri:xmlns:foo" /> |
27
|
|
|
* ``` |
28
|
|
|
* |
29
|
|
|
* ```php |
30
|
|
|
* $foo = xmlns("uri:xmlns:foo"); |
31
|
|
|
* $bar = xmlns("uri:xmlns:bar"); |
32
|
|
|
* |
33
|
|
|
* $foo($element); // true |
34
|
|
|
* $bar($element); // false, as element is from uri:xmlns:foo namespace |
35
|
|
|
* ``` |
36
|
|
|
* |
37
|
|
|
* @param string|\Closure $uri Expected XML namespace URI or predicate |
38
|
|
|
* @return \Closure |
39
|
|
|
*/ |
40
|
|
View Code Duplication |
function xmlns($uri) |
41
|
|
|
{ |
42
|
|
|
$predicate = $uri instanceof \Closure ? $uri : \Kadet\Xmpp\Utils\filter\same($uri); |
43
|
|
|
|
44
|
|
|
return function ($element) use ($predicate) { |
45
|
|
|
if (!$element instanceof XmlElement) { |
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $predicate($element->namespace); |
50
|
|
|
}; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Predicate used to check if given element has specified name. |
55
|
|
|
* |
56
|
|
|
* Assume that `$element` is representation of: |
57
|
|
|
* |
58
|
|
|
* ```xml |
59
|
|
|
* <foo /> |
60
|
|
|
* ``` |
61
|
|
|
* |
62
|
|
|
* ```php |
63
|
|
|
* $foo = name("foo"); |
64
|
|
|
* $bar = name("bar"); |
65
|
|
|
* |
66
|
|
|
* $foo($element); // true |
67
|
|
|
* $bar($element); // false, as element name is foo |
68
|
|
|
* ``` |
69
|
|
|
* |
70
|
|
|
* @param string|\Closure $name Expected element name or predicate. |
71
|
|
|
* @return \Closure |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
function name($name) |
74
|
|
|
{ |
75
|
|
|
$predicate = $name instanceof \Closure ? $name : \Kadet\Xmpp\Utils\filter\same($name); |
76
|
|
|
|
77
|
|
|
return function ($element) use ($predicate) { |
78
|
|
|
if (!$element instanceof XmlElement) { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $predicate($element->localName); |
83
|
|
|
}; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Predicate used to check if element's attribute matches value. |
88
|
|
|
* |
89
|
|
|
* Assume that `$element` is representation of: |
90
|
|
|
* |
91
|
|
|
* ```xml |
92
|
|
|
* <element foo="yes" bar="no" /> |
93
|
|
|
* ``` |
94
|
|
|
* |
95
|
|
|
* ```php |
96
|
|
|
* $foo = attribute("foo", "yes"); |
97
|
|
|
* $bar = attribute("foo", "string") |
98
|
|
|
* |
99
|
|
|
* $foo($element); // true |
100
|
|
|
* $bar($element); // false, as element name is foo |
101
|
|
|
* ``` |
102
|
|
|
* |
103
|
|
|
* @param string|\Closure $name Expected element name or predicate. |
104
|
|
|
* @param $value |
105
|
|
|
* @return \Closure |
106
|
|
|
*/ |
107
|
|
|
function attribute($name, $value) |
108
|
|
|
{ |
109
|
|
|
$predicate = $value instanceof \Closure ? $value : \Kadet\Xmpp\Utils\filter\equals($value); |
110
|
|
|
|
111
|
|
|
return function ($element) use ($name, $predicate) { |
112
|
|
|
if (!$element instanceof XmlElement) { |
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $predicate($element->getAttribute($name)); |
|
|
|
|
117
|
|
|
}; |
118
|
|
|
} |
119
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.