YamlSerializationVisitor.php 5.2 KB

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