XmlDeserializationVisitor.php 9.8 KB

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