XmlDeserializationVisitor.php 8.6 KB

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