XmlEncoder.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Serializer\Encoder;
  11. use Symfony\Component\Serializer\Exception\BadMethodCallException;
  12. use Symfony\Component\Serializer\Exception\NotEncodableValueException;
  13. use Symfony\Component\Serializer\SerializerAwareInterface;
  14. use Symfony\Component\Serializer\SerializerAwareTrait;
  15. /**
  16. * Encodes XML data.
  17. *
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. * @author John Wards <jwards@whiteoctober.co.uk>
  20. * @author Fabian Vogler <fabian@equivalence.ch>
  21. * @author Kévin Dunglas <dunglas@gmail.com>
  22. * @author Dany Maillard <danymaillard93b@gmail.com>
  23. */
  24. class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwareInterface, SerializerAwareInterface
  25. {
  26. use SerializerAwareTrait;
  27. public const FORMAT = 'xml';
  28. public const AS_COLLECTION = 'as_collection';
  29. /**
  30. * An array of ignored XML node types while decoding, each one of the DOM Predefined XML_* constants.
  31. */
  32. public const DECODER_IGNORED_NODE_TYPES = 'decoder_ignored_node_types';
  33. /**
  34. * An array of ignored XML node types while encoding, each one of the DOM Predefined XML_* constants.
  35. */
  36. public const ENCODER_IGNORED_NODE_TYPES = 'encoder_ignored_node_types';
  37. public const ENCODING = 'xml_encoding';
  38. public const FORMAT_OUTPUT = 'xml_format_output';
  39. /**
  40. * A bit field of LIBXML_* constants.
  41. */
  42. public const LOAD_OPTIONS = 'load_options';
  43. public const REMOVE_EMPTY_TAGS = 'remove_empty_tags';
  44. public const ROOT_NODE_NAME = 'xml_root_node_name';
  45. public const STANDALONE = 'xml_standalone';
  46. public const TYPE_CAST_ATTRIBUTES = 'xml_type_cast_attributes';
  47. public const VERSION = 'xml_version';
  48. private $defaultContext = [
  49. self::AS_COLLECTION => false,
  50. self::DECODER_IGNORED_NODE_TYPES => [\XML_PI_NODE, \XML_COMMENT_NODE],
  51. self::ENCODER_IGNORED_NODE_TYPES => [],
  52. self::LOAD_OPTIONS => \LIBXML_NONET | \LIBXML_NOBLANKS,
  53. self::REMOVE_EMPTY_TAGS => false,
  54. self::ROOT_NODE_NAME => 'response',
  55. self::TYPE_CAST_ATTRIBUTES => true,
  56. ];
  57. /**
  58. * @var \DOMDocument
  59. */
  60. private $dom;
  61. private $format;
  62. private $context;
  63. public function __construct(array $defaultContext = [])
  64. {
  65. $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function encode($data, string $format, array $context = [])
  71. {
  72. $encoderIgnoredNodeTypes = $context[self::ENCODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::ENCODER_IGNORED_NODE_TYPES];
  73. $ignorePiNode = \in_array(\XML_PI_NODE, $encoderIgnoredNodeTypes, true);
  74. if ($data instanceof \DOMDocument) {
  75. return $data->saveXML($ignorePiNode ? $data->documentElement : null);
  76. }
  77. $xmlRootNodeName = $context[self::ROOT_NODE_NAME] ?? $this->defaultContext[self::ROOT_NODE_NAME];
  78. $this->dom = $this->createDomDocument($context);
  79. $this->format = $format;
  80. $this->context = $context;
  81. if (null !== $data && !is_scalar($data)) {
  82. $root = $this->dom->createElement($xmlRootNodeName);
  83. $this->dom->appendChild($root);
  84. $this->buildXml($root, $data, $xmlRootNodeName);
  85. } else {
  86. $this->appendNode($this->dom, $data, $xmlRootNodeName);
  87. }
  88. return $this->dom->saveXML($ignorePiNode ? $this->dom->documentElement : null);
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function decode(string $data, string $format, array $context = [])
  94. {
  95. if ('' === trim($data)) {
  96. throw new NotEncodableValueException('Invalid XML data, it can not be empty.');
  97. }
  98. $internalErrors = libxml_use_internal_errors(true);
  99. if (\LIBXML_VERSION < 20900) {
  100. $disableEntities = libxml_disable_entity_loader(true);
  101. }
  102. libxml_clear_errors();
  103. $dom = new \DOMDocument();
  104. $dom->loadXML($data, $context[self::LOAD_OPTIONS] ?? $this->defaultContext[self::LOAD_OPTIONS]);
  105. libxml_use_internal_errors($internalErrors);
  106. if (\LIBXML_VERSION < 20900) {
  107. libxml_disable_entity_loader($disableEntities);
  108. }
  109. if ($error = libxml_get_last_error()) {
  110. libxml_clear_errors();
  111. throw new NotEncodableValueException($error->message);
  112. }
  113. $rootNode = null;
  114. $decoderIgnoredNodeTypes = $context[self::DECODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::DECODER_IGNORED_NODE_TYPES];
  115. foreach ($dom->childNodes as $child) {
  116. if (\XML_DOCUMENT_TYPE_NODE === $child->nodeType) {
  117. throw new NotEncodableValueException('Document types are not allowed.');
  118. }
  119. if (!$rootNode && !\in_array($child->nodeType, $decoderIgnoredNodeTypes, true)) {
  120. $rootNode = $child;
  121. }
  122. }
  123. // todo: throw an exception if the root node name is not correctly configured (bc)
  124. if ($rootNode->hasChildNodes()) {
  125. $xpath = new \DOMXPath($dom);
  126. $data = [];
  127. foreach ($xpath->query('namespace::*', $dom->documentElement) as $nsNode) {
  128. $data['@'.$nsNode->nodeName] = $nsNode->nodeValue;
  129. }
  130. unset($data['@xmlns:xml']);
  131. if (empty($data)) {
  132. return $this->parseXml($rootNode, $context);
  133. }
  134. return array_merge($data, (array) $this->parseXml($rootNode, $context));
  135. }
  136. if (!$rootNode->hasAttributes()) {
  137. return $rootNode->nodeValue;
  138. }
  139. $data = [];
  140. foreach ($rootNode->attributes as $attrKey => $attr) {
  141. $data['@'.$attrKey] = $attr->nodeValue;
  142. }
  143. $data['#'] = $rootNode->nodeValue;
  144. return $data;
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function supportsEncoding(string $format)
  150. {
  151. return self::FORMAT === $format;
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function supportsDecoding(string $format)
  157. {
  158. return self::FORMAT === $format;
  159. }
  160. final protected function appendXMLString(\DOMNode $node, string $val): bool
  161. {
  162. if ('' !== $val) {
  163. $frag = $this->dom->createDocumentFragment();
  164. $frag->appendXML($val);
  165. $node->appendChild($frag);
  166. return true;
  167. }
  168. return false;
  169. }
  170. final protected function appendText(\DOMNode $node, string $val): bool
  171. {
  172. $nodeText = $this->dom->createTextNode($val);
  173. $node->appendChild($nodeText);
  174. return true;
  175. }
  176. final protected function appendCData(\DOMNode $node, string $val): bool
  177. {
  178. $nodeText = $this->dom->createCDATASection($val);
  179. $node->appendChild($nodeText);
  180. return true;
  181. }
  182. /**
  183. * @param \DOMDocumentFragment $fragment
  184. */
  185. final protected function appendDocumentFragment(\DOMNode $node, $fragment): bool
  186. {
  187. if ($fragment instanceof \DOMDocumentFragment) {
  188. $node->appendChild($fragment);
  189. return true;
  190. }
  191. return false;
  192. }
  193. final protected function appendComment(\DOMNode $node, string $data): bool
  194. {
  195. $node->appendChild($this->dom->createComment($data));
  196. return true;
  197. }
  198. /**
  199. * Checks the name is a valid xml element name.
  200. */
  201. final protected function isElementNameValid(string $name): bool
  202. {
  203. return $name &&
  204. false === strpos($name, ' ') &&
  205. preg_match('#^[\pL_][\pL0-9._:-]*$#ui', $name);
  206. }
  207. /**
  208. * Parse the input DOMNode into an array or a string.
  209. *
  210. * @return array|string
  211. */
  212. private function parseXml(\DOMNode $node, array $context = [])
  213. {
  214. $data = $this->parseXmlAttributes($node, $context);
  215. $value = $this->parseXmlValue($node, $context);
  216. if (!\count($data)) {
  217. return $value;
  218. }
  219. if (!\is_array($value)) {
  220. $data['#'] = $value;
  221. return $data;
  222. }
  223. if (1 === \count($value) && key($value)) {
  224. $data[key($value)] = current($value);
  225. return $data;
  226. }
  227. foreach ($value as $key => $val) {
  228. $data[$key] = $val;
  229. }
  230. return $data;
  231. }
  232. /**
  233. * Parse the input DOMNode attributes into an array.
  234. */
  235. private function parseXmlAttributes(\DOMNode $node, array $context = []): array
  236. {
  237. if (!$node->hasAttributes()) {
  238. return [];
  239. }
  240. $data = [];
  241. $typeCastAttributes = (bool) ($context[self::TYPE_CAST_ATTRIBUTES] ?? $this->defaultContext[self::TYPE_CAST_ATTRIBUTES]);
  242. foreach ($node->attributes as $attr) {
  243. if (!is_numeric($attr->nodeValue) || !$typeCastAttributes || (isset($attr->nodeValue[1]) && '0' === $attr->nodeValue[0] && '.' !== $attr->nodeValue[1])) {
  244. $data['@'.$attr->nodeName] = $attr->nodeValue;
  245. continue;
  246. }
  247. if (false !== $val = filter_var($attr->nodeValue, \FILTER_VALIDATE_INT)) {
  248. $data['@'.$attr->nodeName] = $val;
  249. continue;
  250. }
  251. $data['@'.$attr->nodeName] = (float) $attr->nodeValue;
  252. }
  253. return $data;
  254. }
  255. /**
  256. * Parse the input DOMNode value (content and children) into an array or a string.
  257. *
  258. * @return array|string
  259. */
  260. private function parseXmlValue(\DOMNode $node, array $context = [])
  261. {
  262. if (!$node->hasChildNodes()) {
  263. return $node->nodeValue;
  264. }
  265. if (1 === $node->childNodes->length && \in_array($node->firstChild->nodeType, [\XML_TEXT_NODE, \XML_CDATA_SECTION_NODE])) {
  266. return $node->firstChild->nodeValue;
  267. }
  268. $value = [];
  269. $decoderIgnoredNodeTypes = $context[self::DECODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::DECODER_IGNORED_NODE_TYPES];
  270. foreach ($node->childNodes as $subnode) {
  271. if (\in_array($subnode->nodeType, $decoderIgnoredNodeTypes, true)) {
  272. continue;
  273. }
  274. $val = $this->parseXml($subnode, $context);
  275. if ('item' === $subnode->nodeName && isset($val['@key'])) {
  276. $value[$val['@key']] = $val['#'] ?? $val;
  277. } else {
  278. $value[$subnode->nodeName][] = $val;
  279. }
  280. }
  281. $asCollection = $context[self::AS_COLLECTION] ?? $this->defaultContext[self::AS_COLLECTION];
  282. foreach ($value as $key => $val) {
  283. if (!$asCollection && \is_array($val) && 1 === \count($val)) {
  284. $value[$key] = current($val);
  285. }
  286. }
  287. return $value;
  288. }
  289. /**
  290. * Parse the data and convert it to DOMElements.
  291. *
  292. * @param array|object $data
  293. *
  294. * @throws NotEncodableValueException
  295. */
  296. private function buildXml(\DOMNode $parentNode, $data, string $xmlRootNodeName = null): bool
  297. {
  298. $append = true;
  299. $removeEmptyTags = $this->context[self::REMOVE_EMPTY_TAGS] ?? $this->defaultContext[self::REMOVE_EMPTY_TAGS] ?? false;
  300. $encoderIgnoredNodeTypes = $this->context[self::ENCODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::ENCODER_IGNORED_NODE_TYPES];
  301. if (\is_array($data) || ($data instanceof \Traversable && (null === $this->serializer || !$this->serializer->supportsNormalization($data, $this->format)))) {
  302. foreach ($data as $key => $data) {
  303. //Ah this is the magic @ attribute types.
  304. if (0 === strpos($key, '@') && $this->isElementNameValid($attributeName = substr($key, 1))) {
  305. if (!is_scalar($data)) {
  306. $data = $this->serializer->normalize($data, $this->format, $this->context);
  307. }
  308. $parentNode->setAttribute($attributeName, $data);
  309. } elseif ('#' === $key) {
  310. $append = $this->selectNodeType($parentNode, $data);
  311. } elseif ('#comment' === $key) {
  312. if (!\in_array(\XML_COMMENT_NODE, $encoderIgnoredNodeTypes, true)) {
  313. $append = $this->appendComment($parentNode, $data);
  314. }
  315. } elseif (\is_array($data) && false === is_numeric($key)) {
  316. // Is this array fully numeric keys?
  317. if (ctype_digit(implode('', array_keys($data)))) {
  318. /*
  319. * Create nodes to append to $parentNode based on the $key of this array
  320. * Produces <xml><item>0</item><item>1</item></xml>
  321. * From ["item" => [0,1]];.
  322. */
  323. foreach ($data as $subData) {
  324. $append = $this->appendNode($parentNode, $subData, $key);
  325. }
  326. } else {
  327. $append = $this->appendNode($parentNode, $data, $key);
  328. }
  329. } elseif (is_numeric($key) || !$this->isElementNameValid($key)) {
  330. $append = $this->appendNode($parentNode, $data, 'item', $key);
  331. } elseif (null !== $data || !$removeEmptyTags) {
  332. $append = $this->appendNode($parentNode, $data, $key);
  333. }
  334. }
  335. return $append;
  336. }
  337. if (\is_object($data)) {
  338. if (null === $this->serializer) {
  339. throw new BadMethodCallException(sprintf('The serializer needs to be set to allow "%s()" to be used with object data.', __METHOD__));
  340. }
  341. $data = $this->serializer->normalize($data, $this->format, $this->context);
  342. if (null !== $data && !is_scalar($data)) {
  343. return $this->buildXml($parentNode, $data, $xmlRootNodeName);
  344. }
  345. // top level data object was normalized into a scalar
  346. if (!$parentNode->parentNode->parentNode) {
  347. $root = $parentNode->parentNode;
  348. $root->removeChild($parentNode);
  349. return $this->appendNode($root, $data, $xmlRootNodeName);
  350. }
  351. return $this->appendNode($parentNode, $data, 'data');
  352. }
  353. throw new NotEncodableValueException('An unexpected value could not be serialized: '.(!\is_resource($data) ? var_export($data, true) : sprintf('%s resource', get_resource_type($data))));
  354. }
  355. /**
  356. * Selects the type of node to create and appends it to the parent.
  357. *
  358. * @param array|object $data
  359. */
  360. private function appendNode(\DOMNode $parentNode, $data, string $nodeName, string $key = null): bool
  361. {
  362. $node = $this->dom->createElement($nodeName);
  363. if (null !== $key) {
  364. $node->setAttribute('key', $key);
  365. }
  366. $appendNode = $this->selectNodeType($node, $data);
  367. // we may have decided not to append this node, either in error or if its $nodeName is not valid
  368. if ($appendNode) {
  369. $parentNode->appendChild($node);
  370. }
  371. return $appendNode;
  372. }
  373. /**
  374. * Checks if a value contains any characters which would require CDATA wrapping.
  375. */
  376. private function needsCdataWrapping(string $val): bool
  377. {
  378. return 0 < preg_match('/[<>&]/', $val);
  379. }
  380. /**
  381. * Tests the value being passed and decide what sort of element to create.
  382. *
  383. * @throws NotEncodableValueException
  384. */
  385. private function selectNodeType(\DOMNode $node, $val): bool
  386. {
  387. if (\is_array($val)) {
  388. return $this->buildXml($node, $val);
  389. } elseif ($val instanceof \SimpleXMLElement) {
  390. $child = $this->dom->importNode(dom_import_simplexml($val), true);
  391. $node->appendChild($child);
  392. } elseif ($val instanceof \Traversable) {
  393. $this->buildXml($node, $val);
  394. } elseif ($val instanceof \DOMNode) {
  395. $child = $this->dom->importNode($val, true);
  396. $node->appendChild($child);
  397. } elseif (\is_object($val)) {
  398. if (null === $this->serializer) {
  399. throw new BadMethodCallException(sprintf('The serializer needs to be set to allow "%s()" to be used with object data.', __METHOD__));
  400. }
  401. return $this->selectNodeType($node, $this->serializer->normalize($val, $this->format, $this->context));
  402. } elseif (is_numeric($val)) {
  403. return $this->appendText($node, (string) $val);
  404. } elseif (\is_string($val) && $this->needsCdataWrapping($val)) {
  405. return $this->appendCData($node, $val);
  406. } elseif (\is_string($val)) {
  407. return $this->appendText($node, $val);
  408. } elseif (\is_bool($val)) {
  409. return $this->appendText($node, (int) $val);
  410. }
  411. return true;
  412. }
  413. /**
  414. * Create a DOM document, taking serializer options into account.
  415. */
  416. private function createDomDocument(array $context): \DOMDocument
  417. {
  418. $document = new \DOMDocument();
  419. // Set an attribute on the DOM document specifying, as part of the XML declaration,
  420. $xmlOptions = [
  421. // nicely formats output with indentation and extra space
  422. self::FORMAT_OUTPUT => 'formatOutput',
  423. // the version number of the document
  424. self::VERSION => 'xmlVersion',
  425. // the encoding of the document
  426. self::ENCODING => 'encoding',
  427. // whether the document is standalone
  428. self::STANDALONE => 'xmlStandalone',
  429. ];
  430. foreach ($xmlOptions as $xmlOption => $documentProperty) {
  431. if ($contextOption = $context[$xmlOption] ?? $this->defaultContext[$xmlOption] ?? false) {
  432. $document->$documentProperty = $contextOption;
  433. }
  434. }
  435. return $document;
  436. }
  437. }