GenericSerializationVisitor.php 4.2 KB

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