XmlDeserializationVisitor.php 8.2 KB

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