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 * The amount of spaces to use for indentation of nested nodes.
27 *
28 * @var int
29 */
30 protected $indentation;
31
32 public function __construct(int $indentation = 4)
33 {
34 if ($indentation < 1) {
35 throw new \InvalidArgumentException('The indentation must be greater than zero.');
36 }
37
38 $this->indentation = $indentation;
39 }
40
41 /**
42 * Dumps a PHP value to YAML.
43 *
44 * @param mixed $input The PHP value
45 * @param int $inline The level where you switch to inline YAML
46 * @param int $indent The level of indentation (used internally)
47 * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
48 */
49 public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0): string
50 {
51 $output = '';
52 $prefix = $indent ? str_repeat(' ', $indent) : '';
53 $dumpObjectAsInlineMap = true;
54
55 if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) {
56 $dumpObjectAsInlineMap = empty((array) $input);
57 }
58
59 if ($inline <= 0 || (!\is_array($input) && !$input instanceof TaggedValue && $dumpObjectAsInlineMap) || empty($input)) {
60 $output .= $prefix.Inline::dump($input, $flags);
61 } elseif ($input instanceof TaggedValue) {
62 $output .= $this->dumpTaggedValue($input, $inline, $indent, $flags, $prefix);
63 } else {
64 $dumpAsMap = Inline::isHash($input);
65
66 foreach ($input as $key => $value) {
67 if ('' !== $output && "\n" !== $output[-1]) {
68 $output .= "\n";
69 }
70
71 if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r")) {
72 $blockIndentationIndicator = $this->getBlockIndentationIndicator($value);
73
74 if (isset($value[-2]) && "\n" === $value[-2] && "\n" === $value[-1]) {
75 $blockChompingIndicator = '+';
76 } elseif ("\n" === $value[-1]) {
77 $blockChompingIndicator = '';
78 } else {
79 $blockChompingIndicator = '-';
80 }
81
82 $output .= sprintf('%s%s%s |%s%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator, $blockChompingIndicator);
83
84 foreach (explode("\n", $value) as $row) {
85 if ('' === $row) {
86 $output .= "\n";
87 } else {
88 $output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
89 }
90 }
91
92 continue;
93 }
94
95 if ($value instanceof TaggedValue) {
96 $output .= sprintf('%s%s !%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', $value->getTag());
97
98 if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && false !== strpos($value->getValue(), "\n") && false === strpos($value->getValue(), "\r\n")) {
99 $blockIndentationIndicator = $this->getBlockIndentationIndicator($value->getValue());
100 $output .= sprintf(' |%s', $blockIndentationIndicator);
101
102 foreach (explode("\n", $value->getValue()) as $row) {
103 $output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
104 }
105
106 continue;
107 }
108
109 if ($inline - 1 <= 0 || null === $value->getValue() || \is_scalar($value->getValue())) {
110 $output .= ' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
111 } else {
112 $output .= "\n";
113 $output .= $this->dump($value->getValue(), $inline - 1, $dumpAsMap ? $indent + $this->indentation : $indent + 2, $flags);
114 }
115
116 continue;
117 }
118
119 $dumpObjectAsInlineMap = true;
120
121 if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
122 $dumpObjectAsInlineMap = empty((array) $value);
123 }
124
125 $willBeInlined = $inline - 1 <= 0 || !\is_array($value) && $dumpObjectAsInlineMap || empty($value);
126
127 $output .= sprintf('%s%s%s%s',
128 $prefix,
129 $dumpAsMap ? Inline::dump($key, $flags).':' : '-',
130 $willBeInlined ? ' ' : "\n",
131 $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
132 ).($willBeInlined ? "\n" : '');
133 }
134 }
135
136 return $output;
137 }
138
139 private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, int $flags, string $prefix): string
140 {
141 $output = sprintf('%s!%s', $prefix ? $prefix.' ' : '', $value->getTag());
142
143 if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && false !== strpos($value->getValue(), "\n") && false === strpos($value->getValue(), "\r\n")) {
144 $blockIndentationIndicator = $this->getBlockIndentationIndicator($value->getValue());
145 $output .= sprintf(' |%s', $blockIndentationIndicator);
146
147 foreach (explode("\n", $value->getValue()) as $row) {
148 $output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
149 }
150
151 return $output;
152 }
153
154 if ($inline - 1 <= 0 || null === $value->getValue() || \is_scalar($value->getValue())) {
155 return $output.' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
156 }
157
158 return $output."\n".$this->dump($value->getValue(), $inline - 1, $indent, $flags);
159 }
160
161 private function getBlockIndentationIndicator(string $value): string
162 {
163 $lines = explode("\n", $value);
164
165 // If the first line (that is neither empty nor contains only spaces)
166 // starts with a space character, the spec requires a block indentation indicator
167 // http://www.yaml.org/spec/1.2/spec.html#id2793979
168 foreach ($lines as $line) {
169 if ('' !== trim($line, ' ')) {
170 return (' ' === substr($line, 0, 1)) ? (string) $this->indentation : '';
171 }
172 }
173
174 return '';
175 }
176 }