XmlDeserializationVisitor.php 9.2 KB

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