GenericSerializationVisitor.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\Metadata\ClassMetadata;
  19. use JMS\SerializerBundle\Metadata\PropertyMetadata;
  20. abstract class GenericSerializationVisitor extends AbstractVisitor
  21. {
  22. private $navigator;
  23. private $root;
  24. private $dataStack;
  25. private $data;
  26. public function setNavigator(GraphNavigator $navigator)
  27. {
  28. $this->navigator = $navigator;
  29. $this->root = null;
  30. $this->dataStack = new \SplStack;
  31. }
  32. public function getNavigator()
  33. {
  34. return $this->navigator;
  35. }
  36. public function visitNull($data, array $type)
  37. {
  38. return null;
  39. }
  40. public function visitString($data, array $type)
  41. {
  42. if (null === $this->root) {
  43. $this->root = $data;
  44. }
  45. return $data;
  46. }
  47. public function visitBoolean($data, array $type)
  48. {
  49. if (null === $this->root) {
  50. $this->root = $data;
  51. }
  52. return $data;
  53. }
  54. public function visitInteger($data, array $type)
  55. {
  56. if (null === $this->root) {
  57. $this->root = $data;
  58. }
  59. return $data;
  60. }
  61. public function visitDouble($data, array $type)
  62. {
  63. if (null === $this->root) {
  64. $this->root = $data;
  65. }
  66. return $data;
  67. }
  68. /**
  69. * @param array $data
  70. * @param array $type
  71. */
  72. public function visitArray($data, array $type)
  73. {
  74. if (null === $this->root) {
  75. $this->root = array();
  76. $rs = &$this->root;
  77. } else {
  78. // ArrayObject is specially treated by the json_encode function and
  79. // serialized to { } while a mere array would be serialized to [].
  80. $rs = isset($type['params'][1]) ? new \ArrayObject() : array();
  81. }
  82. foreach ($data as $k => $v) {
  83. $v = $this->navigator->accept($v, isset($type['params'][1]) ? $type['params'][1] : null, $this);
  84. if (null === $v && (!is_string($k) || !$this->shouldSerializeNull())) {
  85. continue;
  86. }
  87. $rs[$k] = $v;
  88. }
  89. return $rs;
  90. }
  91. public function startVisitingObject(ClassMetadata $metadata, $data, array $type)
  92. {
  93. if (null === $this->root) {
  94. $this->root = new \stdClass;
  95. }
  96. $this->dataStack->push($this->data);
  97. $this->data = array();
  98. }
  99. public function endVisitingObject(ClassMetadata $metadata, $data, array $type)
  100. {
  101. $rs = $this->data;
  102. $this->data = $this->dataStack->pop();
  103. if ($this->root instanceof \stdClass && 0 === $this->dataStack->count()) {
  104. $this->root = $rs;
  105. }
  106. return $rs;
  107. }
  108. public function visitProperty(PropertyMetadata $metadata, $data)
  109. {
  110. $v = (null === $metadata->getter ? $metadata->reflection->getValue($data)
  111. : $data->{$metadata->getter}());
  112. $v = $this->navigator->accept($v, $metadata->type, $this);
  113. if (null === $v && !$this->shouldSerializeNull()) {
  114. return;
  115. }
  116. $k = $this->namingStrategy->translateName($metadata);
  117. if ($metadata->inline && is_array($v)) {
  118. $this->data = array_merge($this->data, $v);
  119. } else {
  120. $this->data[$k] = $v;
  121. }
  122. }
  123. /**
  124. * Allows you to add additional data to the current object/root element.
  125. *
  126. * @param string $key
  127. * @param scalar|array $value This value must either be a regular scalar, or an array.
  128. * It must not contain any objects anymore.
  129. */
  130. public function addData($key, $value)
  131. {
  132. if (isset($this->data[$key])) {
  133. throw new \InvalidArgumentException(sprintf('There is already data for "%s".', $key));
  134. }
  135. $this->data[$key] = $value;
  136. }
  137. public function getRoot()
  138. {
  139. return $this->root;
  140. }
  141. /**
  142. * @param array $data
  143. */
  144. public function setRoot($data)
  145. {
  146. $this->root = $data;
  147. }
  148. }