@@ -71,9 +71,7 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
71
71
}
72
72
73
73
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 );
77
75
78
76
if (isset ($ value [-2 ]) && "\n" === $ value [-2 ] && "\n" === $ value [-1 ]) {
79
77
$ blockChompingIndicator = '+ ' ;
@@ -100,9 +98,7 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
100
98
$ output .= sprintf ('%s%s !%s ' , $ prefix , $ dumpAsMap ? Inline::dump ($ key , $ flags ).': ' : '- ' , $ value ->getTag ());
101
99
102
100
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 ());
106
102
$ output .= sprintf (' |%s ' , $ blockIndentationIndicator );
107
103
108
104
foreach (explode ("\n" , $ value ->getValue ()) as $ row ) {
@@ -147,9 +143,7 @@ private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, i
147
143
$ output = sprintf ('%s!%s ' , $ prefix ? $ prefix .' ' : '' , $ value ->getTag ());
148
144
149
145
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 ());
153
147
$ output .= sprintf (' |%s ' , $ blockIndentationIndicator );
154
148
155
149
foreach (explode ("\n" , $ value ->getValue ()) as $ row ) {
@@ -165,4 +159,20 @@ private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, i
165
159
166
160
return $ output ."\n" .$ this ->dump ($ value ->getValue (), $ inline - 1 , $ indent , $ flags );
167
161
}
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
+ }
168
178
}
0 commit comments