Dumper.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Yaml;
  11. use Symfony\Component\Yaml\Tag\TaggedValue;
  12. /**
  13. * Dumper dumps PHP variables to YAML strings.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. *
  17. * @final
  18. */
  19. class Dumper
  20. {
  21. /**
  22. * @param int $indentation The amount of spaces to use for indentation of nested nodes
  23. */
  24. public function __construct(private int $indentation = 4)
  25. {
  26. if ($indentation < 1) {
  27. throw new \InvalidArgumentException('The indentation must be greater than zero.');
  28. }
  29. }
  30. /**
  31. * Dumps a PHP value to YAML.
  32. *
  33. * @param mixed $input The PHP value
  34. * @param int $inline The level where you switch to inline YAML
  35. * @param int $indent The level of indentation (used internally)
  36. * @param int-mask-of<Yaml::DUMP_*> $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
  37. */
  38. public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags = 0): string
  39. {
  40. $output = '';
  41. $prefix = $indent ? str_repeat(' ', $indent) : '';
  42. $dumpObjectAsInlineMap = true;
  43. if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) {
  44. $dumpObjectAsInlineMap = !(array) $input;
  45. }
  46. if ($inline <= 0 || (!\is_array($input) && !$input instanceof TaggedValue && $dumpObjectAsInlineMap) || !$input) {
  47. $output .= $prefix.Inline::dump($input, $flags);
  48. } elseif ($input instanceof TaggedValue) {
  49. $output .= $this->dumpTaggedValue($input, $inline, $indent, $flags, $prefix);
  50. } else {
  51. $dumpAsMap = Inline::isHash($input);
  52. foreach ($input as $key => $value) {
  53. if ('' !== $output && "\n" !== $output[-1]) {
  54. $output .= "\n";
  55. }
  56. if (\is_int($key) && Yaml::DUMP_NUMERIC_KEY_AS_STRING & $flags) {
  57. $key = (string) $key;
  58. }
  59. if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && str_contains($value, "\n") && !str_contains($value, "\r")) {
  60. $blockIndentationIndicator = $this->getBlockIndentationIndicator($value);
  61. if (isset($value[-2]) && "\n" === $value[-2] && "\n" === $value[-1]) {
  62. $blockChompingIndicator = '+';
  63. } elseif ("\n" === $value[-1]) {
  64. $blockChompingIndicator = '';
  65. } else {
  66. $blockChompingIndicator = '-';
  67. }
  68. $output .= \sprintf('%s%s%s |%s%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator, $blockChompingIndicator);
  69. foreach (explode("\n", $value) as $row) {
  70. if ('' === $row) {
  71. $output .= "\n";
  72. } else {
  73. $output .= \sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
  74. }
  75. }
  76. continue;
  77. }
  78. if ($value instanceof TaggedValue) {
  79. $output .= \sprintf('%s%s !%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', $value->getTag());
  80. if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && str_contains($value->getValue(), "\n") && !str_contains($value->getValue(), "\r\n")) {
  81. $blockIndentationIndicator = $this->getBlockIndentationIndicator($value->getValue());
  82. $output .= \sprintf(' |%s', $blockIndentationIndicator);
  83. foreach (explode("\n", $value->getValue()) as $row) {
  84. $output .= \sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
  85. }
  86. continue;
  87. }
  88. if ($inline - 1 <= 0 || null === $value->getValue() || \is_scalar($value->getValue())) {
  89. $output .= ' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
  90. } else {
  91. $output .= "\n";
  92. $output .= $this->dump($value->getValue(), $inline - 1, $dumpAsMap ? $indent + $this->indentation : $indent + 2, $flags);
  93. }
  94. continue;
  95. }
  96. $dumpObjectAsInlineMap = true;
  97. if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
  98. $dumpObjectAsInlineMap = !(array) $value;
  99. }
  100. $willBeInlined = $inline - 1 <= 0 || !\is_array($value) && $dumpObjectAsInlineMap || !$value;
  101. $output .= \sprintf('%s%s%s%s',
  102. $prefix,
  103. $dumpAsMap ? Inline::dump($key, $flags).':' : '-',
  104. $willBeInlined ? ' ' : "\n",
  105. $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
  106. ).($willBeInlined ? "\n" : '');
  107. }
  108. }
  109. return $output;
  110. }
  111. private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, int $flags, string $prefix): string
  112. {
  113. $output = \sprintf('%s!%s', $prefix ? $prefix.' ' : '', $value->getTag());
  114. if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && str_contains($value->getValue(), "\n") && !str_contains($value->getValue(), "\r\n")) {
  115. $blockIndentationIndicator = $this->getBlockIndentationIndicator($value->getValue());
  116. $output .= \sprintf(' |%s', $blockIndentationIndicator);
  117. foreach (explode("\n", $value->getValue()) as $row) {
  118. $output .= \sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
  119. }
  120. return $output;
  121. }
  122. if ($inline - 1 <= 0 || null === $value->getValue() || \is_scalar($value->getValue())) {
  123. return $output.' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
  124. }
  125. return $output."\n".$this->dump($value->getValue(), $inline - 1, $indent, $flags);
  126. }
  127. private function getBlockIndentationIndicator(string $value): string
  128. {
  129. $lines = explode("\n", $value);
  130. // If the first line (that is neither empty nor contains only spaces)
  131. // starts with a space character, the spec requires a block indentation indicator
  132. // http://www.yaml.org/spec/1.2/spec.html#id2793979
  133. foreach ($lines as $line) {
  134. if ('' !== trim($line, ' ')) {
  135. return str_starts_with($line, ' ') ? (string) $this->indentation : '';
  136. }
  137. }
  138. return '';
  139. }
  140. }