@@ -71,9 +71,7 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
7171 }
7272
7373 if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $ flags && \is_string ($ value ) && str_contains ($ value , "\n" ) && !str_contains ($ value , "\r" )) {
74- // If the first line starts with a space character, the spec requires a blockIndicationIndicator
75- // http://www.yaml.org/spec/1.2/spec.html#id2793979
76- $ blockIndentationIndicator = str_starts_with ($ value , ' ' ) ? (string ) $ this ->indentation : '' ;
74+ $ blockIndentationIndicator = $ this ->getBlockIndentationIndicator ($ value );
7775
7876 if (isset ($ value [-2 ]) && "\n" === $ value [-2 ] && "\n" === $ value [-1 ]) {
7977 $ blockChompingIndicator = '+ ' ;
@@ -100,9 +98,7 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
10098 $ output .= sprintf ('%s%s !%s ' , $ prefix , $ dumpAsMap ? Inline::dump ($ key , $ flags ).': ' : '- ' , $ value ->getTag ());
10199
102100 if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $ flags && \is_string ($ value ->getValue ()) && str_contains ($ value ->getValue (), "\n" ) && !str_contains ($ value ->getValue (), "\r\n" )) {
103- // If the first line starts with a space character, the spec requires a blockIndicationIndicator
104- // http://www.yaml.org/spec/1.2/spec.html#id2793979
105- $ blockIndentationIndicator = str_starts_with ($ value ->getValue (), ' ' ) ? (string ) $ this ->indentation : '' ;
101+ $ blockIndentationIndicator = $ this ->getBlockIndentationIndicator ($ value ->getValue ());
106102 $ output .= sprintf (' |%s ' , $ blockIndentationIndicator );
107103
108104 foreach (explode ("\n" , $ value ->getValue ()) as $ row ) {
@@ -147,9 +143,7 @@ private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, i
147143 $ output = sprintf ('%s!%s ' , $ prefix ? $ prefix .' ' : '' , $ value ->getTag ());
148144
149145 if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $ flags && \is_string ($ value ->getValue ()) && str_contains ($ value ->getValue (), "\n" ) && !str_contains ($ value ->getValue (), "\r\n" )) {
150- // If the first line starts with a space character, the spec requires a blockIndicationIndicator
151- // http://www.yaml.org/spec/1.2/spec.html#id2793979
152- $ blockIndentationIndicator = (' ' === substr ($ value ->getValue (), 0 , 1 )) ? (string ) $ this ->indentation : '' ;
146+ $ blockIndentationIndicator = $ this ->getBlockIndentationIndicator ($ value ->getValue ());
153147 $ output .= sprintf (' |%s ' , $ blockIndentationIndicator );
154148
155149 foreach (explode ("\n" , $ value ->getValue ()) as $ row ) {
@@ -165,4 +159,20 @@ private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, i
165159
166160 return $ output ."\n" .$ this ->dump ($ value ->getValue (), $ inline - 1 , $ indent , $ flags );
167161 }
162+
163+ private function getBlockIndentationIndicator (string $ value ): string
164+ {
165+ $ lines = explode ("\n" , $ value );
166+
167+ // If the first line (that is neither empty nor contains only spaces)
168+ // starts with a space character, the spec requires a block indentation indicator
169+ // http://www.yaml.org/spec/1.2/spec.html#id2793979
170+ foreach ($ lines as $ line ) {
171+ if ('' !== trim ($ line , ' ' )) {
172+ return (' ' === substr ($ line , 0 , 1 )) ? (string ) $ this ->indentation : '' ;
173+ }
174+ }
175+
176+ return '' ;
177+ }
168178}
0 commit comments