XmlSerializationVisitor.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\SerializerBundle\Serializer;
  18. use JMS\SerializerBundle\Exception\RuntimeException;
  19. use JMS\SerializerBundle\Metadata\ClassMetadata;
  20. use JMS\SerializerBundle\Metadata\PropertyMetadata;
  21. use JMS\SerializerBundle\Serializer\Naming\PropertyNamingStrategyInterface;
  22. /**
  23. * XmlSerializationVisitor.
  24. *
  25. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  26. */
  27. class XmlSerializationVisitor extends AbstractVisitor
  28. {
  29. public $document;
  30. private $navigator;
  31. private $defaultRootName = 'result';
  32. private $defaultVersion = '1.0';
  33. private $defaultEncoding = 'UTF-8';
  34. private $stack;
  35. private $metadataStack;
  36. private $currentNode;
  37. private $currentMetadata;
  38. public function setDefaultRootName($name)
  39. {
  40. $this->defaultRootName = $name;
  41. }
  42. public function setDefaultVersion($version)
  43. {
  44. $this->defaultVersion = $version;
  45. }
  46. public function setDefaultEncoding($encoding)
  47. {
  48. $this->defaultEncoding = $encoding;
  49. }
  50. public function setNavigator(GraphNavigator $navigator)
  51. {
  52. $this->navigator = $navigator;
  53. $this->document = null;
  54. $this->stack = new \SplStack;
  55. $this->metadataStack = new \SplStack;
  56. }
  57. public function getNavigator()
  58. {
  59. return $this->navigator;
  60. }
  61. public function visitString($data, $type)
  62. {
  63. if (null === $this->document) {
  64. $this->document = $this->createDocument(null, null, true);
  65. $this->currentNode->appendChild($this->document->createCDATASection($data));
  66. return;
  67. }
  68. return $this->document->createCDATASection($data);
  69. }
  70. public function visitBoolean($data, $type)
  71. {
  72. if (null === $this->document) {
  73. $this->document = $this->createDocument(null, null, true);
  74. $this->currentNode->appendChild($this->document->createTextNode($data ? 'true' : 'false'));
  75. return;
  76. }
  77. return $this->document->createTextNode($data ? 'true' : 'false');
  78. }
  79. public function visitInteger($data, $type)
  80. {
  81. return $this->visitNumeric($data, $type);
  82. }
  83. public function visitDouble($data, $type)
  84. {
  85. return $this->visitNumeric($data, $type);
  86. }
  87. public function visitArray($data, $type)
  88. {
  89. if (null === $this->document) {
  90. $this->document = $this->createDocument(null, null, true);
  91. }
  92. $entryName = (null !== $this->currentMetadata && null !== $this->currentMetadata->xmlEntryName) ? $this->currentMetadata->xmlEntryName : 'entry';
  93. $keyAttributeName = (null !== $this->currentMetadata && null !== $this->currentMetadata->xmlKeyAttribute) ? $this->currentMetadata->xmlKeyAttribute : null;
  94. foreach ($data as $k => $v) {
  95. $entryNode = $this->document->createElement($entryName);
  96. $this->currentNode->appendChild($entryNode);
  97. $this->setCurrentNode($entryNode);
  98. if (null !== $keyAttributeName) {
  99. $entryNode->setAttribute($keyAttributeName, (string) $k);
  100. }
  101. if (null !== $node = $this->navigator->accept($v, null, $this)) {
  102. $this->currentNode->appendChild($node);
  103. }
  104. $this->revertCurrentNode();
  105. }
  106. }
  107. public function visitTraversable($data, $type)
  108. {
  109. return $this->visitArray($data, $type);
  110. }
  111. public function visitUsingCustomHandler($data, $type, &$visited)
  112. {
  113. foreach ($this->customHandlers as $handler) {
  114. $rs = $handler->serialize($this, $data, $type, $visited);
  115. if ($visited) {
  116. return $rs;
  117. }
  118. }
  119. }
  120. public function startVisitingObject(ClassMetadata $metadata, $data, $type)
  121. {
  122. if (null === $this->document) {
  123. $this->document = $this->createDocument(null, null, false);
  124. $this->document->appendChild($this->currentNode = $this->document->createElement($metadata->xmlRootName ?: $this->defaultRootName));
  125. }
  126. }
  127. public function visitProperty(PropertyMetadata $metadata, $object)
  128. {
  129. if (null === $v = $metadata->reflection->getValue($object)) {
  130. return;
  131. }
  132. if ($metadata->xmlAttribute) {
  133. $node = $this->navigator->accept($v, null, $this);
  134. if (!$node instanceof \DOMCharacterData) {
  135. throw new RuntimeException('Unsupported value for XML attribute. Expected character data, but got %s.', json_encode($v));
  136. }
  137. $this->currentNode->setAttribute($this->namingStrategy->translateName($metadata), $node->nodeValue);
  138. return;
  139. }
  140. if ($addEnclosingElement = !$metadata->xmlCollection || !$metadata->xmlCollectionInline) {
  141. $element = $this->document->createElement($this->namingStrategy->translateName($metadata));
  142. $this->setCurrentNode($element);
  143. }
  144. $this->setCurrentMetadata($metadata);
  145. if (null !== $node = $this->navigator->accept($v, null, $this)) {
  146. $this->currentNode->appendChild($node);
  147. }
  148. $this->revertCurrentMetadata();
  149. if ($addEnclosingElement) {
  150. $this->revertCurrentNode();
  151. if ($element->hasChildNodes() || $element->hasAttributes()) {
  152. $this->currentNode->appendChild($element);
  153. }
  154. }
  155. }
  156. public function endVisitingObject(ClassMetadata $metadata, $data, $type)
  157. {
  158. }
  159. public function visitPropertyUsingCustomHandler(PropertyMetadata $metadata, $object)
  160. {
  161. // TODO
  162. return false;
  163. }
  164. public function getResult()
  165. {
  166. return $this->document->saveXML();
  167. }
  168. public function getCurrentNode()
  169. {
  170. return $this->currentNode;
  171. }
  172. public function getCurrentMetadata()
  173. {
  174. return $this->currentMetadata;
  175. }
  176. public function getDocument()
  177. {
  178. return $this->document;
  179. }
  180. public function setCurrentMetadata(PropertyMetadata $metadata)
  181. {
  182. $this->metadataStack->push($this->currentMetadata);
  183. $this->currentMetadata = $metadata;
  184. }
  185. public function setCurrentNode(\DOMNode $node)
  186. {
  187. $this->stack->push($this->currentNode);
  188. $this->currentNode = $node;
  189. }
  190. public function revertCurrentNode()
  191. {
  192. return $this->currentNode = $this->stack->pop();
  193. }
  194. public function revertCurrentMetadata()
  195. {
  196. return $this->currentMetadata = $this->metadataStack->pop();
  197. }
  198. public function createDocument($version = null, $encoding = null, $addRoot = true)
  199. {
  200. $doc = new \DOMDocument($version ?: $this->defaultVersion, $encoding ?: $this->defaultEncoding);
  201. $doc->formatOutput = true;
  202. if ($addRoot) {
  203. $this->setCurrentNode($rootNode = $doc->createElement($this->defaultRootName));
  204. $doc->appendChild($rootNode);
  205. }
  206. return $doc;
  207. }
  208. private function visitNumeric($data, $type)
  209. {
  210. if (null === $this->document) {
  211. $this->document = $this->createDocument(null, null, true);
  212. $this->currentNode->appendChild($textNode = $this->document->createTextNode((string) $data));
  213. return $textNode;
  214. }
  215. return $this->document->createTextNode((string) $data);
  216. }
  217. }