YamlSerializationVisitor.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 Symfony\Component\Yaml\Inline;
  19. use JMS\Serializer\Metadata\PropertyMetadata;
  20. use JMS\Serializer\Naming\PropertyNamingStrategyInterface;
  21. use JMS\Serializer\Metadata\ClassMetadata;
  22. use JMS\Serializer\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 setNavigator(GraphNavigator $navigator)
  42. {
  43. $this->navigator = $navigator;
  44. $this->writer->reset();
  45. $this->stack = new \SplStack;
  46. $this->metadataStack = new \SplStack;
  47. }
  48. public function visitNull($data, array $type, Context $context)
  49. {
  50. if ('' === $this->writer->content) {
  51. $this->writer->writeln('null');
  52. }
  53. return 'null';
  54. }
  55. public function visitString($data, array $type, Context $context)
  56. {
  57. $v = Inline::dump($data);
  58. if ('' === $this->writer->content) {
  59. $this->writer->writeln($v);
  60. }
  61. return $v;
  62. }
  63. /**
  64. * @param array $data
  65. * @param array $type
  66. */
  67. public function visitArray($data, array $type, Context $context)
  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) || ! $context->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. $typeArray = isset($type['params'][0]) ? (isset($type['params'][1]) && is_array($type['params'][1]) ? $type['params'][1] : $type['params'][0]) : null;
  82. if (null !== $v = $this->navigator->accept($v, $typeArray, $context)) {
  83. $this->writer
  84. ->rtrim(false)
  85. ->writeln(' '.$v)
  86. ;
  87. }
  88. $this->writer->outdent();
  89. }
  90. if ($count === $this->writer->changeCount && isset($type['params'][1])) {
  91. $this->writer
  92. ->rtrim(false)
  93. ->writeln(' {}')
  94. ;
  95. }
  96. }
  97. public function visitBoolean($data, array $type, Context $context)
  98. {
  99. $v = $data ? 'true' : 'false';
  100. if ('' === $this->writer->content) {
  101. $this->writer->writeln($v);
  102. }
  103. return $v;
  104. }
  105. public function visitDouble($data, array $type, Context $context)
  106. {
  107. $v = (string) $data;
  108. if ('' === $this->writer->content) {
  109. $this->writer->writeln($v);
  110. }
  111. return $v;
  112. }
  113. public function visitInteger($data, array $type, Context $context)
  114. {
  115. $v = (string) $data;
  116. if ('' === $this->writer->content) {
  117. $this->writer->writeln($v);
  118. }
  119. return $v;
  120. }
  121. public function startVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context)
  122. {
  123. }
  124. public function visitProperty(PropertyMetadata $metadata, $data, Context $context)
  125. {
  126. $v = $metadata->getValue($data);
  127. if (null === $v && !$context->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, $context)) {
  139. $this->writer
  140. ->rtrim(false)
  141. ->writeln(' '.$v)
  142. ;
  143. } elseif ($count === $this->writer->changeCount && !$metadata->inline) {
  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, Context $context)
  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. }