YamlSerializationVisitor.php 5.1 KB

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