YamlSerializationVisitor.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 Symfony\Component\Yaml\Inline;
  19. use JMS\SerializerBundle\Metadata\PropertyMetadata;
  20. use JMS\SerializerBundle\Serializer\Naming\PropertyNamingStrategyInterface;
  21. use JMS\SerializerBundle\Metadata\ClassMetadata;
  22. use JMS\SerializerBundle\Util\Writer;
  23. /**
  24. * Serialization Visitor for the YAML format.
  25. *
  26. * @see http://www.yaml.org/spec/
  27. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  28. */
  29. class YamlSerializationVisitor extends AbstractVisitor
  30. {
  31. public $writer;
  32. private $navigator;
  33. private $stack;
  34. private $metadataStack;
  35. private $currentMetadata;
  36. public function __construct(PropertyNamingStrategyInterface $namingStrategy)
  37. {
  38. parent::__construct($namingStrategy);
  39. $this->writer = new Writer();
  40. }
  41. public function prepare($data)
  42. {
  43. return $data;
  44. }
  45. public function setNavigator(GraphNavigator $navigator)
  46. {
  47. $this->navigator = $navigator;
  48. $this->writer->reset();
  49. $this->stack = new \SplStack;
  50. $this->metadataStack = new \SplStack;
  51. }
  52. public function visitNull($data, array $type)
  53. {
  54. if ('' === $this->writer->content) {
  55. $this->writer->writeln('null');
  56. }
  57. return 'null';
  58. }
  59. public function visitString($data, array $type)
  60. {
  61. $v = Inline::dump($data);
  62. if ('' === $this->writer->content) {
  63. $this->writer->writeln($v);
  64. }
  65. return $v;
  66. }
  67. /**
  68. * @param array $data
  69. * @param array $type
  70. */
  71. public function visitArray($data, array $type)
  72. {
  73. $count = $this->writer->changeCount;
  74. $isList = array_keys($data) === range(0, count($data) - 1);
  75. foreach ($data as $k => $v) {
  76. if (null === $v && (!is_string($k) || !$this->shouldSerializeNull())) {
  77. continue;
  78. }
  79. if ($isList) {
  80. $this->writer->writeln('-');
  81. } else {
  82. $this->writer->writeln(Inline::dump($k).':');
  83. }
  84. $this->writer->indent();
  85. if (null !== $v = $this->navigator->accept($v, null, $this)) {
  86. $this->writer
  87. ->rtrim(false)
  88. ->writeln(' '.$v)
  89. ;
  90. }
  91. $this->writer->outdent();
  92. }
  93. if ($count === $this->writer->changeCount && isset($type['params'][1])) {
  94. $this->writer
  95. ->rtrim(false)
  96. ->writeln(' {}')
  97. ;
  98. }
  99. }
  100. public function visitBoolean($data, array $type)
  101. {
  102. $v = $data ? 'true' : 'false';
  103. if ('' === $this->writer->content) {
  104. $this->writer->writeln($v);
  105. }
  106. return $v;
  107. }
  108. public function visitDouble($data, array $type)
  109. {
  110. $v = (string) $data;
  111. if ('' === $this->writer->content) {
  112. $this->writer->writeln($v);
  113. }
  114. return $v;
  115. }
  116. public function visitInteger($data, array $type)
  117. {
  118. $v = (string) $data;
  119. if ('' === $this->writer->content) {
  120. $this->writer->writeln($v);
  121. }
  122. return $v;
  123. }
  124. public function startVisitingObject(ClassMetadata $metadata, $data, array $type)
  125. {
  126. }
  127. public function visitProperty(PropertyMetadata $metadata, $data)
  128. {
  129. $v = (null === $metadata->getter ? $metadata->reflection->getValue($data)
  130. : $data->{$metadata->getter}());
  131. if (null === $v && !$this->shouldSerializeNull()) {
  132. return;
  133. }
  134. $name = $this->namingStrategy->translateName($metadata);
  135. if (!$metadata->inline) {
  136. $this->writer
  137. ->writeln(Inline::dump($name).':')
  138. ->indent();
  139. }
  140. $this->setCurrentMetadata($metadata);
  141. $count = $this->writer->changeCount;
  142. if (null !== $v = $this->navigator->accept($v, $metadata->type, $this)) {
  143. $this->writer
  144. ->rtrim(false)
  145. ->writeln(' '.$v)
  146. ;
  147. } elseif ($count === $this->writer->changeCount) {
  148. $this->writer->revert();
  149. }
  150. if (!$metadata->inline) {
  151. $this->writer->outdent();
  152. }
  153. $this->revertCurrentMetadata();
  154. }
  155. public function endVisitingObject(ClassMetadata $metadata, $data, array $type)
  156. {
  157. }
  158. public function setCurrentMetadata(PropertyMetadata $metadata)
  159. {
  160. $this->metadataStack->push($this->currentMetadata);
  161. $this->currentMetadata = $metadata;
  162. }
  163. public function revertCurrentMetadata()
  164. {
  165. return $this->currentMetadata = $this->metadataStack->pop();
  166. }
  167. public function getNavigator()
  168. {
  169. return $this->navigator;
  170. }
  171. public function getResult()
  172. {
  173. return $this->writer->getContent();
  174. }
  175. }