Deserializer
6 months ago
Element
6 months ago
Serializer
6 months ago
ContextStackTrait.php
6 months ago
Element.php
6 months ago
LibXMLException.php
6 months ago
ParseException.php
6 months ago
Reader.php
6 months ago
Service.php
6 months ago
Version.php
6 months ago
Writer.php
6 months ago
XmlDeserializable.php
6 months ago
XmlSerializable.php
6 months ago
Service.php
327 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AmeliaVendor\Sabre\Xml; |
| 6 | |
| 7 | /** |
| 8 | * XML parsing and writing service. |
| 9 | * |
| 10 | * You are encouraged to make an instance of this for your application and |
| 11 | * potentially extend it, as a central API point for dealing with xml and |
| 12 | * configuring the reader and writer. |
| 13 | * |
| 14 | * @copyright Copyright (C) 2009-2015 fruux GmbH (https://fruux.com/). |
| 15 | * @author Evert Pot (http://evertpot.com/) |
| 16 | * @license http://sabre.io/license/ Modified BSD License |
| 17 | */ |
| 18 | class Service |
| 19 | { |
| 20 | /** |
| 21 | * This is the element map. It contains a list of XML elements (in clark |
| 22 | * notation) as keys and PHP class names as values. |
| 23 | * |
| 24 | * The PHP class names must implement Sabre\Xml\Element. |
| 25 | * |
| 26 | * Values may also be a callable. In that case the function will be called |
| 27 | * directly. |
| 28 | * |
| 29 | * @phpstan-var array<string, class-string|callable|object> |
| 30 | */ |
| 31 | public array $elementMap = []; |
| 32 | |
| 33 | /** |
| 34 | * This is a list of namespaces that you want to give default prefixes. |
| 35 | * |
| 36 | * You must make sure you create this entire list before starting to write. |
| 37 | * They should be registered on the root element. |
| 38 | * |
| 39 | * @phpstan-var array<string, class-string|string|null> |
| 40 | */ |
| 41 | public array $namespaceMap = []; |
| 42 | |
| 43 | /** |
| 44 | * This is a list of custom serializers for specific classes. |
| 45 | * |
| 46 | * The writer may use this if you attempt to serialize an object with a |
| 47 | * class that does not implement XmlSerializable. |
| 48 | * |
| 49 | * Instead, it will look at this classmap to see if there is a custom |
| 50 | * serializer here. This is useful if you don't want your value objects |
| 51 | * to be responsible for serializing themselves. |
| 52 | * |
| 53 | * The keys in this classmap need to be fully qualified PHP class names, |
| 54 | * the values must be callbacks. The callbacks take two arguments. The |
| 55 | * writer class, and the value that must be written. |
| 56 | * |
| 57 | * function (Writer $writer, object $value) |
| 58 | * |
| 59 | * @phpstan-var array<class-string, callable(Writer, object):mixed> |
| 60 | */ |
| 61 | public array $classMap = []; |
| 62 | |
| 63 | /** |
| 64 | * A bitmask of the LIBXML_* constants. |
| 65 | */ |
| 66 | public int $options = 0; |
| 67 | |
| 68 | /** |
| 69 | * Returns a fresh XML Reader. |
| 70 | */ |
| 71 | public function getReader(): Reader |
| 72 | { |
| 73 | $r = new Reader(); |
| 74 | $r->elementMap = $this->elementMap; |
| 75 | |
| 76 | return $r; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns a fresh xml writer. |
| 81 | */ |
| 82 | public function getWriter(): Writer |
| 83 | { |
| 84 | $w = new Writer(); |
| 85 | $w->namespaceMap = $this->namespaceMap; |
| 86 | $w->classMap = $this->classMap; |
| 87 | |
| 88 | return $w; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Parses a document in full. |
| 93 | * |
| 94 | * Input may be specified as a string or readable stream resource. |
| 95 | * The returned value is the value of the root document. |
| 96 | * |
| 97 | * Specifying the $contextUri allows the parser to figure out what the URI |
| 98 | * of the document was. This allows relative URIs within the document to be |
| 99 | * expanded easily. |
| 100 | * |
| 101 | * The $rootElementName is specified by reference and will be populated |
| 102 | * with the root element name of the document. |
| 103 | * |
| 104 | * @param string|resource $input |
| 105 | * |
| 106 | * @return array<string, mixed>|object|string |
| 107 | * |
| 108 | * @throws ParseException |
| 109 | */ |
| 110 | public function parse($input, ?string $contextUri = null, ?string &$rootElementName = null) |
| 111 | { |
| 112 | if (!is_string($input)) { |
| 113 | // Unfortunately the XMLReader doesn't support streams. When it |
| 114 | // does, we can optimize this. |
| 115 | if (is_resource($input)) { |
| 116 | $input = (string) stream_get_contents($input); |
| 117 | } else { |
| 118 | // Input is not a string and not a resource. |
| 119 | // Therefore, it has to be a closed resource. |
| 120 | // Effectively empty input has been passed in. |
| 121 | $input = ''; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // If input is empty, then it's safe to throw an exception |
| 126 | if (empty($input)) { |
| 127 | throw new ParseException('The input element to parse is empty. Do not attempt to parse'); |
| 128 | } |
| 129 | |
| 130 | $r = $this->getReader(); |
| 131 | $r->contextUri = $contextUri; |
| 132 | $r->XML($input, null, $this->options); |
| 133 | |
| 134 | $result = $r->parse(); |
| 135 | $rootElementName = $result['name']; |
| 136 | |
| 137 | return $result['value']; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Parses a document in full, and specify what the expected root element |
| 142 | * name is. |
| 143 | * |
| 144 | * This function works similar to parse, but the difference is that the |
| 145 | * user can specify what the expected name of the root element should be, |
| 146 | * in clark notation. |
| 147 | * |
| 148 | * This is useful in cases where you expected a specific document to be |
| 149 | * passed, and reduces the amount of if statements. |
| 150 | * |
| 151 | * It's also possible to pass an array of expected rootElements if your |
| 152 | * code may expect more than one document type. |
| 153 | * |
| 154 | * @param string|string[] $rootElementName |
| 155 | * @param string|resource $input |
| 156 | * |
| 157 | * @return array<string, mixed>|object|string |
| 158 | * |
| 159 | * @throws ParseException |
| 160 | */ |
| 161 | public function expect($rootElementName, $input, ?string $contextUri = null) |
| 162 | { |
| 163 | if (!is_string($input)) { |
| 164 | // Unfortunately the XMLReader doesn't support streams. When it |
| 165 | // does, we can optimize this. |
| 166 | if (is_resource($input)) { |
| 167 | $input = (string) stream_get_contents($input); |
| 168 | } else { |
| 169 | // Input is not a string and not a resource. |
| 170 | // Therefore, it has to be a closed resource. |
| 171 | // Effectively empty input has been passed in. |
| 172 | $input = ''; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // If input is empty, then it's safe to throw an exception |
| 177 | if (empty($input)) { |
| 178 | throw new ParseException('The input element to parse is empty. Do not attempt to parse'); |
| 179 | } |
| 180 | |
| 181 | $r = $this->getReader(); |
| 182 | $r->contextUri = $contextUri; |
| 183 | $r->XML($input, null, $this->options); |
| 184 | |
| 185 | $rootElementName = (array) $rootElementName; |
| 186 | |
| 187 | foreach ($rootElementName as &$rEl) { |
| 188 | if ('{' !== $rEl[0]) { |
| 189 | $rEl = '{}'.$rEl; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | $result = $r->parse(); |
| 194 | if (!in_array($result['name'], $rootElementName, true)) { |
| 195 | throw new ParseException('Expected '.implode(' or ', $rootElementName).' but received '.$result['name'].' as the root element'); |
| 196 | } |
| 197 | |
| 198 | return $result['value']; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Generates an XML document in one go. |
| 203 | * |
| 204 | * The $rootElement must be specified in clark notation. |
| 205 | * The value must be a string, an array or an object implementing |
| 206 | * XmlSerializable. Basically, anything that's supported by the Writer |
| 207 | * object. |
| 208 | * |
| 209 | * $contextUri can be used to specify a sort of 'root' of the PHP application, |
| 210 | * in case the xml document is used as a http response. |
| 211 | * |
| 212 | * This allows an implementor to easily create URI's relative to the root |
| 213 | * of the domain. |
| 214 | * |
| 215 | * @param string|array<int|string, mixed>|object|XmlSerializable $value |
| 216 | */ |
| 217 | public function write(string $rootElementName, $value, ?string $contextUri = null): string |
| 218 | { |
| 219 | $w = $this->getWriter(); |
| 220 | $w->openMemory(); |
| 221 | $w->contextUri = $contextUri; |
| 222 | $w->setIndent(true); |
| 223 | $w->startDocument(); |
| 224 | $w->writeElement($rootElementName, $value); |
| 225 | |
| 226 | return $w->outputMemory(); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Map an XML element to a PHP class. |
| 231 | * |
| 232 | * Calling this function will automatically set up the Reader and Writer |
| 233 | * classes to turn a specific XML element to a PHP class. |
| 234 | * |
| 235 | * For example, given a class such as : |
| 236 | * |
| 237 | * class Author { |
| 238 | * public $firstName; |
| 239 | * public $lastName; |
| 240 | * } |
| 241 | * |
| 242 | * and an XML element such as: |
| 243 | * |
| 244 | * <author xmlns="http://example.org/ns"> |
| 245 | * <firstName>...</firstName> |
| 246 | * <lastName>...</lastName> |
| 247 | * </author> |
| 248 | * |
| 249 | * These can easily be mapped by calling: |
| 250 | * |
| 251 | * $service->mapValueObject('{http://example.org}author', 'Author'); |
| 252 | * |
| 253 | * @param class-string $className |
| 254 | */ |
| 255 | public function mapValueObject(string $elementName, string $className): void |
| 256 | { |
| 257 | list($namespace) = self::parseClarkNotation($elementName); |
| 258 | |
| 259 | $this->elementMap[$elementName] = function (Reader $reader) use ($className, $namespace) { |
| 260 | return \AmeliaVendor\Sabre\Xml\Deserializer\valueObject($reader, $className, $namespace); |
| 261 | }; |
| 262 | $this->classMap[$className] = function (Writer $writer, $valueObject) use ($namespace) { |
| 263 | \AmeliaVendor\Sabre\Xml\Serializer\valueObject($writer, $valueObject, $namespace); |
| 264 | }; |
| 265 | $this->valueObjectMap[$className] = $elementName; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Writes a value object. |
| 270 | * |
| 271 | * This function largely behaves similar to write(), except that it's |
| 272 | * intended specifically to serialize a Value Object into an XML document. |
| 273 | * |
| 274 | * The ValueObject must have been previously registered using |
| 275 | * mapValueObject(). |
| 276 | * |
| 277 | * @throws \InvalidArgumentException |
| 278 | */ |
| 279 | public function writeValueObject(object $object, ?string $contextUri = null): string |
| 280 | { |
| 281 | if (!isset($this->valueObjectMap[get_class($object)])) { |
| 282 | throw new \InvalidArgumentException('"'.get_class($object).'" is not a registered value object class. Register your class with mapValueObject.'); |
| 283 | } |
| 284 | |
| 285 | return $this->write( |
| 286 | $this->valueObjectMap[get_class($object)], |
| 287 | $object, |
| 288 | $contextUri |
| 289 | ); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Parses a clark-notation string, and returns the namespace and element |
| 294 | * name components. |
| 295 | * |
| 296 | * If the string was invalid, it will throw an InvalidArgumentException. |
| 297 | * |
| 298 | * @return array{string, string} |
| 299 | * |
| 300 | * @throws \InvalidArgumentException |
| 301 | */ |
| 302 | public static function parseClarkNotation(string $str): array |
| 303 | { |
| 304 | static $cache = []; |
| 305 | |
| 306 | if (!isset($cache[$str])) { |
| 307 | if (!preg_match('/^{([^}]*)}(.*)$/', $str, $matches)) { |
| 308 | throw new \InvalidArgumentException('\''.$str.'\' is not a valid clark-notation formatted string'); |
| 309 | } |
| 310 | |
| 311 | $cache[$str] = [ |
| 312 | $matches[1], |
| 313 | $matches[2], |
| 314 | ]; |
| 315 | } |
| 316 | |
| 317 | return $cache[$str]; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * A list of classes and which XML elements they map to. |
| 322 | * |
| 323 | * @var array<class-string, string> |
| 324 | */ |
| 325 | protected array $valueObjectMap = []; |
| 326 | } |
| 327 |