@@ -67,9 +67,7 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
67
67
}
68
68
69
69
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $ flags && \is_string ($ value ) && str_contains ($ value , "\n" ) && !str_contains ($ value , "\r" )) {
70
- // If the first line starts with a space character, the spec requires a blockIndicationIndicator
71
- // http://www.yaml.org/spec/1.2/spec.html#id2793979
72
- $ blockIndentationIndicator = str_starts_with ($ value , ' ' ) ? (string ) $ this ->indentation : '' ;
70
+ $ blockIndentationIndicator = $ this ->getBlockIndentationIndicator ($ value );
73
71
74
72
if (isset ($ value [-2 ]) && "\n" === $ value [-2 ] && "\n" === $ value [-1 ]) {
75
73
$ blockChompingIndicator = '+ ' ;
@@ -96,9 +94,7 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
96
94
$ output .= sprintf ('%s%s !%s ' , $ prefix , $ dumpAsMap ? Inline::dump ($ key , $ flags ).': ' : '- ' , $ value ->getTag ());
97
95
98
96
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $ flags && \is_string ($ value ->getValue ()) && str_contains ($ value ->getValue (), "\n" ) && !str_contains ($ value ->getValue (), "\r\n" )) {
99
- // If the first line starts with a space character, the spec requires a blockIndicationIndicator
100
- // http://www.yaml.org/spec/1.2/spec.html#id2793979
101
- $ blockIndentationIndicator = str_starts_with ($ value ->getValue (), ' ' ) ? (string ) $ this ->indentation : '' ;
97
+ $ blockIndentationIndicator = $ this ->getBlockIndentationIndicator ($ value ->getValue ());
102
98
$ output .= sprintf (' |%s ' , $ blockIndentationIndicator );
103
99
104
100
foreach (explode ("\n" , $ value ->getValue ()) as $ row ) {
@@ -143,9 +139,7 @@ private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, i
143
139
$ output = sprintf ('%s!%s ' , $ prefix ? $ prefix .' ' : '' , $ value ->getTag ());
144
140
145
141
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $ flags && \is_string ($ value ->getValue ()) && str_contains ($ value ->getValue (), "\n" ) && !str_contains ($ value ->getValue (), "\r\n" )) {
146
- // If the first line starts with a space character, the spec requires a blockIndicationIndicator
147
- // http://www.yaml.org/spec/1.2/spec.html#id2793979
148
- $ blockIndentationIndicator = (' ' === substr ($ value ->getValue (), 0 , 1 )) ? (string ) $ this ->indentation : '' ;
142
+ $ blockIndentationIndicator = $ this ->getBlockIndentationIndicator ($ value ->getValue ());
149
143
$ output .= sprintf (' |%s ' , $ blockIndentationIndicator );
150
144
151
145
foreach (explode ("\n" , $ value ->getValue ()) as $ row ) {
@@ -161,4 +155,20 @@ private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, i
161
155
162
156
return $ output ."\n" .$ this ->dump ($ value ->getValue (), $ inline - 1 , $ indent , $ flags );
163
157
}
158
+
159
+ private function getBlockIndentationIndicator (string $ value ): string
160
+ {
161
+ $ lines = explode ("\n" , $ value );
162
+
163
+ // If the first line (that is neither empty nor contains only spaces)
164
+ // starts with a space character, the spec requires a block indentation indicator
165
+ // http://www.yaml.org/spec/1.2/spec.html#id2793979
166
+ foreach ($ lines as $ line ) {
167
+ if ('' !== trim ($ line , ' ' )) {
168
+ return (' ' === substr ($ line , 0 , 1 )) ? (string ) $ this ->indentation : '' ;
169
+ }
170
+ }
171
+
172
+ return '' ;
173
+ }
164
174
}
0 commit comments