XmlSerializationVisitor.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. $visited = false;
  114. foreach ($this->customHandlers as $handler) {
  115. $rs = $handler->serialize($this, $data, $type, $visited);
  116. if ($visited) {
  117. return $rs;
  118. }
  119. }
  120. }
  121. public function startVisitingObject(ClassMetadata $metadata, $data, $type)
  122. {
  123. if (null === $this->document) {
  124. $this->document = $this->createDocument(null, null, false);
  125. $this->document->appendChild($this->currentNode = $this->document->createElement($metadata->xmlRootName ?: $this->defaultRootName));
  126. }
  127. }
  128. public function visitProperty(PropertyMetadata $metadata, $object)
  129. {
  130. if (null === $v = $metadata->reflection->getValue($object)) {
  131. return;
  132. }
  133. if ($metadata->xmlAttribute) {
  134. $node = $this->navigator->accept($v, null, $this);
  135. if (!$node instanceof \DOMCharacterData) {
  136. throw new RuntimeException('Unsupported value for XML attribute. Expected character data, but got %s.', json_encode($v));
  137. }
  138. $this->currentNode->setAttribute($this->namingStrategy->translateName($metadata), $node->nodeValue);
  139. return;
  140. }
  141. if ($addEnclosingElement = !$metadata->xmlCollection || !$metadata->xmlCollectionInline) {
  142. $element = $this->document->createElement($this->namingStrategy->translateName($metadata));
  143. $this->setCurrentNode($element);
  144. }
  145. $this->setCurrentMetadata($metadata);
  146. if (null !== $node = $this->navigator->accept($v, null, $this)) {
  147. $this->currentNode->appendChild($node);
  148. }
  149. $this->revertCurrentMetadata();
  150. if ($addEnclosingElement) {
  151. $this->revertCurrentNode();
  152. if ($element->hasChildNodes() || $element->hasAttributes()) {
  153. $this->currentNode->appendChild($element);
  154. }
  155. }
  156. }
  157. public function endVisitingObject(ClassMetadata $metadata, $data, $type)
  158. {
  159. }
  160. public function visitPropertyUsingCustomHandler(PropertyMetadata $metadata, $object)
  161. {
  162. // TODO
  163. return false;
  164. }
  165. public function getResult()
  166. {
  167. return $this->document->saveXML();
  168. }
  169. public function getCurrentNode()
  170. {
  171. return $this->currentNode;
  172. }
  173. public function getCurrentMetadata()
  174. {
  175. return $this->currentMetadata;
  176. }
  177. public function getDocument()
  178. {
  179. return $this->document;
  180. }
  181. public function setCurrentMetadata(PropertyMetadata $metadata)
  182. {
  183. $this->metadataStack->push($this->currentMetadata);
  184. $this->currentMetadata = $metadata;
  185. }
  186. public function setCurrentNode(\DOMNode $node)
  187. {
  188. $this->stack->push($this->currentNode);
  189. $this->currentNode = $node;
  190. }
  191. public function revertCurrentNode()
  192. {
  193. return $this->currentNode = $this->stack->pop();
  194. }
  195. public function revertCurrentMetadata()
  196. {
  197. return $this->currentMetadata = $this->metadataStack->pop();
  198. }
  199. public function createDocument($version = null, $encoding = null, $addRoot = true)
  200. {
  201. $doc = new \DOMDocument($version ?: $this->defaultVersion, $encoding ?: $this->defaultEncoding);
  202. $doc->formatOutput = true;
  203. if ($addRoot) {
  204. $this->setCurrentNode($rootNode = $doc->createElement($this->defaultRootName));
  205. $doc->appendChild($rootNode);
  206. }
  207. return $doc;
  208. }
  209. private function visitNumeric($data, $type)
  210. {
  211. if (null === $this->document) {
  212. $this->document = $this->createDocument(null, null, true);
  213. $this->currentNode->appendChild($textNode = $this->document->createTextNode((string) $data));
  214. return $textNode;
  215. }
  216. return $this->document->createTextNode((string) $data);
  217. }
  218. }