GenericDeserializationVisitor.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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\Serializer\Construction\ObjectConstructorInterface;
  20. use JMS\SerializerBundle\Serializer\Naming\PropertyNamingStrategyInterface;
  21. use JMS\SerializerBundle\Metadata\PropertyMetadata;
  22. use JMS\SerializerBundle\Metadata\ClassMetadata;
  23. abstract class GenericDeserializationVisitor extends AbstractVisitor
  24. {
  25. private $navigator;
  26. private $result;
  27. private $objectConstructor;
  28. private $objectStack;
  29. private $currentObject;
  30. public function __construct(PropertyNamingStrategyInterface $namingStrategy, array $customHandlers, ObjectConstructorInterface $objectConstructor)
  31. {
  32. parent::__construct($namingStrategy, $customHandlers);
  33. $this->objectConstructor = $objectConstructor;
  34. }
  35. public function setNavigator(GraphNavigator $navigator)
  36. {
  37. $this->navigator = $navigator;
  38. $this->result = null;
  39. $this->objectStack = new \SplStack;
  40. }
  41. public function getNavigator()
  42. {
  43. return $this->navigator;
  44. }
  45. public function prepare($data)
  46. {
  47. return $this->decode($data);
  48. }
  49. public function visitString($data, $type)
  50. {
  51. $data = (string) $data;
  52. if (null === $this->result) {
  53. $this->result = $data;
  54. }
  55. return $data;
  56. }
  57. public function visitBoolean($data, $type)
  58. {
  59. $data = (Boolean) $data;
  60. if (null === $this->result) {
  61. $this->result = $data;
  62. }
  63. return $data;
  64. }
  65. public function visitInteger($data, $type)
  66. {
  67. $data = (integer) $data;
  68. if (null === $this->result) {
  69. $this->result = $data;
  70. }
  71. return $data;
  72. }
  73. public function visitDouble($data, $type)
  74. {
  75. $data = (double) $data;
  76. if (null === $this->result) {
  77. $this->result = $data;
  78. }
  79. return $data;
  80. }
  81. public function visitArray($data, $type)
  82. {
  83. if (!is_array($data)) {
  84. throw new RuntimeException(sprintf('Expected array, but got %s: %s', gettype($data), json_encode($data)));
  85. }
  86. // not specified of which type keys/values should be, just pass as is
  87. if ('array' === $type) {
  88. if (null === $this->result) {
  89. $this->result = $data;
  90. }
  91. return $data;
  92. }
  93. // list
  94. if (false === $pos = strpos($type, ',', 6)) {
  95. $listType = substr($type, 6, -1);
  96. $result = array();
  97. if (null === $this->result) {
  98. $this->result = &$result;
  99. }
  100. foreach ($data as $v) {
  101. $result[] = $this->navigator->accept($v, $listType, $this);
  102. }
  103. return $result;
  104. }
  105. // map
  106. $keyType = trim(substr($type, 6, $pos - 6));
  107. $entryType = trim(substr($type, $pos+1, -1));
  108. $result = array();
  109. if (null === $this->result) {
  110. $this->result = &$result;
  111. }
  112. foreach ($data as $k => $v) {
  113. $result[$this->navigator->accept($k, $keyType, $this)] = $this->navigator->accept($v, $entryType, $this);
  114. }
  115. return $result;
  116. }
  117. public function visitTraversable($data, $type)
  118. {
  119. throw new RuntimeException('Traversable is not supported for deserialization.');
  120. }
  121. public function startVisitingObject(ClassMetadata $metadata, $data, $type)
  122. {
  123. $this->setCurrentObject($this->objectConstructor->construct($this, $metadata, $data, $type));
  124. if (null === $this->result) {
  125. $this->result = $this->currentObject;
  126. }
  127. }
  128. public function visitProperty(PropertyMetadata $metadata, $data)
  129. {
  130. $name = $this->namingStrategy->translateName($metadata);
  131. if (!isset($data[$name])) {
  132. return;
  133. }
  134. if (!$metadata->type) {
  135. throw new RuntimeException(sprintf('You must define a type for %s::$%s.', $metadata->reflection->getDeclaringClass()->getName(), $metadata->name));
  136. }
  137. $v = $this->navigator->accept($data[$name], $metadata->type, $this);
  138. if (null === $v) {
  139. return;
  140. }
  141. $metadata->reflection->setValue($this->currentObject, $v);
  142. }
  143. public function endVisitingObject(ClassMetadata $metadata, $data, $type)
  144. {
  145. $obj = $this->currentObject;
  146. $this->revertCurrentObject();
  147. return $obj;
  148. }
  149. public function visitUsingCustomHandler($data, $type, &$visited)
  150. {
  151. $visited = false;
  152. foreach ($this->customHandlers as $handler) {
  153. $rs = $handler->deserialize($this, $data, $type, $visited);
  154. if ($visited) {
  155. return $rs;
  156. }
  157. }
  158. }
  159. public function visitPropertyUsingCustomHandler(PropertyMetadata $metadata, $object)
  160. {
  161. // TODO
  162. return false;
  163. }
  164. public function getResult()
  165. {
  166. return $this->result;
  167. }
  168. public function setCurrentObject($object)
  169. {
  170. $this->objectStack->push($this->currentObject);
  171. $this->currentObject = $object;
  172. }
  173. public function getCurrentObject()
  174. {
  175. return $this->currentObject;
  176. }
  177. public function revertCurrentObject()
  178. {
  179. return $this->currentObject = $this->objectStack->pop();
  180. }
  181. abstract protected function decode($str);
  182. }