@@ -25,6 +25,7 @@ class Parser
25
25
const FOLDED_SCALAR_PATTERN = self ::BLOCK_SCALAR_HEADER_PATTERN ;
26
26
27
27
private $ offset = 0 ;
28
+ private $ totalNumberOfLines ;
28
29
private $ lines = array ();
29
30
private $ currentLineNb = -1 ;
30
31
private $ currentLine = '' ;
@@ -33,11 +34,13 @@ class Parser
33
34
/**
34
35
* Constructor.
35
36
*
36
- * @param int $offset The offset of YAML document (used for line numbers in error messages)
37
+ * @param int $offset The offset of YAML document (used for line numbers in error messages)
38
+ * @param int|null $totalNumberOfLines The overall number of lines being parsed
37
39
*/
38
- public function __construct ($ offset = 0 )
40
+ public function __construct ($ offset = 0 , $ totalNumberOfLines = null )
39
41
{
40
42
$ this ->offset = $ offset ;
43
+ $ this ->totalNumberOfLines = $ totalNumberOfLines ;
41
44
}
42
45
43
46
/**
@@ -61,6 +64,10 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
61
64
$ value = $ this ->cleanup ($ value );
62
65
$ this ->lines = explode ("\n" , $ value );
63
66
67
+ if (null === $ this ->totalNumberOfLines ) {
68
+ $ this ->totalNumberOfLines = count ($ this ->lines );
69
+ }
70
+
64
71
if (function_exists ('mb_internal_encoding ' ) && ((int ) ini_get ('mbstring.func_overload ' )) & 2 ) {
65
72
$ mbEncoding = mb_internal_encoding ();
66
73
mb_internal_encoding ('UTF-8 ' );
@@ -93,7 +100,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
93
100
// array
94
101
if (!isset ($ values ['value ' ]) || '' == trim ($ values ['value ' ], ' ' ) || 0 === strpos (ltrim ($ values ['value ' ], ' ' ), '# ' )) {
95
102
$ c = $ this ->getRealCurrentLineNb () + 1 ;
96
- $ parser = new self ($ c );
103
+ $ parser = new self ($ c, $ this -> totalNumberOfLines );
97
104
$ parser ->refs = &$ this ->refs ;
98
105
$ data [] = $ parser ->parse ($ this ->getNextEmbedBlock (null , true ), $ exceptionOnInvalidType , $ objectSupport );
99
106
} else {
@@ -102,7 +109,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
102
109
) {
103
110
// this is a compact notation element, add to next block and parse
104
111
$ c = $ this ->getRealCurrentLineNb ();
105
- $ parser = new self ($ c );
112
+ $ parser = new self ($ c, $ this -> totalNumberOfLines );
106
113
$ parser ->refs = &$ this ->refs ;
107
114
108
115
$ block = $ values ['value ' ];
@@ -153,7 +160,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
153
160
$ value = $ this ->getNextEmbedBlock ();
154
161
}
155
162
$ c = $ this ->getRealCurrentLineNb () + 1 ;
156
- $ parser = new self ($ c );
163
+ $ parser = new self ($ c, $ this -> totalNumberOfLines );
157
164
$ parser ->refs = &$ this ->refs ;
158
165
$ parsed = $ parser ->parse ($ value , $ exceptionOnInvalidType , $ objectSupport );
159
166
@@ -190,7 +197,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
190
197
$ data [$ key ] = null ;
191
198
} else {
192
199
$ c = $ this ->getRealCurrentLineNb () + 1 ;
193
- $ parser = new self ($ c );
200
+ $ parser = new self ($ c, $ this -> totalNumberOfLines );
194
201
$ parser ->refs = &$ this ->refs ;
195
202
$ data [$ key ] = $ parser ->parse ($ this ->getNextEmbedBlock (), $ exceptionOnInvalidType , $ objectSupport );
196
203
}
@@ -528,6 +535,8 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
528
535
if ($ notEOF ) {
529
536
$ blockLines [] = '' ;
530
537
$ this ->moveToPreviousLine ();
538
+ } elseif (!$ notEOF && !$ this ->isCurrentLineLastLineInDocument ()) {
539
+ $ blockLines [] = '' ;
531
540
}
532
541
533
542
// folded style
@@ -634,6 +643,11 @@ private function isCurrentLineComment()
634
643
return '' !== $ ltrimmedLine && $ ltrimmedLine [0 ] === '# ' ;
635
644
}
636
645
646
+ private function isCurrentLineLastLineInDocument ()
647
+ {
648
+ return ($ this ->offset + $ this ->currentLineNb ) >= ($ this ->totalNumberOfLines - 1 );
649
+ }
650
+
637
651
/**
638
652
* Cleanups a YAML string to be parsed.
639
653
*
0 commit comments