@@ -126,6 +126,9 @@ |
||
126 | 126 | return [$name, $namespace]; |
127 | 127 | } |
128 | 128 | |
129 | + /** |
|
130 | + * @param string|null $prefix |
|
131 | + */ |
|
129 | 132 | private function _lookup($prefix, $namespaces) |
130 | 133 | { |
131 | 134 | if ($prefix === 'xmlns') { |
@@ -71,13 +71,13 @@ discard block |
||
71 | 71 | xml_parser_set_option($this->_parser, XML_OPTION_SKIP_WHITE, 1); |
72 | 72 | xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, 0); |
73 | 73 | |
74 | - xml_set_element_handler($this->_parser, function ($parser, $name, $attrs) { |
|
74 | + xml_set_element_handler($this->_parser, function($parser, $name, $attrs) { |
|
75 | 75 | $this->handleElementStart($name, $attrs); |
76 | - }, function ($parser, $name) { |
|
76 | + }, function($parser, $name) { |
|
77 | 77 | $this->handleElementEnd(); |
78 | 78 | }); |
79 | 79 | |
80 | - xml_set_character_data_handler($this->_parser, function ($parser, $data) { |
|
80 | + xml_set_character_data_handler($this->_parser, function($parser, $data) { |
|
81 | 81 | $this->handleTextData($data); |
82 | 82 | }); |
83 | 83 | |
@@ -119,10 +119,10 @@ discard block |
||
119 | 119 | list($attributes, $namespaces) = $this->_attributes($attrs); |
120 | 120 | list($tag, $prefix) = XmlElement::resolve($name); |
121 | 121 | |
122 | - $uri = $this->_lookup($prefix, $namespaces); |
|
122 | + $uri = $this->_lookup($prefix, $namespaces); |
|
123 | 123 | |
124 | 124 | /** @var XmlElement $element */ |
125 | - $element = $this->factory->create($uri, $tag, [ $name, $uri ]); |
|
125 | + $element = $this->factory->create($uri, $tag, [$name, $uri]); |
|
126 | 126 | foreach ($attributes as $name => $value) { |
127 | 127 | $element->setAttribute($name, $value); |
128 | 128 | } |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | if (count($this->_stack) > 1) { |
138 | 138 | end($this->_stack)->append($element); |
139 | 139 | } |
140 | - $this->emit('parse.begin', [ $element ]); |
|
140 | + $this->emit('parse.begin', [$element]); |
|
141 | 141 | |
142 | 142 | $this->_stack[] = $element; |
143 | 143 | } |
@@ -150,10 +150,10 @@ discard block |
||
150 | 150 | |
151 | 151 | $element = array_pop($this->_stack); |
152 | 152 | if (count($this->_stack) == 1) { |
153 | - $this->emit('element', [ $element ]); |
|
153 | + $this->emit('element', [$element]); |
|
154 | 154 | } |
155 | 155 | |
156 | - $this->emit('parse.end', [ $element ]); |
|
156 | + $this->emit('parse.end', [$element]); |
|
157 | 157 | } |
158 | 158 | |
159 | 159 | private function handleTextData($data) |
@@ -40,6 +40,6 @@ |
||
40 | 40 | |
41 | 41 | private function generateMessage(Error $error) : string |
42 | 42 | { |
43 | - return 'Stream error: '.str_replace('-', ' ', $error->kind).($error->text ? " ({$error->text})" : null); |
|
43 | + return 'Stream error: ' . str_replace('-', ' ', $error->kind) . ($error->text ? " ({$error->text})" : null); |
|
44 | 44 | } |
45 | 45 | } |
@@ -40,7 +40,7 @@ discard block |
||
40 | 40 | |
41 | 41 | public function removeListener($event, callable $listener) |
42 | 42 | { |
43 | - if(!isset($this->listeners[$event])) { |
|
43 | + if (!isset($this->listeners[$event])) { |
|
44 | 44 | return false; |
45 | 45 | } |
46 | 46 | |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | |
67 | 67 | private function addListener($event, callable $listener, int $priority = 0) |
68 | 68 | { |
69 | - if(!isset($this->listeners[$event])) { |
|
69 | + if (!isset($this->listeners[$event])) { |
|
70 | 70 | $this->listeners[$event] = new PriorityCollection(); |
71 | 71 | } |
72 | 72 | |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | } |
81 | 81 | |
82 | 82 | $condition = $this->emitterResolveCondition($condition); |
83 | - return function (...$arguments) use ($listener, $condition) { |
|
83 | + return function(...$arguments) use ($listener, $condition) { |
|
84 | 84 | if ($condition(...$arguments)) { |
85 | 85 | $listener(...$arguments); |
86 | 86 | } |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | |
90 | 90 | private function getOnceCallable(callable $listener, $event) : callable |
91 | 91 | { |
92 | - return $onceListener = function (...$arguments) use (&$onceListener, $event, $listener) { |
|
92 | + return $onceListener = function(...$arguments) use (&$onceListener, $event, $listener) { |
|
93 | 93 | $this->removeListener($event, $onceListener); |
94 | 94 | |
95 | 95 | $listener(...$arguments); |
@@ -19,28 +19,28 @@ discard block |
||
19 | 19 | |
20 | 20 | function xmlns($uri) |
21 | 21 | { |
22 | - return function (XmlElement $element) use ($uri) { |
|
22 | + return function(XmlElement $element) use ($uri) { |
|
23 | 23 | return $element->namespaceURI === $uri; |
24 | 24 | }; |
25 | 25 | } |
26 | 26 | |
27 | 27 | function tag($name) |
28 | 28 | { |
29 | - return function (XmlElement $element) use ($name) { |
|
29 | + return function(XmlElement $element) use ($name) { |
|
30 | 30 | return $element->localName === $name; |
31 | 31 | }; |
32 | 32 | } |
33 | 33 | |
34 | 34 | function ofType($class) |
35 | 35 | { |
36 | - return function ($object) use ($class) { |
|
36 | + return function($object) use ($class) { |
|
37 | 37 | return $object instanceof $class; |
38 | 38 | }; |
39 | 39 | } |
40 | 40 | |
41 | 41 | function all(callable ...$functions) |
42 | 42 | { |
43 | - return function (...$args) use ($functions) { |
|
43 | + return function(...$args) use ($functions) { |
|
44 | 44 | foreach ($functions as $function) { |
45 | 45 | if (!$function(...$args)) { |
46 | 46 | return false; |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | |
54 | 54 | function any(callable ...$functions) |
55 | 55 | { |
56 | - return function (...$args) use ($functions) { |
|
56 | + return function(...$args) use ($functions) { |
|
57 | 57 | foreach ($functions as $function) { |
58 | 58 | if ($function(...$args)) { |
59 | 59 | return true; |
@@ -39,7 +39,7 @@ |
||
39 | 39 | |
40 | 40 | public function getText() |
41 | 41 | { |
42 | - if($text = $this->query(".//xmpp:text")->with('xmpp', 'urn:ietf:params:xml:ns:xmpp-streams')->query()->item(0)) { |
|
42 | + if ($text = $this->query(".//xmpp:text")->with('xmpp', 'urn:ietf:params:xml:ns:xmpp-streams')->query()->item(0)) { |
|
43 | 43 | return $text; |
44 | 44 | } |
45 | 45 |
@@ -220,7 +220,7 @@ discard block |
||
220 | 220 | * Retrieves array of matching elements |
221 | 221 | * |
222 | 222 | * @param string $name Requested element tag name |
223 | - * @param null $uri Requested element namespace |
|
223 | + * @param null|string $uri Requested element namespace |
|
224 | 224 | * |
225 | 225 | * @return XmlElement[] Found Elements |
226 | 226 | */ |
@@ -248,6 +248,9 @@ discard block |
||
248 | 248 | return $this->elements($name, $uri)[$index] ?? false; |
249 | 249 | } |
250 | 250 | |
251 | + /** |
|
252 | + * @param \Closure $predicate |
|
253 | + */ |
|
251 | 254 | public function all($predicate) { |
252 | 255 | $predicate = filter\predicate($predicate); |
253 | 256 | return array_filter($this->_children, $predicate); |
@@ -68,27 +68,27 @@ discard block |
||
68 | 68 | |
69 | 69 | public function xml($clean = true): string |
70 | 70 | { |
71 | - if($this->namespace && $this->_prefix === null) { |
|
71 | + if ($this->namespace && $this->_prefix === null) { |
|
72 | 72 | $this->_prefix = $this->lookupPrefix($this->namespace); |
73 | 73 | } |
74 | 74 | |
75 | 75 | $attributes = $this->attributes(); |
76 | 76 | |
77 | 77 | $result = "<{$this->name}"; |
78 | - $result .= ' '.implode(' ', array_map(function($key, $value) { |
|
79 | - return $key.'="'.htmlspecialchars($value, ENT_QUOTES).'"'; |
|
78 | + $result .= ' ' . implode(' ', array_map(function($key, $value) { |
|
79 | + return $key . '="' . htmlspecialchars($value, ENT_QUOTES) . '"'; |
|
80 | 80 | }, array_keys($attributes), array_values($attributes))); |
81 | 81 | |
82 | - if(!empty($this->_children)) { |
|
83 | - $result .= ">".implode('', array_map(function($element) { |
|
84 | - if(is_string($element)) { |
|
82 | + if (!empty($this->_children)) { |
|
83 | + $result .= ">" . implode('', array_map(function($element) { |
|
84 | + if (is_string($element)) { |
|
85 | 85 | return htmlspecialchars($element); |
86 | 86 | } elseif ($element instanceof XmlElement) { |
87 | 87 | return $element->xml(false); |
88 | 88 | } |
89 | 89 | |
90 | 90 | return (string)$element; |
91 | - }, $this->_children))."</{$this->name}>"; |
|
91 | + }, $this->_children)) . "</{$this->name}>"; |
|
92 | 92 | } else { |
93 | 93 | $result .= "/>"; |
94 | 94 | } |
@@ -98,12 +98,12 @@ discard block |
||
98 | 98 | |
99 | 99 | public function setAttribute(string $attribute, $value, string $uri = null) |
100 | 100 | { |
101 | - if($uri === 'http://www.w3.org/2000/xmlns/') { |
|
101 | + if ($uri === 'http://www.w3.org/2000/xmlns/') { |
|
102 | 102 | $this->_namespaces[(string)$value] = $attribute; |
103 | 103 | return; |
104 | 104 | } |
105 | 105 | |
106 | - if($uri !== null) { |
|
106 | + if ($uri !== null) { |
|
107 | 107 | $attribute = $this->_prefix($attribute, $uri); |
108 | 108 | } |
109 | 109 | |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | |
113 | 113 | public function getAttribute(string $attribute, string $uri = null) |
114 | 114 | { |
115 | - if($uri !== null) { |
|
115 | + if ($uri !== null) { |
|
116 | 116 | $attribute = $this->_prefix($attribute, $uri); |
117 | 117 | } |
118 | 118 | |
@@ -124,11 +124,11 @@ discard block |
||
124 | 124 | */ |
125 | 125 | public function getNamespaces($parent = true): array |
126 | 126 | { |
127 | - if(!$this->_parent) { |
|
127 | + if (!$this->_parent) { |
|
128 | 128 | return $this->_namespaces; |
129 | 129 | } |
130 | 130 | |
131 | - if($parent) { |
|
131 | + if ($parent) { |
|
132 | 132 | return array_merge($this->_namespaces, $this->_parent->getNamespaces()); |
133 | 133 | } else { |
134 | 134 | return array_diff_assoc($this->_namespaces, $this->_parent->getNamespaces()); |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | { |
140 | 140 | $namespaces = $this->getNamespaces(false); |
141 | 141 | $namespaces = array_map(function($prefix, $uri) { |
142 | - return [ $prefix ? "xmlns:{$prefix}" : 'xmlns', $uri ]; |
|
142 | + return [$prefix ? "xmlns:{$prefix}" : 'xmlns', $uri]; |
|
143 | 143 | }, array_values($namespaces), array_keys($namespaces)); |
144 | 144 | |
145 | 145 | return array_merge( |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | |
161 | 161 | public function setNamespace(string $uri, $prefix = false) |
162 | 162 | { |
163 | - if($prefix === false) { |
|
163 | + if ($prefix === false) { |
|
164 | 164 | $prefix = $this->_prefix; |
165 | 165 | } |
166 | 166 | |
@@ -179,15 +179,15 @@ discard block |
||
179 | 179 | |
180 | 180 | public function append($element) |
181 | 181 | { |
182 | - if(!is_string($element) && !$element instanceof XmlElement) { |
|
182 | + if (!is_string($element) && !$element instanceof XmlElement) { |
|
183 | 183 | throw new InvalidArgumentException(helper\format('$element should be either string or object of {class} class, {type} given', [ |
184 | 184 | 'class' => XmlElement::class, |
185 | 185 | 'type' => helper\typeof($element) |
186 | 186 | ])); |
187 | 187 | } |
188 | 188 | |
189 | - if($element instanceof XmlElement) { |
|
190 | - $element->parent = $this; |
|
189 | + if ($element instanceof XmlElement) { |
|
190 | + $element->parent = $this; |
|
191 | 191 | } |
192 | 192 | |
193 | 193 | $this->_children[] = $element; |
@@ -195,7 +195,7 @@ discard block |
||
195 | 195 | |
196 | 196 | public function getName() |
197 | 197 | { |
198 | - return ($this->_prefix ? $this->prefix.':' : null).$this->localName; |
|
198 | + return ($this->_prefix ? $this->prefix . ':' : null) . $this->localName; |
|
199 | 199 | } |
200 | 200 | |
201 | 201 | public function getPrefix() |
@@ -211,7 +211,7 @@ discard block |
||
211 | 211 | protected function setParent(XmlElement $parent) |
212 | 212 | { |
213 | 213 | $this->_parent = $parent; |
214 | - if($this->namespace === false) { |
|
214 | + if ($this->namespace === false) { |
|
215 | 215 | $this->namespace = $parent->namespace; |
216 | 216 | } |
217 | 217 | } |
@@ -227,7 +227,7 @@ discard block |
||
227 | 227 | public function elements($name, $uri = null) : array |
228 | 228 | { |
229 | 229 | $predicate = filter\tag($name); |
230 | - if($uri !== null) { |
|
230 | + if ($uri !== null) { |
|
231 | 231 | $predicate = filter\all($predicate, filter\xmlns($uri)); |
232 | 232 | } |
233 | 233 |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | public function query(string $query = null) |
34 | 34 | { |
35 | 35 | /** @var \DOMNode $element */ |
36 | - foreach($this->_xpath->query($query ?: $this->_query) as $element) { |
|
36 | + foreach ($this->_xpath->query($query ?: $this->_query) as $element) { |
|
37 | 37 | yield $this->getElementFromPath($element->getNodePath()); |
38 | 38 | } |
39 | 39 | } |
@@ -70,14 +70,14 @@ discard block |
||
70 | 70 | array_shift($path); |
71 | 71 | |
72 | 72 | $current = $this->_context; |
73 | - foreach($path as $chunk) { |
|
73 | + foreach ($path as $chunk) { |
|
74 | 74 | // Chunk is in format node-name[index], parse it with regex |
75 | 75 | preg_match('/([\w\*]+)(?:\[([0-9]+)\])?/', $chunk, $matches); |
76 | 76 | |
77 | 77 | $name = $matches[1]; |
78 | 78 | $index = isset($matches[2]) ? $matches[2] - 1 : 0; |
79 | 79 | |
80 | - if($name == '*') { |
|
80 | + if ($name == '*') { |
|
81 | 81 | // Path returns * if namespace occurs so we need to obtain index-th child |
82 | 82 | $current = $current->children[$index]; |
83 | 83 | } else { |
@@ -76,25 +76,25 @@ discard block |
||
76 | 76 | |
77 | 77 | $this->_parser = $parser; |
78 | 78 | |
79 | - $this->on('element', function (Error $element) { |
|
79 | + $this->on('element', function(Error $element) { |
|
80 | 80 | $this->handleError($element); |
81 | 81 | }, with\ofType(Error::class)); |
82 | 82 | |
83 | - $this->_parser->on('parse.begin', function (XmlElement $stream) { |
|
83 | + $this->_parser->on('parse.begin', function(XmlElement $stream) { |
|
84 | 84 | $this->_stream = $stream; |
85 | - $this->emit('stream.open', [ $stream ]); |
|
85 | + $this->emit('stream.open', [$stream]); |
|
86 | 86 | }, with\all(with\tag('stream'), with\xmlns(self::NAMESPACE_URI))); |
87 | 87 | |
88 | - $this->_parser->on('parse.end', function (XmlElement $stream) { |
|
89 | - $this->emit('stream.close', [ $stream ]); |
|
88 | + $this->_parser->on('parse.end', function(XmlElement $stream) { |
|
89 | + $this->emit('stream.close', [$stream]); |
|
90 | 90 | $this->_stream = null; |
91 | 91 | }, with\all(with\tag('stream'), with\xmlns(self::NAMESPACE_URI))); |
92 | 92 | |
93 | 93 | $this->on('data', [$this->_parser, 'parse']); |
94 | - $this->_parser->on('element', function (...$arguments) { |
|
94 | + $this->_parser->on('element', function(...$arguments) { |
|
95 | 95 | $this->emit('element', $arguments); |
96 | 96 | }); |
97 | - $this->on('close', function () { $this->_isOpened = false; }); |
|
97 | + $this->on('close', function() { $this->_isOpened = false; }); |
|
98 | 98 | } |
99 | 99 | |
100 | 100 | /** |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | */ |
107 | 107 | public function write($data) |
108 | 108 | { |
109 | - $this->emit('send.'.($data instanceof XmlElement ? 'element' : 'text'), [ $data ]); |
|
109 | + $this->emit('send.' . ($data instanceof XmlElement ? 'element' : 'text'), [$data]); |
|
110 | 110 | |
111 | 111 | return parent::write($data); |
112 | 112 | } |
@@ -136,7 +136,7 @@ discard block |
||
136 | 136 | */ |
137 | 137 | public function close() |
138 | 138 | { |
139 | - if($this->isOpened()) { |
|
139 | + if ($this->isOpened()) { |
|
140 | 140 | $this->write('</stream:stream>'); |
141 | 141 | $this->_isOpened = false; |
142 | 142 | } |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | |
172 | 172 | private function handleError(Error $element) |
173 | 173 | { |
174 | - if($this->emit('stream.error', [ $element ])) { |
|
174 | + if ($this->emit('stream.error', [$element])) { |
|
175 | 175 | throw new StreamErrorException($element); |
176 | 176 | } |
177 | 177 |
@@ -41,14 +41,14 @@ |
||
41 | 41 | 'port' => $port |
42 | 42 | ]); |
43 | 43 | |
44 | - if($stream = @stream_socket_client("tcp://$ip:$port")) { |
|
44 | + if ($stream = @stream_socket_client("tcp://$ip:$port")) { |
|
45 | 45 | $stream = new TcpStream($stream, $this->_loop); |
46 | - $this->emit('connect', [ $stream ]); |
|
46 | + $this->emit('connect', [$stream]); |
|
47 | 47 | return $stream; |
48 | 48 | } |
49 | 49 | } |
50 | 50 | |
51 | - throw new \RuntimeException('Cannot connect to '.$this->_host); |
|
51 | + throw new \RuntimeException('Cannot connect to ' . $this->_host); |
|
52 | 52 | } |
53 | 53 | |
54 | 54 | public function __construct(string $host, LoopInterface $loop) |