@@ -240,6 +240,10 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
240
240
if ($ isRef ) {
241
241
$ this ->refs [$ isRef ] = $ data [$ key ];
242
242
}
243
+
244
+ if ($ objectForMap && !is_object ($ data )) {
245
+ $ data = (object ) $ data ;
246
+ }
243
247
} else {
244
248
// multiple documents are not supported
245
249
if ('--- ' === $ this ->currentLine ) {
@@ -515,13 +519,13 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
515
519
}
516
520
517
521
$ isCurrentLineBlank = $ this ->isCurrentLineBlank ();
518
- $ text = '' ;
522
+ $ blockLines = array () ;
519
523
520
524
// leading blank lines are consumed before determining indentation
521
525
while ($ notEOF && $ isCurrentLineBlank ) {
522
526
// newline only if not EOF
523
527
if ($ notEOF = $ this ->moveToNextLine ()) {
524
- $ text .= "\n" ;
528
+ $ blockLines [] = '' ;
525
529
$ isCurrentLineBlank = $ this ->isCurrentLineBlank ();
526
530
}
527
531
}
@@ -542,37 +546,59 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
542
546
preg_match ($ pattern , $ this ->currentLine , $ matches )
543
547
)
544
548
) {
545
- if ($ isCurrentLineBlank ) {
546
- $ text .= substr ($ this ->currentLine , $ indentation );
549
+ if ($ isCurrentLineBlank && strlen ($ this ->currentLine ) > $ indentation ) {
550
+ $ blockLines [] = substr ($ this ->currentLine , $ indentation );
551
+ } elseif ($ isCurrentLineBlank ) {
552
+ $ blockLines [] = '' ;
547
553
} else {
548
- $ text . = $ matches [1 ];
554
+ $ blockLines [] = $ matches [1 ];
549
555
}
550
556
551
557
// newline only if not EOF
552
558
if ($ notEOF = $ this ->moveToNextLine ()) {
553
- $ text .= "\n" ;
554
559
$ isCurrentLineBlank = $ this ->isCurrentLineBlank ();
555
560
}
556
561
}
557
562
} elseif ($ notEOF ) {
558
- $ text .= "\n" ;
563
+ $ blockLines [] = '' ;
559
564
}
560
565
561
566
if ($ notEOF ) {
567
+ $ blockLines [] = '' ;
562
568
$ this ->moveToPreviousLine ();
563
569
}
564
570
565
571
// folded style
566
572
if ('> ' === $ style ) {
567
- // folded lines
568
- // replace all non-leading/non-trailing single newlines with spaces
569
- preg_match ('/(\n*)$/ ' , $ text , $ matches );
570
- $ text = preg_replace ('/(?<!\n|^)\n(?!\n)/ ' , ' ' , rtrim ($ text , "\n" ));
571
- $ text .= $ matches [1 ];
572
-
573
- // empty separation lines
574
- // remove one newline from each group of non-leading/non-trailing newlines
575
- $ text = preg_replace ('/[^\n]\n+\K\n(?=[^\n])/ ' , '' , $ text );
573
+ $ text = '' ;
574
+ $ previousLineIndented = false ;
575
+ $ previousLineBlank = false ;
576
+
577
+ for ($ i = 0 ; $ i < count ($ blockLines ); $ i ++) {
578
+ if ('' === $ blockLines [$ i ]) {
579
+ $ text .= "\n" ;
580
+ $ previousLineIndented = false ;
581
+ $ previousLineBlank = true ;
582
+ } elseif (' ' === $ blockLines [$ i ][0 ]) {
583
+ $ text .= "\n" .$ blockLines [$ i ];
584
+ $ previousLineIndented = true ;
585
+ $ previousLineBlank = false ;
586
+ } elseif ($ previousLineIndented ) {
587
+ $ text .= "\n" .$ blockLines [$ i ];
588
+ $ previousLineIndented = false ;
589
+ $ previousLineBlank = false ;
590
+ } elseif ($ previousLineBlank || 0 === $ i ) {
591
+ $ text .= $ blockLines [$ i ];
592
+ $ previousLineIndented = false ;
593
+ $ previousLineBlank = false ;
594
+ } else {
595
+ $ text .= ' ' .$ blockLines [$ i ];
596
+ $ previousLineIndented = false ;
597
+ $ previousLineBlank = false ;
598
+ }
599
+ }
600
+ } else {
601
+ $ text = implode ("\n" , $ blockLines );
576
602
}
577
603
578
604
// deal with trailing newlines
0 commit comments