XmlSerializationVisitor.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 AbstractSerializationVisitor
  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. private $hasValue;
  39. public function setDefaultRootName($name)
  40. {
  41. $this->defaultRootName = $name;
  42. }
  43. public function setDefaultVersion($version)
  44. {
  45. $this->defaultVersion = $version;
  46. }
  47. public function setDefaultEncoding($encoding)
  48. {
  49. $this->defaultEncoding = $encoding;
  50. }
  51. public function setNavigator(GraphNavigator $navigator)
  52. {
  53. $this->navigator = $navigator;
  54. $this->document = null;
  55. $this->stack = new \SplStack;
  56. $this->metadataStack = new \SplStack;
  57. }
  58. public function getNavigator()
  59. {
  60. return $this->navigator;
  61. }
  62. public function visitString($data, $type)
  63. {
  64. if (null === $this->document) {
  65. $this->document = $this->createDocument(null, null, true);
  66. $this->currentNode->appendChild($this->document->createCDATASection($data));
  67. return;
  68. }
  69. return $this->document->createCDATASection($data);
  70. }
  71. public function visitBoolean($data, $type)
  72. {
  73. if (null === $this->document) {
  74. $this->document = $this->createDocument(null, null, true);
  75. $this->currentNode->appendChild($this->document->createTextNode($data ? 'true' : 'false'));
  76. return;
  77. }
  78. return $this->document->createTextNode($data ? 'true' : 'false');
  79. }
  80. public function visitInteger($data, $type)
  81. {
  82. return $this->visitNumeric($data, $type);
  83. }
  84. public function visitDouble($data, $type)
  85. {
  86. return $this->visitNumeric($data, $type);
  87. }
  88. public function visitArray($data, $type)
  89. {
  90. if (null === $this->document) {
  91. $this->document = $this->createDocument(null, null, true);
  92. }
  93. $entryName = (null !== $this->currentMetadata && null !== $this->currentMetadata->xmlEntryName) ? $this->currentMetadata->xmlEntryName : 'entry';
  94. $keyAttributeName = (null !== $this->currentMetadata && null !== $this->currentMetadata->xmlKeyAttribute) ? $this->currentMetadata->xmlKeyAttribute : null;
  95. foreach ($data as $k => $v) {
  96. $entryNode = $this->document->createElement($entryName);
  97. $this->currentNode->appendChild($entryNode);
  98. $this->setCurrentNode($entryNode);
  99. if (null !== $keyAttributeName) {
  100. $entryNode->setAttribute($keyAttributeName, (string) $k);
  101. }
  102. if (null !== $node = $this->navigator->accept($v, null, $this)) {
  103. $this->currentNode->appendChild($node);
  104. }
  105. $this->revertCurrentNode();
  106. }
  107. }
  108. public function visitTraversable($data, $type)
  109. {
  110. return $this->visitArray($data, $type);
  111. }
  112. public function startVisitingObject(ClassMetadata $metadata, $data, $type)
  113. {
  114. if (null === $this->document) {
  115. $this->document = $this->createDocument(null, null, false);
  116. $this->document->appendChild($this->currentNode = $this->document->createElement($metadata->xmlRootName ?: $this->defaultRootName));
  117. }
  118. $this->hasValue = false;
  119. }
  120. public function visitProperty(PropertyMetadata $metadata, $object)
  121. {
  122. $v = (null === $metadata->getter ? $metadata->reflection->getValue($object)
  123. : $object->{$metadata->getter}());
  124. if (null === $v) {
  125. return;
  126. }
  127. if ($metadata->xmlAttribute) {
  128. $node = $this->navigator->accept($v, null, $this);
  129. if (!$node instanceof \DOMCharacterData) {
  130. throw new RuntimeException(sprintf('Unsupported value for XML attribute. Expected character data, but got %s.', json_encode($v)));
  131. }
  132. $this->currentNode->setAttribute($this->namingStrategy->translateName($metadata), $node->nodeValue);
  133. return;
  134. }
  135. if (($metadata->xmlValue && $this->currentNode->childNodes->length > 0)
  136. || (!$metadata->xmlValue && $this->hasValue)) {
  137. throw new \RuntimeException(sprintf('If you make use of @XmlValue, all other properties in the class must have the @XmlAttribute annotation. Invalid usage detected in class %s.', $metadata->reflection->class));
  138. }
  139. if ($metadata->xmlValue) {
  140. $this->hasValue = true;
  141. $node = $this->navigator->accept($v, null, $this);
  142. if (!$node instanceof \DOMCharacterData) {
  143. throw new RuntimeException(sprintf('Unsupported value for property %s::$%s. Expected character data, but got %s.', $metadata->reflection->class, $metadata->reflection->name, is_object($node) ? get_class($node) : gettype($node)));
  144. }
  145. $this->currentNode->appendChild($node);
  146. return;
  147. }
  148. if ($addEnclosingElement = (!$metadata->xmlCollection || !$metadata->xmlCollectionInline) && !$metadata->inline) {
  149. $element = $this->document->createElement($this->namingStrategy->translateName($metadata));
  150. $this->setCurrentNode($element);
  151. }
  152. $this->setCurrentMetadata($metadata);
  153. if (null !== $node = $this->navigator->accept($v, null, $this)) {
  154. $this->currentNode->appendChild($node);
  155. }
  156. $this->revertCurrentMetadata();
  157. if ($addEnclosingElement) {
  158. $this->revertCurrentNode();
  159. if ($element->hasChildNodes() || $element->hasAttributes()) {
  160. $this->currentNode->appendChild($element);
  161. }
  162. }
  163. }
  164. public function endVisitingObject(ClassMetadata $metadata, $data, $type)
  165. {
  166. }
  167. public function visitPropertyUsingCustomHandler(PropertyMetadata $metadata, $object)
  168. {
  169. // TODO
  170. return false;
  171. }
  172. public function getResult()
  173. {
  174. return $this->document->saveXML();
  175. }
  176. public function getCurrentNode()
  177. {
  178. return $this->currentNode;
  179. }
  180. public function getCurrentMetadata()
  181. {
  182. return $this->currentMetadata;
  183. }
  184. public function getDocument()
  185. {
  186. return $this->document;
  187. }
  188. public function setCurrentMetadata(PropertyMetadata $metadata)
  189. {
  190. $this->metadataStack->push($this->currentMetadata);
  191. $this->currentMetadata = $metadata;
  192. }
  193. public function setCurrentNode(\DOMNode $node)
  194. {
  195. $this->stack->push($this->currentNode);
  196. $this->currentNode = $node;
  197. }
  198. public function revertCurrentNode()
  199. {
  200. return $this->currentNode = $this->stack->pop();
  201. }
  202. public function revertCurrentMetadata()
  203. {
  204. return $this->currentMetadata = $this->metadataStack->pop();
  205. }
  206. public function createDocument($version = null, $encoding = null, $addRoot = true)
  207. {
  208. $doc = new \DOMDocument($version ?: $this->defaultVersion, $encoding ?: $this->defaultEncoding);
  209. $doc->formatOutput = true;
  210. if ($addRoot) {
  211. $this->setCurrentNode($rootNode = $doc->createElement($this->defaultRootName));
  212. $doc->appendChild($rootNode);
  213. }
  214. return $doc;
  215. }
  216. private function visitNumeric($data, $type)
  217. {
  218. if (null === $this->document) {
  219. $this->document = $this->createDocument(null, null, true);
  220. $this->currentNode->appendChild($textNode = $this->document->createTextNode((string) $data));
  221. return $textNode;
  222. }
  223. return $this->document->createTextNode((string) $data);
  224. }
  225. }