diff options
Diffstat (limited to 'vendor/sabre/xml/lib/Deserializer/functions.php')
-rw-r--r-- | vendor/sabre/xml/lib/Deserializer/functions.php | 117 |
1 files changed, 76 insertions, 41 deletions
diff --git a/vendor/sabre/xml/lib/Deserializer/functions.php b/vendor/sabre/xml/lib/Deserializer/functions.php index 07038d99a..0eff4b7e0 100644 --- a/vendor/sabre/xml/lib/Deserializer/functions.php +++ b/vendor/sabre/xml/lib/Deserializer/functions.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + namespace Sabre\Xml\Deserializer; use Sabre\Xml\Reader; @@ -52,17 +54,13 @@ use Sabre\Xml\Reader; * * Attributes will be removed from the top-level elements. If elements with * the same name appear twice in the list, only the last one will be kept. - * - * - * @param Reader $reader - * @param string $namespace - * @return array */ -function keyValue(Reader $reader, $namespace = null) { - +function keyValue(Reader $reader, string $namespace = null): array +{ // If there's no children, we don't do anything. if ($reader->isEmptyElement) { $reader->next(); + return []; } @@ -81,9 +79,8 @@ function keyValue(Reader $reader, $namespace = null) { $values = []; do { - - if ($reader->nodeType === Reader::ELEMENT) { - if ($namespace !== null && $reader->namespaceURI === $namespace) { + if (Reader::ELEMENT === $reader->nodeType) { + if (null !== $namespace && $reader->namespaceURI === $namespace) { $values[$reader->localName] = $reader->parseCurrentElement()['value']; } else { $clark = $reader->getClark(); @@ -94,12 +91,11 @@ function keyValue(Reader $reader, $namespace = null) { break; } } - } while ($reader->nodeType !== Reader::END_ELEMENT); + } while (Reader::END_ELEMENT !== $reader->nodeType); $reader->read(); return $values; - } /** @@ -146,15 +142,14 @@ function keyValue(Reader $reader, $namespace = null) { * "elem5", * ]; * - * @param Reader $reader - * @param string $namespace * @return string[] */ -function enum(Reader $reader, $namespace = null) { - +function enum(Reader $reader, string $namespace = null): array +{ // If there's no children, we don't do anything. if ($reader->isEmptyElement) { $reader->next(); + return []; } if (!$reader->read()) { @@ -172,8 +167,7 @@ function enum(Reader $reader, $namespace = null) { $values = []; do { - - if ($reader->nodeType !== Reader::ELEMENT) { + if (Reader::ELEMENT !== $reader->nodeType) { continue; } if (!is_null($namespace) && $namespace === $reader->namespaceURI) { @@ -181,12 +175,11 @@ function enum(Reader $reader, $namespace = null) { } else { $values[] = $reader->getClark(); } - } while ($reader->depth >= $currentDepth && $reader->next()); $reader->next(); - return $values; + return $values; } /** @@ -196,16 +189,14 @@ function enum(Reader $reader, $namespace = null) { * This is primarily used by the mapValueObject function from the Service * class, but it can also easily be used for more specific situations. * - * @param Reader $reader - * @param string $className - * @param string $namespace * @return object */ -function valueObject(Reader $reader, $className, $namespace) { - +function valueObject(Reader $reader, string $className, string $namespace) +{ $valueObject = new $className(); if ($reader->isEmptyElement) { $reader->next(); + return $valueObject; } @@ -213,9 +204,7 @@ function valueObject(Reader $reader, $className, $namespace) { $reader->read(); do { - - if ($reader->nodeType === Reader::ELEMENT && $reader->namespaceURI == $namespace) { - + if (Reader::ELEMENT === $reader->nodeType && $reader->namespaceURI == $namespace) { if (property_exists($valueObject, $reader->localName)) { if (is_array($defaultProperties[$reader->localName])) { $valueObject->{$reader->localName}[] = $reader->parseCurrentElement()['value']; @@ -231,16 +220,16 @@ function valueObject(Reader $reader, $className, $namespace) { break; } } - } while ($reader->nodeType !== Reader::END_ELEMENT); + } while (Reader::END_ELEMENT !== $reader->nodeType); $reader->read(); - return $valueObject; + return $valueObject; } /** * This deserializer helps you deserialize xml structures that look like - * this: + * this:. * * <collection> * <item>...</item> @@ -248,7 +237,7 @@ function valueObject(Reader $reader, $className, $namespace) { * <item>...</item> * </collection> * - * Many XML documents use patterns like that, and this deserializer + * Many XML documents use patterns like that, and this deserializer * allow you to get all the 'items' as an array. * * In that previous example, you would register the deserializer as such: @@ -259,25 +248,71 @@ function valueObject(Reader $reader, $className, $namespace) { * * The repeatingElements deserializer simply returns everything as an array. * - * @param Reader $reader - * @param string $childElementName Element name in clark-notation - * @return array + * $childElementName must either be a a clark-notation element name, or if no + * namespace is used, the bare element name. */ -function repeatingElements(Reader $reader, $childElementName) { - - if ($childElementName[0] !== '{') { - $childElementName = '{}' . $childElementName; +function repeatingElements(Reader $reader, string $childElementName): array +{ + if ('{' !== $childElementName[0]) { + $childElementName = '{}'.$childElementName; } $result = []; foreach ($reader->parseGetElements() as $element) { - if ($element['name'] === $childElementName) { $result[] = $element['value']; } - } return $result; +} + +/** + * This deserializer helps you to deserialize structures which contain mixed content like this:. + * + * <p>some text <extref>and a inline tag</extref>and even more text</p> + * + * The above example will return + * + * [ + * 'some text', + * [ + * 'name' => '{}extref', + * 'value' => 'and a inline tag', + * 'attributes' => [] + * ], + * 'and even more text' + * ] + * + * In strict XML documents you wont find this kind of markup but in html this is a quite common pattern. + */ +function mixedContent(Reader $reader): array +{ + // If there's no children, we don't do anything. + if ($reader->isEmptyElement) { + $reader->next(); + + return []; + } + + $previousDepth = $reader->depth; + + $content = []; + $reader->read(); + while (true) { + if (Reader::ELEMENT == $reader->nodeType) { + $content[] = $reader->parseCurrentElement(); + } elseif ($reader->depth >= $previousDepth && in_array($reader->nodeType, [Reader::TEXT, Reader::CDATA, Reader::WHITESPACE])) { + $content[] = $reader->value; + $reader->read(); + } elseif (Reader::END_ELEMENT == $reader->nodeType) { + // Ensuring we are moving the cursor after the end element. + $reader->read(); + break; + } else { + $reader->read(); + } + } + return $content; } |