@@ -43,15 +43,32 @@ public function __construct($offset = 0)
43
43
*
44
44
* @param string $value A YAML string
45
45
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
46
- * @param bool $objectSupport true if object support is enabled, false otherwise
47
- * @param bool $objectForMap true if maps should return a stdClass instead of array()
46
+ * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
48
47
*
49
48
* @return mixed A PHP value
50
49
*
51
50
* @throws ParseException If the YAML is not valid
52
51
*/
53
- public function parse ($ value , $ exceptionOnInvalidType = false , $ objectSupport = false , $ objectForMap = false )
52
+ public function parse ($ value , $ exceptionOnInvalidType = false , $ flags = 0 )
54
53
{
54
+ if (is_bool ($ flags )) {
55
+ @trigger_error ('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::PARSE_OBJECT flag instead. ' , E_USER_DEPRECATED );
56
+
57
+ if ($ flags ) {
58
+ $ flags = Yaml::PARSE_OBJECT ;
59
+ } else {
60
+ $ flags = 0 ;
61
+ }
62
+ }
63
+
64
+ if (func_num_args () >= 4 ) {
65
+ @trigger_error ('Passing a boolean flag to toggle object for map support is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::PARSE_OBJECT_FOR_MAP flag instead. ' , E_USER_DEPRECATED );
66
+
67
+ if (func_get_arg (3 )) {
68
+ $ flags |= Yaml::PARSE_OBJECT_FOR_MAP ;
69
+ }
70
+ }
71
+
55
72
if (!preg_match ('//u ' , $ value )) {
56
73
throw new ParseException ('The YAML value does not appear to be valid UTF-8. ' );
57
74
}
@@ -95,7 +112,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
95
112
$ c = $ this ->getRealCurrentLineNb () + 1 ;
96
113
$ parser = new self ($ c );
97
114
$ parser ->refs = &$ this ->refs ;
98
- $ data [] = $ parser ->parse ($ this ->getNextEmbedBlock (null , true ), $ exceptionOnInvalidType , $ objectSupport , $ objectForMap );
115
+ $ data [] = $ parser ->parse ($ this ->getNextEmbedBlock (null , true ), $ exceptionOnInvalidType , $ flags );
99
116
} else {
100
117
if (isset ($ values ['leadspaces ' ])
101
118
&& preg_match ('#^(?P<key> ' .Inline::REGEX_QUOTED_STRING .'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u ' , $ values ['value ' ], $ matches )
@@ -110,9 +127,9 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
110
127
$ block .= "\n" .$ this ->getNextEmbedBlock ($ this ->getCurrentLineIndentation () + strlen ($ values ['leadspaces ' ]) + 1 );
111
128
}
112
129
113
- $ data [] = $ parser ->parse ($ block , $ exceptionOnInvalidType , $ objectSupport , $ objectForMap );
130
+ $ data [] = $ parser ->parse ($ block , $ exceptionOnInvalidType , $ flags );
114
131
} else {
115
- $ data [] = $ this ->parseValue ($ values ['value ' ], $ exceptionOnInvalidType , $ objectSupport , $ objectForMap , $ context );
132
+ $ data [] = $ this ->parseValue ($ values ['value ' ], $ exceptionOnInvalidType , $ flags , $ context );
116
133
}
117
134
}
118
135
if ($ isRef ) {
@@ -125,7 +142,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
125
142
$ context = 'mapping ' ;
126
143
127
144
// force correct settings
128
- Inline::parse (null , $ exceptionOnInvalidType , $ objectSupport , $ objectForMap , $ this ->refs );
145
+ Inline::parse (null , $ exceptionOnInvalidType , $ flags , $ this ->refs );
129
146
try {
130
147
$ key = Inline::parseScalar ($ values ['key ' ]);
131
148
} catch (ParseException $ e ) {
@@ -169,7 +186,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
169
186
$ c = $ this ->getRealCurrentLineNb () + 1 ;
170
187
$ parser = new self ($ c );
171
188
$ parser ->refs = &$ this ->refs ;
172
- $ parsed = $ parser ->parse ($ value , $ exceptionOnInvalidType , $ objectSupport , $ objectForMap );
189
+ $ parsed = $ parser ->parse ($ value , $ exceptionOnInvalidType , $ flags );
173
190
174
191
if (!is_array ($ parsed )) {
175
192
throw new ParseException ('YAML merge keys used with a scalar value instead of an array. ' , $ this ->getRealCurrentLineNb () + 1 , $ this ->currentLine );
@@ -220,15 +237,15 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
220
237
$ c = $ this ->getRealCurrentLineNb () + 1 ;
221
238
$ parser = new self ($ c );
222
239
$ parser ->refs = &$ this ->refs ;
223
- $ value = $ parser ->parse ($ this ->getNextEmbedBlock (), $ exceptionOnInvalidType , $ objectSupport , $ objectForMap );
240
+ $ value = $ parser ->parse ($ this ->getNextEmbedBlock (), $ exceptionOnInvalidType , $ flags );
224
241
// Spec: Keys MUST be unique; first one wins.
225
242
// But overwriting is allowed when a merge node is used in current block.
226
243
if ($ allowOverwrite || !isset ($ data [$ key ])) {
227
244
$ data [$ key ] = $ value ;
228
245
}
229
246
}
230
247
} else {
231
- $ value = $ this ->parseValue ($ values ['value ' ], $ exceptionOnInvalidType , $ objectSupport , $ objectForMap , $ context );
248
+ $ value = $ this ->parseValue ($ values ['value ' ], $ exceptionOnInvalidType , $ flags , $ context );
232
249
// Spec: Keys MUST be unique; first one wins.
233
250
// But overwriting is allowed when a merge node is used in current block.
234
251
if ($ allowOverwrite || !isset ($ data [$ key ])) {
@@ -247,7 +264,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
247
264
// 1-liner optionally followed by newline(s)
248
265
if (is_string ($ value ) && $ this ->lines [0 ] === trim ($ value )) {
249
266
try {
250
- $ value = Inline::parse ($ this ->lines [0 ], $ exceptionOnInvalidType , $ objectSupport , $ objectForMap , $ this ->refs );
267
+ $ value = Inline::parse ($ this ->lines [0 ], $ exceptionOnInvalidType , $ flags , $ this ->refs );
251
268
} catch (ParseException $ e ) {
252
269
$ e ->setParsedLine ($ this ->getRealCurrentLineNb () + 1 );
253
270
$ e ->setSnippet ($ this ->currentLine );
@@ -301,7 +318,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
301
318
mb_internal_encoding ($ mbEncoding );
302
319
}
303
320
304
- if ($ objectForMap && !is_object ($ data )) {
321
+ if (Yaml:: PARSE_OBJECT_FOR_MAP & $ flags && !is_object ($ data )) {
305
322
$ data = (object ) $ data ;
306
323
}
307
324
@@ -464,15 +481,14 @@ private function moveToPreviousLine()
464
481
*
465
482
* @param string $value A YAML value
466
483
* @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise
467
- * @param bool $objectSupport True if object support is enabled, false otherwise
468
- * @param bool $objectForMap true if maps should return a stdClass instead of array()
484
+ * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
469
485
* @param string $context The parser context (either sequence or mapping)
470
486
*
471
487
* @return mixed A PHP value
472
488
*
473
489
* @throws ParseException When reference does not exist
474
490
*/
475
- private function parseValue ($ value , $ exceptionOnInvalidType , $ objectSupport , $ objectForMap , $ context )
491
+ private function parseValue ($ value , $ exceptionOnInvalidType , $ flags , $ context )
476
492
{
477
493
if (0 === strpos ($ value , '* ' )) {
478
494
if (false !== $ pos = strpos ($ value , '# ' )) {
@@ -495,7 +511,7 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $ob
495
511
}
496
512
497
513
try {
498
- $ parsedValue = Inline::parse ($ value , $ exceptionOnInvalidType , $ objectSupport , $ objectForMap , $ this ->refs );
514
+ $ parsedValue = Inline::parse ($ value , $ exceptionOnInvalidType , $ flags , $ this ->refs );
499
515
500
516
if ('mapping ' === $ context && '" ' !== $ value [0 ] && "' " !== $ value [0 ] && '[ ' !== $ value [0 ] && '{ ' !== $ value [0 ] && '! ' !== $ value [0 ] && false !== strpos ($ parsedValue , ': ' )) {
501
517
throw new ParseException ('A colon cannot be used in an unquoted mapping value. ' );
0 commit comments