@@ -33,12 +33,14 @@ public static function load($value)
33
33
{
34
34
$ value = trim ($ value );
35
35
36
- if (0 == strlen ( $ value) ) {
36
+ if ('' === $ value ) {
37
37
return '' ;
38
38
}
39
39
40
- $ mbEncoding = mb_internal_encoding ();
41
- mb_internal_encoding ('ASCII ' );
40
+ if (function_exists ('mb_internal_encoding ' ) && ((int ) ini_get ('mbstring.func_overload ' )) & 2 ) {
41
+ $ mbEncoding = mb_internal_encoding ();
42
+ mb_internal_encoding ('ASCII ' );
43
+ }
42
44
43
45
switch ($ value [0 ]) {
44
46
case '[ ' :
@@ -77,7 +79,8 @@ public static function dump($value)
77
79
78
80
switch (true ) {
79
81
case is_resource ($ value ):
80
- throw new InvalidArgumentException ('Unable to dump PHP resources in a YAML file. ' );
82
+ return stream_get_contents ($ value );
83
+ // throw new InvalidArgumentException('Unable to dump PHP resources in a YAML file.');
81
84
case is_object ($ value ):
82
85
return '!!php/object: ' .serialize ($ value );
83
86
case is_array ($ value ):
@@ -88,10 +91,10 @@ public static function dump($value)
88
91
return 'true ' ;
89
92
case false === $ value :
90
93
return 'false ' ;
91
- case ctype_digit ($ value ):
94
+ case ( is_string ( $ value ) && ctype_digit ($ value) ):
92
95
return is_string ($ value ) ? "' $ value' " : (int ) $ value ;
93
- case is_numeric ($ value ):
94
- return is_infinite ($ value ) ? str_ireplace ('INF ' , '.Inf ' , strval ( $ value) ) : (is_string ($ value ) ? "' $ value' " : $ value );
96
+ case is_numeric ($ value ) && false === strpbrk ( $ value , "\f\n\r\t\v" ) :
97
+ return is_infinite ($ value ) ? str_ireplace ('INF ' , '.Inf ' , ( string ) $ value ) : (is_string ($ value ) ? "' $ value' " : $ value );
95
98
case false !== strpos ($ value , "\n" ) || false !== strpos ($ value , "\r" ):
96
99
return sprintf ('"%s" ' , str_replace (array ('" ' , "\n" , "\r" ), array ('\\" ' , '\n ' , '\r ' ), $ value ));
97
100
case preg_match ('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ - ? | < > = ! % @ ` ]/x ' , $ value ):
@@ -122,8 +125,11 @@ protected static function dumpArray($value)
122
125
{
123
126
// array
124
127
$ keys = array_keys ($ value );
125
-
126
- if (count ($ value ) > 0 && array_values ($ value ) === $ value )
128
+
129
+ if (
130
+ (1 == count ($ keys ) && '0 ' == $ keys [0 ])
131
+ ||
132
+ (count ($ keys ) > 1 && array_sum (array_map ('intval ' , $ keys )) == count ($ keys ) * (count ($ keys ) - 1 ) / 2 ))
127
133
{
128
134
$ output = array ();
129
135
foreach ($ value as $ val ) {
@@ -353,27 +359,33 @@ protected static function evaluateScalar($scalar)
353
359
case 0 === strpos ($ scalar , '!str ' ):
354
360
return (string ) substr ($ scalar , 5 );
355
361
case 0 === strpos ($ scalar , '! ' ):
356
- return intval ( self ::parseScalar (substr ($ scalar , 2 ) ));
362
+ return ( int ) self ::parseScalar (substr ($ scalar , 2 ));
357
363
case 0 === strpos ($ scalar , '!!php/object: ' ):
358
364
return unserialize (substr ($ scalar , 13 ));
359
365
case ctype_digit ($ scalar ):
360
366
$ raw = $ scalar ;
361
- $ cast = intval ( $ scalar) ;
367
+ $ cast = ( int ) $ scalar ;
362
368
363
369
return '0 ' == $ scalar [0 ] ? octdec ($ scalar ) : (((string ) $ raw == (string ) $ cast ) ? $ cast : $ raw );
364
370
case in_array (strtolower ($ scalar ), $ trueValues ):
365
371
return true ;
366
372
case in_array (strtolower ($ scalar ), $ falseValues ):
367
373
return false ;
374
+ case 0 === strpos ($ scalar , '0x ' ):
375
+ return hexdec ($ scalar );
368
376
case is_numeric ($ scalar ):
369
- return ' 0x ' == $ scalar [ 0 ]. $ scalar [ 1 ] ? hexdec ( $ scalar ) : floatval ($ scalar );
377
+ return floatval ($ scalar );
370
378
case 0 == strcasecmp ($ scalar , '.inf ' ):
371
379
case 0 == strcasecmp ($ scalar , '.NaN ' ):
372
380
return -log (0 );
373
381
case 0 == strcasecmp ($ scalar , '-.inf ' ):
374
382
return log (0 );
375
- case preg_match ('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/ ' , $ scalar ):
376
- return floatval (str_replace (', ' , '' , $ scalar ));
383
+ case preg_match ('/^(-|\+)?[0-9,]+(\.\d+)?$/ ' , $ scalar ):
384
+ $ replaced = str_replace (', ' , '' , $ scalar );
385
+ $ replaced = str_replace ('+ ' , '' , $ replaced );
386
+ $ floatval = floatval ($ replaced );
387
+ $ intval = intval ($ replaced );
388
+ return $ floatval == $ intval ? $ intval : $ floatval ;
377
389
case preg_match (self ::getTimestampRegex (), $ scalar ):
378
390
return strtotime ($ scalar );
379
391
default :
0 commit comments