@@ -69,9 +69,7 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):
69
69
}
70
70
71
71
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $ flags && \is_string ($ value ) && false !== strpos ($ value , "\n" ) && false === strpos ($ value , "\r" )) {
72
- // If the first line starts with a space character, the spec requires a blockIndicationIndicator
73
- // http://www.yaml.org/spec/1.2/spec.html#id2793979
74
- $ blockIndentationIndicator = (' ' === substr ($ value , 0 , 1 )) ? (string ) $ this ->indentation : '' ;
72
+ $ blockIndentationIndicator = $ this ->getBlockIndentationIndicator ($ value );
75
73
76
74
if (isset ($ value [-2 ]) && "\n" === $ value [-2 ] && "\n" === $ value [-1 ]) {
77
75
$ blockChompingIndicator = '+ ' ;
@@ -98,9 +96,7 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):
98
96
$ output .= sprintf ('%s%s !%s ' , $ prefix , $ dumpAsMap ? Inline::dump ($ key , $ flags ).': ' : '- ' , $ value ->getTag ());
99
97
100
98
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $ flags && \is_string ($ value ->getValue ()) && false !== strpos ($ value ->getValue (), "\n" ) && false === strpos ($ value ->getValue (), "\r\n" )) {
101
- // If the first line starts with a space character, the spec requires a blockIndicationIndicator
102
- // http://www.yaml.org/spec/1.2/spec.html#id2793979
103
- $ blockIndentationIndicator = (' ' === substr ($ value ->getValue (), 0 , 1 )) ? (string ) $ this ->indentation : '' ;
99
+ $ blockIndentationIndicator = $ this ->getBlockIndentationIndicator ($ value ->getValue ());
104
100
$ output .= sprintf (' |%s ' , $ blockIndentationIndicator );
105
101
106
102
foreach (explode ("\n" , $ value ->getValue ()) as $ row ) {
@@ -145,9 +141,7 @@ private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, i
145
141
$ output = sprintf ('%s!%s ' , $ prefix ? $ prefix .' ' : '' , $ value ->getTag ());
146
142
147
143
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $ flags && \is_string ($ value ->getValue ()) && false !== strpos ($ value ->getValue (), "\n" ) && false === strpos ($ value ->getValue (), "\r\n" )) {
148
- // If the first line starts with a space character, the spec requires a blockIndicationIndicator
149
- // http://www.yaml.org/spec/1.2/spec.html#id2793979
150
- $ blockIndentationIndicator = (' ' === substr ($ value ->getValue (), 0 , 1 )) ? (string ) $ this ->indentation : '' ;
144
+ $ blockIndentationIndicator = $ this ->getBlockIndentationIndicator ($ value ->getValue ());
151
145
$ output .= sprintf (' |%s ' , $ blockIndentationIndicator );
152
146
153
147
foreach (explode ("\n" , $ value ->getValue ()) as $ row ) {
@@ -163,4 +157,20 @@ private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, i
163
157
164
158
return $ output ."\n" .$ this ->dump ($ value ->getValue (), $ inline - 1 , $ indent , $ flags );
165
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
+ }
166
176
}
0 commit comments