XmlDeserializationVisitor.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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\XmlErrorException;
  19. use JMS\SerializerBundle\Exception\RuntimeException;
  20. use JMS\SerializerBundle\Metadata\PropertyMetadata;
  21. use JMS\SerializerBundle\Metadata\ClassMetadata;
  22. use JMS\SerializerBundle\Serializer\Construction\ObjectConstructorInterface;
  23. use JMS\SerializerBundle\Serializer\Naming\PropertyNamingStrategyInterface;
  24. class XmlDeserializationVisitor extends AbstractDeserializationVisitor
  25. {
  26. private $objectConstructor;
  27. private $objectStack;
  28. private $metadataStack;
  29. private $currentObject;
  30. private $currentMetadata;
  31. private $result;
  32. private $navigator;
  33. private $disableExternalEntities;
  34. public function __construct(PropertyNamingStrategyInterface $namingStrategy, array $customHandlers, ObjectConstructorInterface $objectConstructor, $disableExternalEntities = true)
  35. {
  36. parent::__construct($namingStrategy, $customHandlers);
  37. $this->objectConstructor = $objectConstructor;
  38. $this->disableExternalEntities = $disableExternalEntities;
  39. }
  40. public function setNavigator(GraphNavigator $navigator)
  41. {
  42. $this->navigator = $navigator;
  43. $this->objectStack = new \SplStack;
  44. $this->metadataStack = new \SplStack;
  45. $this->result = null;
  46. }
  47. public function getNavigator()
  48. {
  49. return $this->navigator;
  50. }
  51. public function prepare($data)
  52. {
  53. $previous = libxml_use_internal_errors(true);
  54. $previousEntityLoaderState = libxml_disable_entity_loader($this->disableExternalEntities);
  55. $dom = new \DOMDocument();
  56. $dom->loadXML($data);
  57. foreach ($dom->childNodes as $child) {
  58. if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
  59. throw new \InvalidArgumentException('Document types are not allowed.');
  60. }
  61. }
  62. $doc = simplexml_load_string($data);
  63. libxml_use_internal_errors($previous);
  64. libxml_disable_entity_loader($previousEntityLoaderState);
  65. if (false === $doc) {
  66. throw new XmlErrorException(libxml_get_last_error());
  67. }
  68. return $doc;
  69. }
  70. public function visitString($data, $type)
  71. {
  72. $data = (string) $data;
  73. if (null === $this->result) {
  74. $this->result = $data;
  75. }
  76. return $data;
  77. }
  78. public function visitBoolean($data, $type)
  79. {
  80. $data = (string) $data;
  81. if ('true' === $data) {
  82. $data = true;
  83. } else if ('false' === $data) {
  84. $data = false;
  85. } else {
  86. throw new RuntimeException(sprintf('Could not convert data to boolean. Expected "true", or "false", but got %s.', json_encode($data)));
  87. }
  88. if (null === $this->result) {
  89. $this->result = $data;
  90. }
  91. return $data;
  92. }
  93. public function visitInteger($data, $type)
  94. {
  95. $data = (integer) $data;
  96. if (null === $this->result) {
  97. $this->result = $data;
  98. }
  99. return $data;
  100. }
  101. public function visitDouble($data, $type)
  102. {
  103. $data = (double) $data;
  104. if (null === $this->result) {
  105. $this->result = $data;
  106. }
  107. return $data;
  108. }
  109. public function visitArray($data, $type)
  110. {
  111. $entryName = null !== $this->currentMetadata && $this->currentMetadata->xmlEntryName ? $this->currentMetadata->xmlEntryName : 'entry';
  112. if (!isset($data->$entryName)) {
  113. if (null === $this->result) {
  114. return $this->result = array();
  115. }
  116. return array();
  117. }
  118. if ('array' === $type) {
  119. throw new RuntimeException(sprintf('You must specify either a list type, or a key and entry type for type array.'));
  120. }
  121. if (false === $pos = strpos($type, ',', 6)) {
  122. $listType = substr($type, 6, -1);
  123. $result = array();
  124. if (null === $this->result) {
  125. $this->result = &$result;
  126. }
  127. foreach ($data->$entryName as $v) {
  128. $result[] = $this->navigator->accept($v, $listType, $this);
  129. }
  130. return $result;
  131. }
  132. if (null === $this->currentMetadata) {
  133. throw new RuntimeException('Maps are not supported on top-level without metadata.');
  134. }
  135. $keyType = trim(substr($type, 6, $pos - 6));
  136. $entryType = trim(substr($type, $pos+1, -1));
  137. $result = array();
  138. if (null === $this->result) {
  139. $this->result = &$result;
  140. }
  141. foreach ($data->$entryName as $v) {
  142. if (!isset($v[$this->currentMetadata->xmlKeyAttribute])) {
  143. throw new RuntimeException(sprintf('The key attribute "%s" must be set for each entry of the map.', $this->currentMetadata->xmlKeyAttribute));
  144. }
  145. $k = $this->navigator->accept($v[$this->currentMetadata->xmlKeyAttribute], $keyType, $this);
  146. $result[$k] = $this->navigator->accept($v, $entryType, $this);
  147. }
  148. return $result;
  149. }
  150. public function visitTraversable($data, $type)
  151. {
  152. throw new RuntimeException('Traversable is not supported for Deserialization.');
  153. }
  154. public function startVisitingObject(ClassMetadata $metadata, $data, $type)
  155. {
  156. $this->setCurrentObject($this->objectConstructor->construct($this, $metadata, $data, $type));
  157. if (null === $this->result) {
  158. $this->result = $this->currentObject;
  159. }
  160. }
  161. public function visitProperty(PropertyMetadata $metadata, $data)
  162. {
  163. $name = $this->namingStrategy->translateName($metadata);
  164. if (!$metadata->type) {
  165. throw new RuntimeException(sprintf('You must define a type for %s::$%s.', $metadata->reflection->getDeclaringClass()->getName(), $metadata->name));
  166. }
  167. if ($metadata->xmlAttribute) {
  168. if (isset($data[$name])) {
  169. $v = $this->navigator->accept($data[$name], $metadata->type, $this);
  170. $metadata->reflection->setValue($this->currentObject, $v);
  171. }
  172. return;
  173. }
  174. if ($metadata->xmlValue) {
  175. $v = $this->navigator->accept($data, $metadata->type, $this);
  176. $metadata->reflection->setValue($this->currentObject, $v);
  177. return;
  178. }
  179. if ($metadata->xmlCollection) {
  180. $enclosingElem = $data;
  181. if (!$metadata->xmlCollectionInline && isset($data->$name)) {
  182. $enclosingElem = $data->$name;
  183. }
  184. $this->setCurrentMetadata($metadata);
  185. $v = $this->navigator->accept($enclosingElem, $metadata->type, $this);
  186. $this->revertCurrentMetadata();
  187. $metadata->reflection->setValue($this->currentObject, $v);
  188. return;
  189. }
  190. if (!isset($data->$name)) {
  191. return;
  192. }
  193. $v = $this->navigator->accept($data->$name, $metadata->type, $this);
  194. if (null === $metadata->setter) {
  195. $metadata->reflection->setValue($this->currentObject, $v);
  196. return;
  197. }
  198. $this->currentObject->{$metadata->setter}($v);
  199. }
  200. public function endVisitingObject(ClassMetadata $metadata, $data, $type)
  201. {
  202. $rs = $this->currentObject;
  203. $this->revertCurrentObject();
  204. return $rs;
  205. }
  206. public function visitPropertyUsingCustomHandler(PropertyMetadata $metadata, $object)
  207. {
  208. // TODO
  209. return false;
  210. }
  211. public function setCurrentObject($object)
  212. {
  213. $this->objectStack->push($this->currentObject);
  214. $this->currentObject = $object;
  215. }
  216. public function getCurrentObject()
  217. {
  218. return $this->currentObject;
  219. }
  220. public function revertCurrentObject()
  221. {
  222. return $this->currentObject = $this->objectStack->pop();
  223. }
  224. public function setCurrentMetadata(PropertyMetadata $metadata)
  225. {
  226. $this->metadataStack->push($this->currentMetadata);
  227. $this->currentMetadata = $metadata;
  228. }
  229. public function getCurrentMetadata()
  230. {
  231. return $this->currentMetadata;
  232. }
  233. public function revertCurrentMetadata()
  234. {
  235. return $this->currentMetadata = $this->metadataStack->pop();
  236. }
  237. public function getResult()
  238. {
  239. return $this->result;
  240. }
  241. }