2020 */
2121class Parser
2222{
23- const FOLDED_SCALAR_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)? ' ;
23+ const BLOCK_SCALAR_HEADER_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)? ' ;
24+ // BC - wrongly named
25+ const FOLDED_SCALAR_PATTERN = self ::BLOCK_SCALAR_HEADER_PATTERN ;
2426
2527 private $ offset = 0 ;
2628 private $ lines = array ();
@@ -337,8 +339,8 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false)
337339
338340 $ isItUnindentedCollection = $ this ->isStringUnIndentedCollectionItem ($ this ->currentLine );
339341
340- // Comments must not be removed inside a string block (ie. after a line ending with "|")
341- $ removeCommentsPattern = '~ ' .self ::FOLDED_SCALAR_PATTERN .'$~ ' ;
342+ // Comments must not be removed inside a block scalar
343+ $ removeCommentsPattern = '~ ' .self ::BLOCK_SCALAR_HEADER_PATTERN .'$~ ' ;
342344 $ removeComments = !preg_match ($ removeCommentsPattern , $ this ->currentLine );
343345
344346 while ($ this ->moveToNextLine ()) {
@@ -427,10 +429,10 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
427429 return $ this ->refs [$ value ];
428430 }
429431
430- if (preg_match ('/^ ' .self ::FOLDED_SCALAR_PATTERN .'$/ ' , $ value , $ matches )) {
432+ if (preg_match ('/^ ' .self ::BLOCK_SCALAR_HEADER_PATTERN .'$/ ' , $ value , $ matches )) {
431433 $ modifiers = isset ($ matches ['modifiers ' ]) ? $ matches ['modifiers ' ] : '' ;
432434
433- return $ this ->parseFoldedScalar ($ matches ['separator ' ], preg_replace ('#\d+# ' , '' , $ modifiers ), (int ) abs ($ modifiers ));
435+ return $ this ->parseBlockScalar ($ matches ['separator ' ], preg_replace ('#\d+# ' , '' , $ modifiers ), (int ) abs ($ modifiers ));
434436 }
435437
436438 try {
@@ -444,15 +446,15 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
444446 }
445447
446448 /**
447- * Parses a folded scalar.
449+ * Parses a block scalar.
448450 *
449- * @param string $separator The separator that was used to begin this folded scalar (| or >)
450- * @param string $indicator The indicator that was used to begin this folded scalar (+ or -)
451- * @param int $indentation The indentation that was used to begin this folded scalar
451+ * @param string $style The style indicator that was used to begin this block scalar (| or >)
452+ * @param string $chomping The chomping indicator that was used to begin this block scalar (+ or -)
453+ * @param int $indentation The indentation indicator that was used to begin this block scalar
452454 *
453455 * @return string The text value
454456 */
455- private function parseFoldedScalar ( $ separator , $ indicator = '' , $ indentation = 0 )
457+ private function parseBlockScalar ( $ style , $ chomping = '' , $ indentation = 0 )
456458 {
457459 $ notEOF = $ this ->moveToNextLine ();
458460 if (!$ notEOF ) {
@@ -507,17 +509,23 @@ private function parseFoldedScalar($separator, $indicator = '', $indentation = 0
507509 $ this ->moveToPreviousLine ();
508510 }
509511
510- // replace all non-trailing single newlines with spaces in folded blocks
511- if ('> ' === $ separator ) {
512+ // folded style
513+ if ('> ' === $ style ) {
514+ // folded lines
515+ // replace all non-leading/non-trailing single newlines with spaces
512516 preg_match ('/(\n*)$/ ' , $ text , $ matches );
513- $ text = preg_replace ('/(?<!\n)\n(?!\n)/ ' , ' ' , rtrim ($ text , "\n" ));
517+ $ text = preg_replace ('/(?<!\n|^ )\n(?!\n)/ ' , ' ' , rtrim ($ text , "\n" ));
514518 $ text .= $ matches [1 ];
519+
520+ // empty separation lines
521+ // remove one newline from each group of non-leading/non-trailing newlines
522+ $ text = preg_replace ('/[^\n]\n+\K\n(?=[^\n])/ ' , '' , $ text );
515523 }
516524
517- // deal with trailing newlines as indicated
518- if ('' === $ indicator ) {
525+ // deal with trailing newlines
526+ if ('' === $ chomping ) {
519527 $ text = preg_replace ('/\n+$/ ' , "\n" , $ text );
520- } elseif ('- ' === $ indicator ) {
528+ } elseif ('- ' === $ chomping ) {
521529 $ text = preg_replace ('/\n+$/ ' , '' , $ text );
522530 }
523531
0 commit comments