@@ -130,7 +130,7 @@ private function updateData(array $newData)
130130 $ this ->arrayTypeForDepths [$ this ->depth ] = $ this ->isHash ($ currentData ) ? self ::ARRAY_TYPE_HASH : self ::ARRAY_TYPE_SEQUENCE ;
131131
132132 $ this ->log (sprintf (
133- 'Array type: %s, format: %s ' ,
133+ 'Changing array type & format via updateData() ' ,
134134 $ this ->arrayTypeForDepths [$ this ->depth ],
135135 $ this ->arrayFormatForDepths [$ this ->depth ]
136136 ));
@@ -333,12 +333,7 @@ private function addNewKeyToYaml($key, $value)
333333 // because we're inside a multi-line array, put this item
334334 // onto the *next* line & indent it
335335
336- // But, if the *value* is an array, then ITS children will
337- // also need to be indented artificially by the same amount
338- $ newYamlValue = str_replace ("\n" , "\n" .$ this ->getCurrentIndentation (), $ newYamlValue );
339-
340- // now add the new line & indentation to the top-level
341- $ newYamlValue = "\n" .$ this ->getCurrentIndentation ().$ newYamlValue ;
336+ $ newYamlValue = "\n" .$ this ->indentMultilineYamlArray ($ newYamlValue );
342337 } else {
343338 if ($ firstItemInArray ) {
344339 // avoid the starting "," if first item in array
@@ -454,13 +449,23 @@ private function changeValueInYaml($value)
454449
455450 $ endValuePosition = $ this ->findEndPositionOfValue ($ originalVal );
456451
457- // empty space between key & value
458- $ newDataString = ' ' .$ this ->convertToYaml ($ value );
452+ $ newYamlValue = $ this ->convertToYaml ($ value );
453+ if (!is_array ($ originalVal ) && is_array ($ value )) {
454+ // we're converting from a scalar to a (multiline) array
455+ // this means we need to break onto the next line
456+
457+ // increase the indentation
458+ $ this ->manuallyIncrementIndentation ();
459+ $ newYamlValue = "\n" .$ this ->indentMultilineYamlArray ($ newYamlValue );
460+ } else {
461+ // empty space between key & value
462+ $ newYamlValue = ' ' .$ newYamlValue ;
463+ }
459464 $ newContents = substr ($ this ->contents , 0 , $ this ->currentPosition )
460- .$ newDataString
465+ .$ newYamlValue
461466 .substr ($ this ->contents , $ endValuePosition );
462467
463- $ newPosition = $ this ->currentPosition + \strlen ($ newDataString );
468+ $ newPosition = $ this ->currentPosition + \strlen ($ newYamlValue );
464469
465470 $ newData = $ this ->currentData ;
466471 $ newData = $ this ->setValueAtCurrentPath ($ value , $ newData );
@@ -844,6 +849,8 @@ private function log(string $message, $includeContent = false)
844849 'depth ' => $ this ->depth ,
845850 'position ' => $ this ->currentPosition ,
846851 'indentation ' => $ this ->indentationForDepths [$ this ->depth ],
852+ 'type ' => $ this ->arrayTypeForDepths [$ this ->depth ],
853+ 'format ' => $ this ->arrayFormatForDepths [$ this ->depth ],
847854 ];
848855
849856 if ($ includeContent ) {
@@ -1123,4 +1130,21 @@ private function isCharLineBreak(string $char): bool
11231130 {
11241131 return "\n" === $ char || "\r" === $ char ;
11251132 }
1133+
1134+ /**
1135+ * Takes an unindented multi-line YAML string and indents it so
1136+ * it can be inserted into the current position.
1137+ *
1138+ * Usually an empty line needs to be prepended to this result before
1139+ * adding to the content.
1140+ */
1141+ private function indentMultilineYamlArray (string $ yaml ): string
1142+ {
1143+ // But, if the *value* is an array, then ITS children will
1144+ // also need to be indented artificially by the same amount
1145+ $ yaml = str_replace ("\n" , "\n" .$ this ->getCurrentIndentation (), $ yaml );
1146+
1147+ // now indent this level
1148+ return $ this ->getCurrentIndentation ().$ yaml ;
1149+ }
11261150}
0 commit comments