GenericSerializationVisitor.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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, $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 visitArray($data, $type)
  66. {
  67. if (null === $this->root) {
  68. $this->root = array();
  69. $rs = &$this->root;
  70. } else {
  71. $rs = array();
  72. }
  73. foreach ($data as $k => $v) {
  74. $v = $this->navigator->accept($v, null, $this);
  75. if (null === $v) {
  76. continue;
  77. }
  78. $rs[$k] = $v;
  79. }
  80. return $rs;
  81. }
  82. public function visitTraversable($data, $type)
  83. {
  84. return $this->visitArray($data, $type);
  85. }
  86. public function startVisitingObject(ClassMetadata $metadata, $data, $type)
  87. {
  88. if (null === $this->root) {
  89. $this->root = new \stdClass;
  90. }
  91. $this->dataStack->push($this->data);
  92. $this->data = array();
  93. }
  94. public function endVisitingObject(ClassMetadata $metadata, $data, $type)
  95. {
  96. $rs = $this->data;
  97. $this->data = $this->dataStack->pop();
  98. if ($this->root instanceof \stdClass && 0 === $this->dataStack->count()) {
  99. $this->root = $rs;
  100. }
  101. return $rs;
  102. }
  103. public function visitProperty(PropertyMetadata $metadata, $data)
  104. {
  105. $v = $this->navigator->accept($metadata->reflection->getValue($data), null, $this);
  106. if (null === $v) {
  107. return;
  108. }
  109. $k = $this->namingStrategy->translateName($metadata);
  110. $this->data[$k] = $v;
  111. }
  112. public function visitUsingCustomHandler($data, $type, &$visited)
  113. {
  114. $visited = false;
  115. foreach ($this->customHandlers as $handler) {
  116. $rs = $handler->serialize($this, $data, $type, $visited);
  117. if ($visited) {
  118. return $rs;
  119. }
  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. }