GenericSerializationVisitor.php 4.0 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 AbstractSerializationVisitor
  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, $type)
  38. {
  39. if (null === $this->root) {
  40. $this->root = $data;
  41. }
  42. return $data;
  43. }
  44. public function visitBoolean($data, $type)
  45. {
  46. if (null === $this->root) {
  47. $this->root = $data;
  48. }
  49. return $data;
  50. }
  51. public function visitInteger($data, $type)
  52. {
  53. if (null === $this->root) {
  54. $this->root = $data;
  55. }
  56. return $data;
  57. }
  58. public function visitDouble($data, $type)
  59. {
  60. if (null === $this->root) {
  61. $this->root = $data;
  62. }
  63. return $data;
  64. }
  65. public function visitNull($data, $type)
  66. {
  67. return null;
  68. }
  69. public function visitArray($data, $type)
  70. {
  71. if (null === $this->root) {
  72. $this->root = array();
  73. $rs = &$this->root;
  74. } else {
  75. $rs = array();
  76. }
  77. foreach ($data as $k => $v) {
  78. $v = $this->navigator->accept($v, null, $this);
  79. if (null === $v && (!is_string($k) || !$this->isNullable())) {
  80. continue;
  81. }
  82. $rs[$k] = $v;
  83. }
  84. return $rs;
  85. }
  86. public function visitTraversable($data, $type)
  87. {
  88. return $this->visitArray($data, $type);
  89. }
  90. public function startVisitingObject(ClassMetadata $metadata, $data, $type)
  91. {
  92. if (null === $this->root) {
  93. $this->root = new \stdClass;
  94. }
  95. $this->dataStack->push($this->data);
  96. $this->data = array();
  97. }
  98. public function endVisitingObject(ClassMetadata $metadata, $data, $type)
  99. {
  100. $rs = $this->data;
  101. $this->data = $this->dataStack->pop();
  102. if ($this->root instanceof \stdClass && 0 === $this->dataStack->count()) {
  103. $this->root = $rs;
  104. }
  105. return $rs;
  106. }
  107. public function visitProperty(PropertyMetadata $metadata, $data)
  108. {
  109. $v = (null === $metadata->getter ? $metadata->reflection->getValue($data)
  110. : $data->{$metadata->getter}());
  111. $v = $this->navigator->accept($v, null, $this);
  112. if (null === $v && !$this->isNullable()) {
  113. return;
  114. }
  115. $k = $this->namingStrategy->translateName($metadata);
  116. if ($metadata->inline && is_array($v)) {
  117. $this->data = array_merge($this->data, $v);
  118. } else {
  119. $this->data[$k] = $v;
  120. }
  121. }
  122. public function visitPropertyUsingCustomHandler(PropertyMetadata $metadata, $object)
  123. {
  124. // TODO
  125. return false;
  126. }
  127. public function getRoot()
  128. {
  129. return $this->root;
  130. }
  131. public function setRoot($data)
  132. {
  133. $this->root = $data;
  134. }
  135. }