@@ -92,15 +92,15 @@ public static function parse($value, $flags = 0, $references = array())
92
92
$ i = 0 ;
93
93
switch ($ value [0 ]) {
94
94
case '[ ' :
95
- $ result = self ::parseSequence ($ value , $ i , $ references );
95
+ $ result = self ::parseSequence ($ value , $ flags , $ i , $ references );
96
96
++$ i ;
97
97
break ;
98
98
case '{ ' :
99
- $ result = self ::parseMapping ($ value , $ i , $ references );
99
+ $ result = self ::parseMapping ($ value , $ flags , $ i , $ references );
100
100
++$ i ;
101
101
break ;
102
102
default :
103
- $ result = self ::parseScalar ($ value , null , array ('" ' , "' " ), $ i , true , $ references );
103
+ $ result = self ::parseScalar ($ value , $ flags , null , array ('" ' , "' " ), $ i , true , $ references );
104
104
}
105
105
106
106
// some comments are allowed at the end
@@ -152,6 +152,8 @@ public static function dump($value, $flags = 0)
152
152
}
153
153
154
154
return 'null ' ;
155
+ case $ value instanceof \DateTimeInterface:
156
+ return $ value ->format ('c ' );
155
157
case is_object ($ value ):
156
158
if (Yaml::DUMP_OBJECT & $ flags ) {
157
159
return '!php/object: ' .serialize ($ value );
@@ -243,6 +245,7 @@ private static function dumpArray($value, $flags)
243
245
* Parses a scalar to a YAML string.
244
246
*
245
247
* @param string $scalar
248
+ * @param int $flags
246
249
* @param string $delimiters
247
250
* @param array $stringDelimiters
248
251
* @param int &$i
@@ -255,7 +258,7 @@ private static function dumpArray($value, $flags)
255
258
*
256
259
* @internal
257
260
*/
258
- public static function parseScalar ($ scalar , $ delimiters = null , $ stringDelimiters = array ('" ' , "' " ), &$ i = 0 , $ evaluate = true , $ references = array ())
261
+ public static function parseScalar ($ scalar , $ flags = 0 , $ delimiters = null , $ stringDelimiters = array ('" ' , "' " ), &$ i = 0 , $ evaluate = true , $ references = array ())
259
262
{
260
263
if (in_array ($ scalar [$ i ], $ stringDelimiters )) {
261
264
// quoted scalar
@@ -290,7 +293,7 @@ public static function parseScalar($scalar, $delimiters = null, $stringDelimiter
290
293
}
291
294
292
295
if ($ evaluate ) {
293
- $ output = self ::evaluateScalar ($ output , $ references );
296
+ $ output = self ::evaluateScalar ($ output , $ flags , $ references );
294
297
}
295
298
}
296
299
@@ -331,14 +334,15 @@ private static function parseQuotedScalar($scalar, &$i)
331
334
* Parses a sequence to a YAML string.
332
335
*
333
336
* @param string $sequence
337
+ * @param int $flags
334
338
* @param int &$i
335
339
* @param array $references
336
340
*
337
341
* @return string A YAML string
338
342
*
339
343
* @throws ParseException When malformed inline YAML string is parsed
340
344
*/
341
- private static function parseSequence ($ sequence , &$ i = 0 , $ references = array ())
345
+ private static function parseSequence ($ sequence , $ flags , &$ i = 0 , $ references = array ())
342
346
{
343
347
$ output = array ();
344
348
$ len = strlen ($ sequence );
@@ -349,11 +353,11 @@ private static function parseSequence($sequence, &$i = 0, $references = array())
349
353
switch ($ sequence [$ i ]) {
350
354
case '[ ' :
351
355
// nested sequence
352
- $ output [] = self ::parseSequence ($ sequence , $ i , $ references );
356
+ $ output [] = self ::parseSequence ($ sequence , $ flags , $ i , $ references );
353
357
break ;
354
358
case '{ ' :
355
359
// nested mapping
356
- $ output [] = self ::parseMapping ($ sequence , $ i , $ references );
360
+ $ output [] = self ::parseMapping ($ sequence , $ flags , $ i , $ references );
357
361
break ;
358
362
case '] ' :
359
363
return $ output ;
@@ -362,14 +366,14 @@ private static function parseSequence($sequence, &$i = 0, $references = array())
362
366
break ;
363
367
default :
364
368
$ isQuoted = in_array ($ sequence [$ i ], array ('" ' , "' " ));
365
- $ value = self ::parseScalar ($ sequence , array (', ' , '] ' ), array ('" ' , "' " ), $ i , true , $ references );
369
+ $ value = self ::parseScalar ($ sequence , $ flags , array (', ' , '] ' ), array ('" ' , "' " ), $ i , true , $ references );
366
370
367
371
// the value can be an array if a reference has been resolved to an array var
368
372
if (!is_array ($ value ) && !$ isQuoted && false !== strpos ($ value , ': ' )) {
369
373
// embedded mapping?
370
374
try {
371
375
$ pos = 0 ;
372
- $ value = self ::parseMapping ('{ ' .$ value .'} ' , $ pos , $ references );
376
+ $ value = self ::parseMapping ('{ ' .$ value .'} ' , $ flags , $ pos , $ references );
373
377
} catch (\InvalidArgumentException $ e ) {
374
378
// no, it's not
375
379
}
@@ -390,14 +394,15 @@ private static function parseSequence($sequence, &$i = 0, $references = array())
390
394
* Parses a mapping to a YAML string.
391
395
*
392
396
* @param string $mapping
397
+ * @param int $flags
393
398
* @param int &$i
394
399
* @param array $references
395
400
*
396
401
* @return string A YAML string
397
402
*
398
403
* @throws ParseException When malformed inline YAML string is parsed
399
404
*/
400
- private static function parseMapping ($ mapping , &$ i = 0 , $ references = array ())
405
+ private static function parseMapping ($ mapping , $ flags , &$ i = 0 , $ references = array ())
401
406
{
402
407
$ output = array ();
403
408
$ len = strlen ($ mapping );
@@ -419,7 +424,7 @@ private static function parseMapping($mapping, &$i = 0, $references = array())
419
424
}
420
425
421
426
// key
422
- $ key = self ::parseScalar ($ mapping , array (': ' , ' ' ), array ('" ' , "' " ), $ i , false );
427
+ $ key = self ::parseScalar ($ mapping , $ flags , array (': ' , ' ' ), array ('" ' , "' " ), $ i , false );
423
428
424
429
// value
425
430
$ done = false ;
@@ -428,7 +433,7 @@ private static function parseMapping($mapping, &$i = 0, $references = array())
428
433
switch ($ mapping [$ i ]) {
429
434
case '[ ' :
430
435
// nested sequence
431
- $ value = self ::parseSequence ($ mapping , $ i , $ references );
436
+ $ value = self ::parseSequence ($ mapping , $ flags , $ i , $ references );
432
437
// Spec: Keys MUST be unique; first one wins.
433
438
// Parser cannot abort this mapping earlier, since lines
434
439
// are processed sequentially.
@@ -439,7 +444,7 @@ private static function parseMapping($mapping, &$i = 0, $references = array())
439
444
break ;
440
445
case '{ ' :
441
446
// nested mapping
442
- $ value = self ::parseMapping ($ mapping , $ i , $ references );
447
+ $ value = self ::parseMapping ($ mapping , $ flags , $ i , $ references );
443
448
// Spec: Keys MUST be unique; first one wins.
444
449
// Parser cannot abort this mapping earlier, since lines
445
450
// are processed sequentially.
@@ -452,7 +457,7 @@ private static function parseMapping($mapping, &$i = 0, $references = array())
452
457
case ' ' :
453
458
break ;
454
459
default :
455
- $ value = self ::parseScalar ($ mapping , array (', ' , '} ' ), array ('" ' , "' " ), $ i , true , $ references );
460
+ $ value = self ::parseScalar ($ mapping , $ flags , array (', ' , '} ' ), array ('" ' , "' " ), $ i , true , $ references );
456
461
// Spec: Keys MUST be unique; first one wins.
457
462
// Parser cannot abort this mapping earlier, since lines
458
463
// are processed sequentially.
@@ -478,13 +483,14 @@ private static function parseMapping($mapping, &$i = 0, $references = array())
478
483
* Evaluates scalars and replaces magic values.
479
484
*
480
485
* @param string $scalar
486
+ * @param int $flags
481
487
* @param array $references
482
488
*
483
489
* @return string A YAML string
484
490
*
485
491
* @throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved
486
492
*/
487
- private static function evaluateScalar ($ scalar , $ references = array ())
493
+ private static function evaluateScalar ($ scalar , $ flags , $ references = array ())
488
494
{
489
495
$ scalar = trim ($ scalar );
490
496
$ scalarLower = strtolower ($ scalar );
@@ -523,7 +529,7 @@ private static function evaluateScalar($scalar, $references = array())
523
529
case 0 === strpos ($ scalar , '!str ' ):
524
530
return (string ) substr ($ scalar , 5 );
525
531
case 0 === strpos ($ scalar , '! ' ):
526
- return (int ) self ::parseScalar (substr ($ scalar , 2 ));
532
+ return (int ) self ::parseScalar (substr ($ scalar , 2 ), $ flags );
527
533
case 0 === strpos ($ scalar , '!php/object: ' ):
528
534
if (self ::$ objectSupport ) {
529
535
return unserialize (substr ($ scalar , 12 ));
@@ -569,6 +575,10 @@ private static function evaluateScalar($scalar, $references = array())
569
575
case preg_match ('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/ ' , $ scalar ):
570
576
return (float ) str_replace (', ' , '' , $ scalar );
571
577
case preg_match (self ::getTimestampRegex (), $ scalar ):
578
+ if (Yaml::PARSE_DATETIME & $ flags ) {
579
+ return new \DateTime ($ scalar ,new \DateTimeZone ('UTC ' ));
580
+ }
581
+
572
582
$ timeZone = date_default_timezone_get ();
573
583
date_default_timezone_set ('UTC ' );
574
584
$ time = strtotime ($ scalar );
0 commit comments