@@ -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); |
@@ -88,18 +88,18 @@ discard block |
||
| 88 | 88 | |
| 89 | 89 | public function xml($clean = true): string |
| 90 | 90 | { |
| 91 | - if($this->namespace && $this->_prefix === null) { |
|
| 91 | + if ($this->namespace && $this->_prefix === null) { |
|
| 92 | 92 | $this->_prefix = $this->lookupPrefix($this->namespace); |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | $attributes = $this->attributes(); |
| 96 | 96 | |
| 97 | 97 | $result = "<{$this->name}"; |
| 98 | - $result .= ' '.implode(' ', array_map(function($key, $value) { |
|
| 99 | - return $key.'="'.htmlspecialchars($value, ENT_QUOTES).'"'; |
|
| 98 | + $result .= ' ' . implode(' ', array_map(function($key, $value) { |
|
| 99 | + return $key . '="' . htmlspecialchars($value, ENT_QUOTES) . '"'; |
|
| 100 | 100 | }, array_keys($attributes), array_values($attributes))); |
| 101 | 101 | |
| 102 | - if(!empty($this->_children)) { |
|
| 102 | + if (!empty($this->_children)) { |
|
| 103 | 103 | $result .= ">{$this->innerXml}</{$this->name}>"; |
| 104 | 104 | } else { |
| 105 | 105 | $result .= "/>"; |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | public function getInnerXml() |
| 112 | 112 | { |
| 113 | 113 | return implode('', array_map(function($element) { |
| 114 | - if(is_string($element)) { |
|
| 114 | + if (is_string($element)) { |
|
| 115 | 115 | return htmlspecialchars($element); |
| 116 | 116 | } elseif ($element instanceof XmlElement) { |
| 117 | 117 | return $element->xml(false); |
@@ -123,12 +123,12 @@ discard block |
||
| 123 | 123 | |
| 124 | 124 | public function setAttribute(string $attribute, $value, string $uri = null) |
| 125 | 125 | { |
| 126 | - if($uri === 'http://www.w3.org/2000/xmlns/') { |
|
| 126 | + if ($uri === 'http://www.w3.org/2000/xmlns/') { |
|
| 127 | 127 | $this->setNamespace($value, $attribute); |
| 128 | 128 | return; |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | - if($uri !== null) { |
|
| 131 | + if ($uri !== null) { |
|
| 132 | 132 | $attribute = $this->_prefix($attribute, $uri); |
| 133 | 133 | } |
| 134 | 134 | |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | |
| 138 | 138 | public function getAttribute(string $attribute, string $uri = null) |
| 139 | 139 | { |
| 140 | - if($uri !== null) { |
|
| 140 | + if ($uri !== null) { |
|
| 141 | 141 | $attribute = $this->_prefix($attribute, $uri); |
| 142 | 142 | } |
| 143 | 143 | |
@@ -154,11 +154,11 @@ discard block |
||
| 154 | 154 | */ |
| 155 | 155 | public function getNamespaces($parent = true): array |
| 156 | 156 | { |
| 157 | - if(!$this->_parent) { |
|
| 157 | + if (!$this->_parent) { |
|
| 158 | 158 | return $this->_namespaces; |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | - if($parent) { |
|
| 161 | + if ($parent) { |
|
| 162 | 162 | return array_merge($this->_namespaces, $this->_parent->getNamespaces()); |
| 163 | 163 | } else { |
| 164 | 164 | return array_diff_assoc($this->_namespaces, $this->_parent->getNamespaces()); |
@@ -169,7 +169,7 @@ discard block |
||
| 169 | 169 | { |
| 170 | 170 | $namespaces = $this->getNamespaces(false); |
| 171 | 171 | $namespaces = array_map(function($prefix, $uri) { |
| 172 | - return [ $prefix ? "xmlns:{$prefix}" : 'xmlns', $uri ]; |
|
| 172 | + return [$prefix ? "xmlns:{$prefix}" : 'xmlns', $uri]; |
|
| 173 | 173 | }, array_values($namespaces), array_keys($namespaces)); |
| 174 | 174 | |
| 175 | 175 | return array_merge( |
@@ -194,7 +194,7 @@ discard block |
||
| 194 | 194 | */ |
| 195 | 195 | public function setNamespace(string $uri, $prefix = false) |
| 196 | 196 | { |
| 197 | - if($prefix === false) { |
|
| 197 | + if ($prefix === false) { |
|
| 198 | 198 | $prefix = $this->_prefix; |
| 199 | 199 | } |
| 200 | 200 | |
@@ -213,15 +213,15 @@ discard block |
||
| 213 | 213 | |
| 214 | 214 | public function append($element) |
| 215 | 215 | { |
| 216 | - if(!is_string($element) && !$element instanceof XmlElement) { |
|
| 216 | + if (!is_string($element) && !$element instanceof XmlElement) { |
|
| 217 | 217 | throw new InvalidArgumentException(helper\format('$element should be either string or object of {class} class, {type} given', [ |
| 218 | 218 | 'class' => XmlElement::class, |
| 219 | 219 | 'type' => helper\typeof($element) |
| 220 | 220 | ])); |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | - if($element instanceof XmlElement) { |
|
| 224 | - $element->parent = $this; |
|
| 223 | + if ($element instanceof XmlElement) { |
|
| 224 | + $element->parent = $this; |
|
| 225 | 225 | } |
| 226 | 226 | |
| 227 | 227 | return $this->_children[] = $element; |
@@ -229,7 +229,7 @@ discard block |
||
| 229 | 229 | |
| 230 | 230 | public function getName() |
| 231 | 231 | { |
| 232 | - return ($this->_prefix ? $this->prefix.':' : null).$this->localName; |
|
| 232 | + return ($this->_prefix ? $this->prefix . ':' : null) . $this->localName; |
|
| 233 | 233 | } |
| 234 | 234 | |
| 235 | 235 | public function getPrefix() |
@@ -244,17 +244,17 @@ discard block |
||
| 244 | 244 | |
| 245 | 245 | protected function setParent(XmlElement $parent) |
| 246 | 246 | { |
| 247 | - if(!$this->_prefix && ($prefix = $parent->lookupPrefix($this->namespace)) !== false) { |
|
| 247 | + if (!$this->_prefix && ($prefix = $parent->lookupPrefix($this->namespace)) !== false) { |
|
| 248 | 248 | $this->_namespaces[$this->namespace] = $prefix; |
| 249 | 249 | $this->_prefix = $prefix; |
| 250 | 250 | } |
| 251 | 251 | |
| 252 | 252 | $this->_parent = $parent; |
| 253 | - if($this->namespace === false) { |
|
| 253 | + if ($this->namespace === false) { |
|
| 254 | 254 | $this->namespace = $parent->namespace; |
| 255 | 255 | } |
| 256 | 256 | |
| 257 | - if(!$this->_prefix) { |
|
| 257 | + if (!$this->_prefix) { |
|
| 258 | 258 | $this->_prefix = $this->lookupPrefix($this->namespace); |
| 259 | 259 | } |
| 260 | 260 | } |
@@ -270,7 +270,7 @@ discard block |
||
| 270 | 270 | public function elements($name, $uri = null) : array |
| 271 | 271 | { |
| 272 | 272 | $predicate = filter\tag($name); |
| 273 | - if($uri !== null) { |
|
| 273 | + if ($uri !== null) { |
|
| 274 | 274 | $predicate = filter\all($predicate, filter\xmlns($uri)); |
| 275 | 275 | } |
| 276 | 276 | |
@@ -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) |