XmlDeserializationVisitor.php 9.1 KB

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