GenericDeserializationVisitor.php 5.5 KB

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