GenericSerializationVisitor.php 4.7 KB

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