Debian Codegraph @

vendor/symfony/yaml/Dumper.php

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