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