diff options
Diffstat (limited to 'vendor/sabre/xml/lib/Service.php')
-rw-r--r-- | vendor/sabre/xml/lib/Service.php | 110 |
1 files changed, 58 insertions, 52 deletions
diff --git a/vendor/sabre/xml/lib/Service.php b/vendor/sabre/xml/lib/Service.php index acea94ea9..882b2dc2c 100644 --- a/vendor/sabre/xml/lib/Service.php +++ b/vendor/sabre/xml/lib/Service.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + namespace Sabre\Xml; /** @@ -13,8 +15,8 @@ namespace Sabre\Xml; * @author Evert Pot (http://evertpot.com/) * @license http://sabre.io/license/ Modified BSD License */ -class Service { - +class Service +{ /** * This is the element map. It contains a list of XML elements (in clark * notation) as keys and PHP class names as values. @@ -59,30 +61,26 @@ class Service { public $classMap = []; /** - * Returns a fresh XML Reader - * - * @return Reader + * Returns a fresh XML Reader. */ - function getReader() { - + public function getReader(): Reader + { $r = new Reader(); $r->elementMap = $this->elementMap; - return $r; + return $r; } /** - * Returns a fresh xml writer - * - * @return Writer + * Returns a fresh xml writer. */ - function getWriter() { - + public function getWriter(): Writer + { $w = new Writer(); $w->namespaceMap = $this->namespaceMap; $w->classMap = $this->classMap; - return $w; + return $w; } /** @@ -99,17 +97,22 @@ class Service { * with the root element name of the document. * * @param string|resource $input - * @param string|null $contextUri - * @param string|null $rootElementName + * * @throws ParseException + * * @return array|object|string */ - function parse($input, $contextUri = null, &$rootElementName = null) { - + public function parse($input, string $contextUri = null, string &$rootElementName = null) + { if (is_resource($input)) { // Unfortunately the XMLReader doesn't support streams. When it // does, we can optimize this. $input = stream_get_contents($input); + + // If input is an empty string, then its safe to throw exception + if ('' === $input) { + throw new ParseException('The input element to parse is empty. Do not attempt to parse'); + } } $r = $this->getReader(); $r->contextUri = $contextUri; @@ -117,8 +120,8 @@ class Service { $result = $r->parse(); $rootElementName = $result['name']; - return $result['value']; + return $result['value']; } /** @@ -137,33 +140,42 @@ class Service { * * @param string|string[] $rootElementName * @param string|resource $input - * @param string|null $contextUri + * @param string|null $contextUri + * * @throws ParseException + * * @return array|object|string */ - function expect($rootElementName, $input, $contextUri = null) { - + public function expect($rootElementName, $input, string $contextUri = null) + { if (is_resource($input)) { // Unfortunately the XMLReader doesn't support streams. When it // does, we can optimize this. $input = stream_get_contents($input); + + // If input is empty string, then its safe to throw exception + if ('' === $input) { + throw new ParseException('The input element to parse is empty. Do not attempt to parse'); + } } $r = $this->getReader(); $r->contextUri = $contextUri; $r->xml($input); - $rootElementName = (array)$rootElementName; + $rootElementName = (array) $rootElementName; foreach ($rootElementName as &$rEl) { - if ($rEl[0] !== '{') $rEl = '{}' . $rEl; + if ('{' !== $rEl[0]) { + $rEl = '{}'.$rEl; + } } $result = $r->parse(); if (!in_array($result['name'], $rootElementName, true)) { - throw new ParseException('Expected ' . implode(' or ', (array)$rootElementName) . ' but received ' . $result['name'] . ' as the root element'); + throw new ParseException('Expected '.implode(' or ', (array) $rootElementName).' but received '.$result['name'].' as the root element'); } - return $result['value']; + return $result['value']; } /** @@ -180,20 +192,20 @@ class Service { * This allows an implementor to easily create URI's relative to the root * of the domain. * - * @param string $rootElementName * @param string|array|XmlSerializable $value - * @param string|null $contextUri + * + * @return string */ - function write($rootElementName, $value, $contextUri = null) { - + public function write(string $rootElementName, $value, string $contextUri = null) + { $w = $this->getWriter(); $w->openMemory(); $w->contextUri = $contextUri; $w->setIndent(true); $w->startDocument(); $w->writeElement($rootElementName, $value); - return $w->outputMemory(); + return $w->outputMemory(); } /** @@ -219,18 +231,15 @@ class Service { * These can easily be mapped by calling: * * $service->mapValueObject('{http://example.org}author', 'Author'); - * - * @param string $elementName - * @param object $className - * @return void */ - function mapValueObject($elementName, $className) { + public function mapValueObject(string $elementName, string $className) + { list($namespace) = self::parseClarkNotation($elementName); - $this->elementMap[$elementName] = function(Reader $reader) use ($className, $namespace) { + $this->elementMap[$elementName] = function (Reader $reader) use ($className, $namespace) { return \Sabre\Xml\Deserializer\valueObject($reader, $className, $namespace); }; - $this->classMap[$className] = function(Writer $writer, $valueObject) use ($namespace) { + $this->classMap[$className] = function (Writer $writer, $valueObject) use ($namespace) { return \Sabre\Xml\Serializer\valueObject($writer, $valueObject, $namespace); }; $this->valueObjectMap[$className] = $elementName; @@ -246,20 +255,20 @@ class Service { * mapValueObject(). * * @param object $object - * @param string $contextUri - * @return void + * + * @throws \InvalidArgumentException */ - function writeValueObject($object, $contextUri = null) { - + public function writeValueObject($object, string $contextUri = null) + { if (!isset($this->valueObjectMap[get_class($object)])) { - throw new \InvalidArgumentException('"' . get_class($object) . '" is not a registered value object class. Register your class with mapValueObject.'); + throw new \InvalidArgumentException('"'.get_class($object).'" is not a registered value object class. Register your class with mapValueObject.'); } + return $this->write( $this->valueObjectMap[get_class($object)], $object, $contextUri ); - } /** @@ -268,22 +277,20 @@ class Service { * * If the string was invalid, it will throw an InvalidArgumentException. * - * @param string $str - * @throws InvalidArgumentException - * @return array + * @throws \InvalidArgumentException */ - static function parseClarkNotation($str) { + public static function parseClarkNotation(string $str): array + { static $cache = []; if (!isset($cache[$str])) { - if (!preg_match('/^{([^}]*)}(.*)$/', $str, $matches)) { - throw new \InvalidArgumentException('\'' . $str . '\' is not a valid clark-notation formatted string'); + throw new \InvalidArgumentException('\''.$str.'\' is not a valid clark-notation formatted string'); } $cache[$str] = [ $matches[1], - $matches[2] + $matches[2], ]; } @@ -294,5 +301,4 @@ class Service { * A list of classes and which XML elements they map to. */ protected $valueObjectMap = []; - } |