YamlSerializationVisitor.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. public function visitArray($data, array $type)
  68. {
  69. $count = $this->writer->changeCount;
  70. $isList = array_keys($data) === range(0, count($data) - 1);
  71. foreach ($data as $k => $v) {
  72. if (null === $v && (!is_string($k) || !$this->shouldSerializeNull())) {
  73. continue;
  74. }
  75. if ($isList) {
  76. $this->writer->writeln('-');
  77. } else {
  78. $this->writer->writeln(Inline::dump($k).':');
  79. }
  80. $this->writer->indent();
  81. if (null !== $v = $this->navigator->accept($v, null, $this)) {
  82. $this->writer
  83. ->rtrim(false)
  84. ->writeln(' '.$v)
  85. ;
  86. }
  87. $this->writer->outdent();
  88. }
  89. if ($count === $this->writer->changeCount && isset($type['params'][1])) {
  90. $this->writer
  91. ->rtrim(false)
  92. ->writeln(' {}')
  93. ;
  94. }
  95. }
  96. public function visitBoolean($data, array $type)
  97. {
  98. $v = $data ? 'true' : 'false';
  99. if ('' === $this->writer->content) {
  100. $this->writer->writeln($v);
  101. }
  102. return $v;
  103. }
  104. public function visitDouble($data, array $type)
  105. {
  106. $v = (string) $data;
  107. if ('' === $this->writer->content) {
  108. $this->writer->writeln($v);
  109. }
  110. return $v;
  111. }
  112. public function visitInteger($data, array $type)
  113. {
  114. $v = (string) $data;
  115. if ('' === $this->writer->content) {
  116. $this->writer->writeln($v);
  117. }
  118. return $v;
  119. }
  120. public function startVisitingObject(ClassMetadata $metadata, $data, array $type)
  121. {
  122. }
  123. public function visitProperty(PropertyMetadata $metadata, $data)
  124. {
  125. $v = (null === $metadata->getter ? $metadata->reflection->getValue($data)
  126. : $data->{$metadata->getter}());
  127. if (null === $v && !$this->shouldSerializeNull()) {
  128. return;
  129. }
  130. $name = $this->namingStrategy->translateName($metadata);
  131. if (!$metadata->inline) {
  132. $this->writer
  133. ->writeln(Inline::dump($name).':')
  134. ->indent();
  135. }
  136. $this->setCurrentMetadata($metadata);
  137. $count = $this->writer->changeCount;
  138. if (null !== $v = $this->navigator->accept($v, $metadata->type, $this)) {
  139. $this->writer
  140. ->rtrim(false)
  141. ->writeln(' '.$v)
  142. ;
  143. } else if ($count === $this->writer->changeCount) {
  144. $this->writer->revert();
  145. }
  146. if (!$metadata->inline) {
  147. $this->writer->outdent();
  148. }
  149. $this->revertCurrentMetadata();
  150. }
  151. public function endVisitingObject(ClassMetadata $metadata, $data, array $type)
  152. {
  153. }
  154. public function setCurrentMetadata(PropertyMetadata $metadata)
  155. {
  156. $this->metadataStack->push($this->currentMetadata);
  157. $this->currentMetadata = $metadata;
  158. }
  159. public function revertCurrentMetadata()
  160. {
  161. return $this->currentMetadata = $this->metadataStack->pop();
  162. }
  163. public function getNavigator()
  164. {
  165. return $this->navigator;
  166. }
  167. public function getResult()
  168. {
  169. return $this->writer->getContent();
  170. }
  171. }