XmlDeserializationVisitor.php 8.2 KB

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