@@ -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
@@ -294,7 +297,7 @@ public static function parseScalar($scalar, $delimiters = null, $stringDelimiter
294
297
}
295
298
296
299
if ($ evaluate ) {
297
- $ output = self ::evaluateScalar ($ output , $ references );
300
+ $ output = self ::evaluateScalar ($ output , $ flags , $ references );
298
301
}
299
302
}
300
303
@@ -335,14 +338,15 @@ private static function parseQuotedScalar($scalar, &$i)
335
338
* Parses a sequence to a YAML string.
336
339
*
337
340
* @param string $sequence
341
+ * @param int $flags
338
342
* @param int &$i
339
343
* @param array $references
340
344
*
341
345
* @return string A YAML string
342
346
*
343
347
* @throws ParseException When malformed inline YAML string is parsed
344
348
*/
345
- private static function parseSequence ($ sequence , &$ i = 0 , $ references = array ())
349
+ private static function parseSequence ($ sequence , $ flags , &$ i = 0 , $ references = array ())
346
350
{
347
351
$ output = array ();
348
352
$ len = strlen ($ sequence );
@@ -353,11 +357,11 @@ private static function parseSequence($sequence, &$i = 0, $references = array())
353
357
switch ($ sequence [$ i ]) {
354
358
case '[ ' :
355
359
// nested sequence
356
- $ output [] = self ::parseSequence ($ sequence , $ i , $ references );
360
+ $ output [] = self ::parseSequence ($ sequence , $ flags , $ i , $ references );
357
361
break ;
358
362
case '{ ' :
359
363
// nested mapping
360
- $ output [] = self ::parseMapping ($ sequence , $ i , $ references );
364
+ $ output [] = self ::parseMapping ($ sequence , $ flags , $ i , $ references );
361
365
break ;
362
366
case '] ' :
363
367
return $ output ;
@@ -366,14 +370,14 @@ private static function parseSequence($sequence, &$i = 0, $references = array())
366
370
break ;
367
371
default :
368
372
$ isQuoted = in_array ($ sequence [$ i ], array ('" ' , "' " ));
369
- $ value = self ::parseScalar ($ sequence , array (', ' , '] ' ), array ('" ' , "' " ), $ i , true , $ references );
373
+ $ value = self ::parseScalar ($ sequence , $ flags , array (', ' , '] ' ), array ('" ' , "' " ), $ i , true , $ references );
370
374
371
375
// the value can be an array if a reference has been resolved to an array var
372
376
if (!is_array ($ value ) && !$ isQuoted && false !== strpos ($ value , ': ' )) {
373
377
// embedded mapping?
374
378
try {
375
379
$ pos = 0 ;
376
- $ value = self ::parseMapping ('{ ' .$ value .'} ' , $ pos , $ references );
380
+ $ value = self ::parseMapping ('{ ' .$ value .'} ' , $ flags , $ pos , $ references );
377
381
} catch (\InvalidArgumentException $ e ) {
378
382
// no, it's not
379
383
}
@@ -394,14 +398,15 @@ private static function parseSequence($sequence, &$i = 0, $references = array())
394
398
* Parses a mapping to a YAML string.
395
399
*
396
400
* @param string $mapping
401
+ * @param int $flags
397
402
* @param int &$i
398
403
* @param array $references
399
404
*
400
405
* @return string A YAML string
401
406
*
402
407
* @throws ParseException When malformed inline YAML string is parsed
403
408
*/
404
- private static function parseMapping ($ mapping , &$ i = 0 , $ references = array ())
409
+ private static function parseMapping ($ mapping , $ flags , &$ i = 0 , $ references = array ())
405
410
{
406
411
$ output = array ();
407
412
$ len = strlen ($ mapping );
@@ -423,7 +428,7 @@ private static function parseMapping($mapping, &$i = 0, $references = array())
423
428
}
424
429
425
430
// key
426
- $ key = self ::parseScalar ($ mapping , array (': ' , ' ' ), array ('" ' , "' " ), $ i , false );
431
+ $ key = self ::parseScalar ($ mapping , $ flags , array (': ' , ' ' ), array ('" ' , "' " ), $ i , false );
427
432
428
433
// value
429
434
$ done = false ;
@@ -432,7 +437,7 @@ private static function parseMapping($mapping, &$i = 0, $references = array())
432
437
switch ($ mapping [$ i ]) {
433
438
case '[ ' :
434
439
// nested sequence
435
- $ value = self ::parseSequence ($ mapping , $ i , $ references );
440
+ $ value = self ::parseSequence ($ mapping , $ flags , $ i , $ references );
436
441
// Spec: Keys MUST be unique; first one wins.
437
442
// Parser cannot abort this mapping earlier, since lines
438
443
// are processed sequentially.
@@ -443,7 +448,7 @@ private static function parseMapping($mapping, &$i = 0, $references = array())
443
448
break ;
444
449
case '{ ' :
445
450
// nested mapping
446
- $ value = self ::parseMapping ($ mapping , $ i , $ references );
451
+ $ value = self ::parseMapping ($ mapping , $ flags , $ i , $ references );
447
452
// Spec: Keys MUST be unique; first one wins.
448
453
// Parser cannot abort this mapping earlier, since lines
449
454
// are processed sequentially.
@@ -456,7 +461,7 @@ private static function parseMapping($mapping, &$i = 0, $references = array())
456
461
case ' ' :
457
462
break ;
458
463
default :
459
- $ value = self ::parseScalar ($ mapping , array (', ' , '} ' ), array ('" ' , "' " ), $ i , true , $ references );
464
+ $ value = self ::parseScalar ($ mapping , $ flags , array (', ' , '} ' ), array ('" ' , "' " ), $ i , true , $ references );
460
465
// Spec: Keys MUST be unique; first one wins.
461
466
// Parser cannot abort this mapping earlier, since lines
462
467
// are processed sequentially.
@@ -482,13 +487,14 @@ private static function parseMapping($mapping, &$i = 0, $references = array())
482
487
* Evaluates scalars and replaces magic values.
483
488
*
484
489
* @param string $scalar
490
+ * @param int $flags
485
491
* @param array $references
486
492
*
487
493
* @return string A YAML string
488
494
*
489
495
* @throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved
490
496
*/
491
- private static function evaluateScalar ($ scalar , $ references = array ())
497
+ private static function evaluateScalar ($ scalar , $ flags , $ references = array ())
492
498
{
493
499
$ scalar = trim ($ scalar );
494
500
$ scalarLower = strtolower ($ scalar );
@@ -527,7 +533,7 @@ private static function evaluateScalar($scalar, $references = array())
527
533
case 0 === strpos ($ scalar , '!str ' ):
528
534
return (string ) substr ($ scalar , 5 );
529
535
case 0 === strpos ($ scalar , '! ' ):
530
- return (int ) self ::parseScalar (substr ($ scalar , 2 ));
536
+ return (int ) self ::parseScalar (substr ($ scalar , 2 ), $ flags );
531
537
case 0 === strpos ($ scalar , '!php/object: ' ):
532
538
if (self ::$ objectSupport ) {
533
539
return unserialize (substr ($ scalar , 12 ));
@@ -573,6 +579,10 @@ private static function evaluateScalar($scalar, $references = array())
573
579
case preg_match ('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/ ' , $ scalar ):
574
580
return (float ) str_replace (', ' , '' , $ scalar );
575
581
case preg_match (self ::getTimestampRegex (), $ scalar ):
582
+ if (Yaml::PARSE_DATETIME & $ flags ) {
583
+ return new \DateTime ($ scalar ,new \DateTimeZone ('UTC ' ));
584
+ }
585
+
576
586
$ timeZone = date_default_timezone_get ();
577
587
date_default_timezone_set ('UTC ' );
578
588
$ time = strtotime ($ scalar );
0 commit comments