GenericDeserializationVisitor.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 AbstractVisitor
  29. {
  30. private $navigator;
  31. private $result;
  32. private $objectStack;
  33. private $currentObject;
  34. public function setNavigator(GraphNavigator $navigator)
  35. {
  36. $this->navigator = $navigator;
  37. $this->result = null;
  38. $this->objectStack = new \SplStack;
  39. }
  40. public function getNavigator()
  41. {
  42. return $this->navigator;
  43. }
  44. public function prepare($data)
  45. {
  46. return $this->decode($data);
  47. }
  48. public function visitNull($data, array $type)
  49. {
  50. return null;
  51. }
  52. public function visitString($data, array $type)
  53. {
  54. $data = (string) $data;
  55. if (null === $this->result) {
  56. $this->result = $data;
  57. }
  58. return $data;
  59. }
  60. public function visitBoolean($data, array $type)
  61. {
  62. $data = (Boolean) $data;
  63. if (null === $this->result) {
  64. $this->result = $data;
  65. }
  66. return $data;
  67. }
  68. public function visitInteger($data, array $type)
  69. {
  70. $data = (integer) $data;
  71. if (null === $this->result) {
  72. $this->result = $data;
  73. }
  74. return $data;
  75. }
  76. public function visitDouble($data, array $type)
  77. {
  78. $data = (double) $data;
  79. if (null === $this->result) {
  80. $this->result = $data;
  81. }
  82. return $data;
  83. }
  84. public function visitArray($data, array $type)
  85. {
  86. if ( ! is_array($data)) {
  87. throw new RuntimeException(sprintf('Expected array, but got %s: %s', gettype($data), json_encode($data)));
  88. }
  89. // If no further parameters were given, keys/values are just passed as is.
  90. if ( ! $type['params']) {
  91. if (null === $this->result) {
  92. $this->result = $data;
  93. }
  94. return $data;
  95. }
  96. switch (count($type['params'])) {
  97. case 1: // Array is a list.
  98. $listType = $type['params'][0];
  99. $result = array();
  100. if (null === $this->result) {
  101. $this->result = &$result;
  102. }
  103. foreach ($data as $v) {
  104. $result[] = $this->navigator->accept($v, $listType, $this);
  105. }
  106. return $result;
  107. case 2: // Array is a map.
  108. list($keyType, $entryType) = $type['params'];
  109. $result = array();
  110. if (null === $this->result) {
  111. $this->result = &$result;
  112. }
  113. foreach ($data as $k => $v) {
  114. $result[$this->navigator->accept($k, $keyType, $this)] = $this->navigator->accept($v, $entryType, $this);
  115. }
  116. return $result;
  117. default:
  118. throw new \RuntimeException(sprintf('Array type cannot have more than 2 parameters, but got %s.', json_encode($type['params'])));
  119. }
  120. }
  121. public function startVisitingObject(ClassMetadata $metadata, $object, array $type)
  122. {
  123. $this->setCurrentObject($object);
  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->class, $metadata->name));
  136. }
  137. $v = $this->navigator->accept($data[$name], $metadata->type, $this);
  138. if (null === $v) {
  139. return;
  140. }
  141. if (null === $metadata->setter) {
  142. $metadata->reflection->setValue($this->currentObject, $v);
  143. return;
  144. }
  145. $this->currentObject->{$metadata->setter}($v);
  146. }
  147. public function endVisitingObject(ClassMetadata $metadata, $data, array $type)
  148. {
  149. $obj = $this->currentObject;
  150. $this->revertCurrentObject();
  151. return $obj;
  152. }
  153. public function getResult()
  154. {
  155. return $this->result;
  156. }
  157. public function setCurrentObject($object)
  158. {
  159. $this->objectStack->push($this->currentObject);
  160. $this->currentObject = $object;
  161. }
  162. public function getCurrentObject()
  163. {
  164. return $this->currentObject;
  165. }
  166. public function revertCurrentObject()
  167. {
  168. return $this->currentObject = $this->objectStack->pop();
  169. }
  170. abstract protected function decode($str);
  171. }