GenericSerializationVisitor.php 4.5 KB

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