@@ -67,9 +67,7 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
6767 }
6868
6969 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 );
7371
7472 if (isset ($ value [-2 ]) && "\n" === $ value [-2 ] && "\n" === $ value [-1 ]) {
7573 $ blockChompingIndicator = '+ ' ;
@@ -96,9 +94,7 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
9694 $ output .= sprintf ('%s%s !%s ' , $ prefix , $ dumpAsMap ? Inline::dump ($ key , $ flags ).': ' : '- ' , $ value ->getTag ());
9795
9896 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 ());
10298 $ output .= sprintf (' |%s ' , $ blockIndentationIndicator );
10399
104100 foreach (explode ("\n" , $ value ->getValue ()) as $ row ) {
@@ -143,9 +139,7 @@ private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, i
143139 $ output = sprintf ('%s!%s ' , $ prefix ? $ prefix .' ' : '' , $ value ->getTag ());
144140
145141 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 ());
149143 $ output .= sprintf (' |%s ' , $ blockIndentationIndicator );
150144
151145 foreach (explode ("\n" , $ value ->getValue ()) as $ row ) {
@@ -161,4 +155,20 @@ private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, i
161155
162156 return $ output ."\n" .$ this ->dump ($ value ->getValue (), $ inline - 1 , $ indent , $ flags );
163157 }
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+ }
164174}
0 commit comments