XmlDeserializationVisitor.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. 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.', $this->currentMetadata->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->xmlValue) {
  164. $v = $this->navigator->accept($data, $metadata->type, $this);
  165. $metadata->reflection->setValue($this->currentObject, $v);
  166. return;
  167. }
  168. if ($metadata->xmlCollection) {
  169. $enclosingElem = $data;
  170. if (!$metadata->xmlCollectionInline && isset($data->$name)) {
  171. $enclosingElem = $data->$name;
  172. }
  173. $this->setCurrentMetadata($metadata);
  174. $v = $this->navigator->accept($enclosingElem, $metadata->type, $this);
  175. $this->revertCurrentMetadata();
  176. $metadata->reflection->setValue($this->currentObject, $v);
  177. return;
  178. }
  179. if (!isset($data->$name)) {
  180. return;
  181. }
  182. $v = $this->navigator->accept($data->$name, $metadata->type, $this);
  183. if (null === $metadata->setter) {
  184. $metadata->reflection->setValue($this->currentObject, $v);
  185. return;
  186. }
  187. $this->currentObject->{$metadata->setter}($v);
  188. }
  189. public function endVisitingObject(ClassMetadata $metadata, $data, $type)
  190. {
  191. $rs = $this->currentObject;
  192. $this->revertCurrentObject();
  193. return $rs;
  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. }