2010-01-04 14:26:20 +00:00
< ? php
/*
2010-04-07 01:51:29 +01:00
* This file is part of the Symfony package .
2013-12-28 07:37:38 +00:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-01-04 14:26:20 +00:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2011-01-15 13:29:43 +00:00
namespace Symfony\Component\Yaml ;
2011-06-14 11:44:54 +01:00
use Symfony\Component\Yaml\Exception\ParseException ;
use Symfony\Component\Yaml\Exception\DumpException ;
2010-01-04 14:26:20 +00:00
/**
* Inline implements a YAML parser / dumper for the YAML inline syntax .
*
2011-03-06 11:40:06 +00:00
* @ author Fabien Potencier < fabien @ symfony . com >
2016-02-09 19:49:29 +00:00
*
* @ internal
2010-01-04 14:26:20 +00:00
*/
class Inline
{
2010-05-06 12:25:53 +01:00
const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\']*(?:\'\'[^\']*)*)\')' ;
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
private static $exceptionOnInvalidType = false ;
private static $objectSupport = false ;
2014-03-27 09:46:24 +00:00
private static $objectForMap = false ;
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
2010-05-06 12:25:53 +01:00
/**
2011-02-13 14:31:54 +00:00
* Converts a YAML string to a PHP array .
2010-05-06 12:25:53 +01:00
*
2016-02-08 18:49:14 +00:00
* @ param string $value A YAML string
* @ param int $flags A bit field of PARSE_ * constants to customize the YAML parser behavior
* @ param array $references Mapping of variable names to values
2010-05-06 12:25:53 +01:00
*
* @ return array A PHP array representing the YAML string
2012-12-16 12:02:54 +00:00
*
2014-03-27 09:46:24 +00:00
* @ throws ParseException
2010-05-06 12:25:53 +01:00
*/
2016-02-08 18:49:14 +00:00
public static function parse ( $value , $flags = 0 , $references = array ())
2010-01-04 14:26:20 +00:00
{
2016-02-08 18:49:14 +00:00
if ( is_bool ( $flags )) {
@ trigger_error ( 'Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE flag instead.' , E_USER_DEPRECATED );
if ( $flags ) {
$flags = Yaml :: PARSE_EXCEPTION_ON_INVALID_TYPE ;
} else {
$flags = 0 ;
}
}
if ( func_num_args () >= 3 && ! is_array ( $references )) {
@ 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 );
if ( $references ) {
$flags |= Yaml :: PARSE_OBJECT ;
}
if ( func_num_args () >= 4 ) {
@ 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 );
if ( func_get_arg ( 3 )) {
$flags |= Yaml :: PARSE_OBJECT_FOR_MAP ;
}
}
if ( func_num_args () >= 5 ) {
$references = func_get_arg ( 4 );
} else {
$references = array ();
}
}
self :: $exceptionOnInvalidType = ( bool ) ( Yaml :: PARSE_EXCEPTION_ON_INVALID_TYPE & $flags );
self :: $objectSupport = ( bool ) ( Yaml :: PARSE_OBJECT & $flags );
self :: $objectForMap = ( bool ) ( Yaml :: PARSE_OBJECT_FOR_MAP & $flags );
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
2010-05-06 12:25:53 +01:00
$value = trim ( $value );
2010-01-04 14:26:20 +00:00
2015-06-06 07:25:15 +01:00
if ( '' === $value ) {
2010-05-06 12:25:53 +01:00
return '' ;
}
2010-03-29 11:40:22 +01:00
2015-10-14 15:40:43 +01:00
if ( 2 /* MB_OVERLOAD_STRING */ & ( int ) ini_get ( 'mbstring.func_overload' )) {
2010-05-06 12:25:53 +01:00
$mbEncoding = mb_internal_encoding ();
mb_internal_encoding ( 'ASCII' );
}
2010-03-29 11:40:22 +01:00
2012-06-13 13:57:32 +01:00
$i = 0 ;
2010-05-07 15:09:11 +01:00
switch ( $value [ 0 ]) {
2010-05-06 12:25:53 +01:00
case '[' :
2016-02-17 21:09:28 +00:00
$result = self :: parseSequence ( $value , $flags , $i , $references );
2012-06-13 13:57:32 +01:00
++ $i ;
2010-05-06 12:25:53 +01:00
break ;
case '{' :
2016-02-17 21:09:28 +00:00
$result = self :: parseMapping ( $value , $flags , $i , $references );
2012-06-13 13:57:32 +01:00
++ $i ;
2010-05-06 12:25:53 +01:00
break ;
default :
2016-02-17 21:09:28 +00:00
$result = self :: parseScalar ( $value , $flags , null , array ( '"' , " ' " ), $i , true , $references );
2012-06-13 13:57:32 +01:00
}
2012-06-13 13:57:32 +01:00
2012-06-13 13:57:32 +01:00
// some comments are allowed at the end
if ( preg_replace ( '/\s+#.*$/A' , '' , substr ( $value , $i ))) {
throw new ParseException ( sprintf ( 'Unexpected characters near "%s".' , substr ( $value , $i )));
2010-05-06 12:25:53 +01:00
}
2010-03-29 11:40:22 +01:00
2010-05-07 15:09:11 +01:00
if ( isset ( $mbEncoding )) {
2010-05-06 12:25:53 +01:00
mb_internal_encoding ( $mbEncoding );
}
2010-01-04 14:26:20 +00:00
2010-05-06 12:25:53 +01:00
return $result ;
2010-01-04 14:26:20 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* Dumps a given PHP variable to a YAML string .
*
2016-02-09 19:56:50 +00:00
* @ param mixed $value The PHP variable to convert
* @ param int $flags A bit field of Yaml :: DUMP_ * constants to customize the dumped YAML string
2010-05-06 12:25:53 +01:00
*
* @ return string The YAML string representing the PHP array
*
2011-06-14 11:44:54 +01:00
* @ throws DumpException When trying to dump PHP resource
2010-05-06 12:25:53 +01:00
*/
2016-02-09 19:56:50 +00:00
public static function dump ( $value , $flags = 0 )
2010-01-04 14:26:20 +00:00
{
2016-01-27 19:42:59 +00:00
if ( is_bool ( $flags )) {
2016-02-09 19:56:50 +00:00
@ trigger_error ( 'Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.' , E_USER_DEPRECATED );
if ( $flags ) {
$flags = Yaml :: DUMP_EXCEPTION_ON_INVALID_TYPE ;
} else {
$flags = 0 ;
}
}
if ( func_num_args () >= 3 ) {
2016-01-27 19:42:59 +00:00
@ 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::DUMP_OBJECT flag instead.' , E_USER_DEPRECATED );
2016-02-09 19:56:50 +00:00
if ( func_get_arg ( 2 )) {
$flags |= Yaml :: DUMP_OBJECT ;
}
2016-01-27 19:42:59 +00:00
}
2010-05-07 15:09:11 +01:00
switch ( true ) {
2010-05-06 12:25:53 +01:00
case is_resource ( $value ) :
2016-02-09 19:56:50 +00:00
if ( Yaml :: DUMP_EXCEPTION_ON_INVALID_TYPE & $flags ) {
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
throw new DumpException ( sprintf ( 'Unable to dump PHP resources in a YAML file ("%s").' , get_resource_type ( $value )));
}
return 'null' ;
2016-02-17 21:09:28 +00:00
case $value instanceof \DateTimeInterface :
return $value -> format ( 'c' );
2010-05-06 12:25:53 +01:00
case is_object ( $value ) :
2016-01-27 19:42:59 +00:00
if ( Yaml :: DUMP_OBJECT & $flags ) {
2016-01-20 19:11:57 +00:00
return '!php/object:' . serialize ( $value );
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
}
2016-02-08 17:17:53 +00:00
if ( Yaml :: DUMP_OBJECT_AS_MAP & $flags && ( $value instanceof \stdClass || $value instanceof \ArrayObject )) {
return self :: dumpArray (( array ) $value , $flags );
}
2016-02-09 19:56:50 +00:00
if ( Yaml :: DUMP_EXCEPTION_ON_INVALID_TYPE & $flags ) {
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
throw new DumpException ( 'Object support when dumping a YAML file has been disabled.' );
}
return 'null' ;
2010-05-06 12:25:53 +01:00
case is_array ( $value ) :
2016-02-09 19:56:50 +00:00
return self :: dumpArray ( $value , $flags );
2010-05-06 12:25:53 +01:00
case null === $value :
return 'null' ;
case true === $value :
return 'true' ;
case false === $value :
return 'false' ;
case ctype_digit ( $value ) :
return is_string ( $value ) ? " ' $value ' " : ( int ) $value ;
case is_numeric ( $value ) :
2011-11-04 07:17:10 +00:00
$locale = setlocale ( LC_NUMERIC , 0 );
if ( false !== $locale ) {
setlocale ( LC_NUMERIC , 'C' );
}
2014-09-21 20:22:48 +01:00
if ( is_float ( $value )) {
2015-03-12 10:28:44 +00:00
$repr = ( string ) $value ;
2014-09-21 20:22:48 +01:00
if ( is_infinite ( $value )) {
$repr = str_ireplace ( 'INF' , '.Inf' , $repr );
} elseif ( floor ( $value ) == $value && $repr == $value ) {
// Preserve float data type since storing a whole number will result in integer value.
$repr = '!!float ' . $repr ;
}
} else {
2015-03-12 10:28:44 +00:00
$repr = is_string ( $value ) ? " ' $value ' " : ( string ) $value ;
2014-09-21 20:22:48 +01:00
}
2011-11-04 07:17:10 +00:00
if ( false !== $locale ) {
setlocale ( LC_NUMERIC , $locale );
}
return $repr ;
2015-01-16 14:59:09 +00:00
case '' == $value :
return " '' " ;
2016-02-20 09:54:12 +00:00
case Yaml :: DUMP_BASE64_BINARY_DATA & $flags && self :: isBinaryString ( $value ) :
return '!!binary ' . base64_encode ( $value );
2011-03-05 21:39:48 +00:00
case Escaper :: requiresDoubleQuoting ( $value ) :
return Escaper :: escapeWithDoubleQuotes ( $value );
case Escaper :: requiresSingleQuoting ( $value ) :
2015-03-30 08:37:12 +01:00
case preg_match ( self :: getHexRegex (), $value ) :
2015-01-05 10:24:54 +00:00
case preg_match ( self :: getTimestampRegex (), $value ) :
2011-03-05 21:39:48 +00:00
return Escaper :: escapeWithSingleQuotes ( $value );
2010-05-06 12:25:53 +01:00
default :
return $value ;
2010-01-04 14:26:20 +00:00
}
}
2010-05-06 12:25:53 +01:00
/**
* Dumps a PHP array to a YAML string .
*
2016-02-09 19:56:50 +00:00
* @ param array $value The PHP array to dump
* @ param int $flags A bit field of Yaml :: DUMP_ * constants to customize the dumped YAML string
2010-05-06 12:25:53 +01:00
*
* @ return string The YAML string representing the PHP array
*/
2016-02-09 19:56:50 +00:00
private static function dumpArray ( $value , $flags )
2010-01-04 14:26:20 +00:00
{
2010-05-06 12:25:53 +01:00
// array
$keys = array_keys ( $value );
2015-03-07 19:12:23 +00:00
$keysCount = count ( $keys );
if (( 1 === $keysCount && '0' == $keys [ 0 ])
|| ( $keysCount > 1 && array_reduce ( $keys , function ( $v , $w ) { return ( int ) $v + $w ; }, 0 ) === $keysCount * ( $keysCount - 1 ) / 2 )
2010-05-08 14:53:22 +01:00
) {
2010-05-06 12:25:53 +01:00
$output = array ();
2010-05-07 15:09:11 +01:00
foreach ( $value as $val ) {
2016-02-09 19:56:50 +00:00
$output [] = self :: dump ( $val , $flags );
2010-05-06 12:25:53 +01:00
}
return sprintf ( '[%s]' , implode ( ', ' , $output ));
}
2010-01-04 14:26:20 +00:00
2010-05-06 12:25:53 +01:00
// mapping
$output = array ();
2010-05-07 15:09:11 +01:00
foreach ( $value as $key => $val ) {
2016-02-09 19:56:50 +00:00
$output [] = sprintf ( '%s: %s' , self :: dump ( $key , $flags ), self :: dump ( $val , $flags ));
2010-05-06 12:25:53 +01:00
}
2010-01-04 14:26:20 +00:00
2010-05-06 12:25:53 +01:00
return sprintf ( '{ %s }' , implode ( ', ' , $output ));
2010-01-04 14:26:20 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* Parses a scalar to a YAML string .
*
2014-12-29 22:41:34 +00:00
* @ param string $scalar
2016-02-17 21:09:28 +00:00
* @ param int $flags
2012-05-18 18:41:48 +01:00
* @ param string $delimiters
* @ param array $stringDelimiters
2014-08-15 13:16:41 +01:00
* @ param int & $i
* @ param bool $evaluate
* @ param array $references
2010-05-06 12:25:53 +01:00
*
* @ return string A YAML string
*
2011-06-14 11:44:54 +01:00
* @ throws ParseException When malformed inline YAML string is parsed
2015-10-12 12:16:35 +01:00
*
* @ internal
2010-05-06 12:25:53 +01:00
*/
2016-02-17 21:09:28 +00:00
public static function parseScalar ( $scalar , $flags = 0 , $delimiters = null , $stringDelimiters = array ( '"' , " ' " ), & $i = 0 , $evaluate = true , $references = array ())
2010-01-04 14:26:20 +00:00
{
2010-05-07 15:09:11 +01:00
if ( in_array ( $scalar [ $i ], $stringDelimiters )) {
2010-05-06 12:25:53 +01:00
// quoted scalar
$output = self :: parseQuotedScalar ( $scalar , $i );
2012-06-13 13:57:32 +01:00
if ( null !== $delimiters ) {
$tmp = ltrim ( substr ( $scalar , $i ), ' ' );
if ( ! in_array ( $tmp [ 0 ], $delimiters )) {
throw new ParseException ( sprintf ( 'Unexpected characters (%s).' , substr ( $scalar , $i )));
}
}
2010-05-07 15:09:11 +01:00
} else {
2010-05-06 12:25:53 +01:00
// "normal" string
2010-05-07 15:09:11 +01:00
if ( ! $delimiters ) {
2010-05-06 12:25:53 +01:00
$output = substr ( $scalar , $i );
$i += strlen ( $output );
// remove comments
2015-09-14 16:56:48 +01:00
if ( preg_match ( '/[ \t]+#/' , $output , $match , PREG_OFFSET_CAPTURE )) {
$output = substr ( $output , 0 , $match [ 0 ][ 1 ]);
2010-05-06 12:25:53 +01:00
}
2011-12-18 13:42:59 +00:00
} elseif ( preg_match ( '/^(.+?)(' . implode ( '|' , $delimiters ) . ')/' , substr ( $scalar , $i ), $match )) {
2010-05-06 12:25:53 +01:00
$output = $match [ 1 ];
$i += strlen ( $output );
2010-05-07 15:09:11 +01:00
} else {
2011-06-14 11:44:54 +01:00
throw new ParseException ( sprintf ( 'Malformed inline YAML string (%s).' , $scalar ));
2010-01-04 14:26:20 +00:00
}
2015-11-03 07:24:46 +00:00
// a non-quoted string cannot start with @ or ` (reserved) nor with a scalar indicator (| or >)
if ( $output && ( '@' === $output [ 0 ] || '`' === $output [ 0 ] || '|' === $output [ 0 ] || '>' === $output [ 0 ])) {
2015-11-10 10:42:06 +00:00
throw new ParseException ( sprintf ( 'The reserved indicator "%s" cannot start a plain scalar; you need to quote the scalar.' , $output [ 0 ]));
2015-10-19 10:47:48 +01:00
}
2016-02-15 17:14:40 +00:00
if ( $output && '%' === $output [ 0 ]) {
@ trigger_error ( 'Not quoting a scalar starting with the "%" indicator character is deprecated since Symfony 3.1 and will throw a ParseException in 4.0.' , E_USER_DEPRECATED );
}
2014-02-25 00:33:01 +00:00
if ( $evaluate ) {
2016-02-17 21:09:28 +00:00
$output = self :: evaluateScalar ( $output , $flags , $references );
2014-02-25 00:33:01 +00:00
}
2010-05-06 12:25:53 +01:00
}
2010-01-04 14:26:20 +00:00
2010-05-06 12:25:53 +01:00
return $output ;
2010-01-04 14:26:20 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* Parses a quoted scalar to YAML .
*
2012-05-18 18:41:48 +01:00
* @ param string $scalar
2014-01-23 16:32:31 +00:00
* @ param int & $i
2010-05-06 12:25:53 +01:00
*
* @ return string A YAML string
*
2011-06-14 11:44:54 +01:00
* @ throws ParseException When malformed inline YAML string is parsed
2010-05-06 12:25:53 +01:00
*/
2012-07-09 13:50:58 +01:00
private static function parseQuotedScalar ( $scalar , & $i )
2010-01-04 14:26:20 +00:00
{
2012-06-13 13:57:32 +01:00
if ( ! preg_match ( '/' . self :: REGEX_QUOTED_STRING . '/Au' , substr ( $scalar , $i ), $match )) {
2011-06-14 11:44:54 +01:00
throw new ParseException ( sprintf ( 'Malformed inline YAML string (%s).' , substr ( $scalar , $i )));
2010-01-04 14:26:20 +00:00
}
2010-05-06 12:25:53 +01:00
$output = substr ( $match [ 0 ], 1 , strlen ( $match [ 0 ]) - 2 );
2010-01-04 14:26:20 +00:00
2011-03-05 21:39:48 +00:00
$unescaper = new Unescaper ();
2010-05-07 15:09:11 +01:00
if ( '"' == $scalar [ $i ]) {
2011-03-05 21:39:48 +00:00
$output = $unescaper -> unescapeDoubleQuotedString ( $output );
2010-05-07 15:09:11 +01:00
} else {
2011-03-05 21:39:48 +00:00
$output = $unescaper -> unescapeSingleQuotedString ( $output );
2010-05-06 12:25:53 +01:00
}
$i += strlen ( $match [ 0 ]);
return $output ;
2010-01-04 14:26:20 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* Parses a sequence to a YAML string .
*
2012-05-18 18:41:48 +01:00
* @ param string $sequence
2016-02-17 21:09:28 +00:00
* @ param int $flags
2014-08-15 13:16:41 +01:00
* @ param int & $i
* @ param array $references
2010-05-06 12:25:53 +01:00
*
* @ return string A YAML string
*
2014-03-27 09:46:24 +00:00
* @ throws ParseException When malformed inline YAML string is parsed
2010-05-06 12:25:53 +01:00
*/
2016-02-17 21:09:28 +00:00
private static function parseSequence ( $sequence , $flags , & $i = 0 , $references = array ())
2010-05-06 12:25:53 +01:00
{
$output = array ();
$len = strlen ( $sequence );
2015-03-06 19:37:36 +00:00
++ $i ;
2010-05-06 12:25:53 +01:00
// [foo, bar, ...]
2010-05-07 15:09:11 +01:00
while ( $i < $len ) {
2010-05-08 14:32:30 +01:00
switch ( $sequence [ $i ]) {
2010-05-06 12:25:53 +01:00
case '[' :
// nested sequence
2016-02-17 21:09:28 +00:00
$output [] = self :: parseSequence ( $sequence , $flags , $i , $references );
2010-05-06 12:25:53 +01:00
break ;
case '{' :
// nested mapping
2016-02-17 21:09:28 +00:00
$output [] = self :: parseMapping ( $sequence , $flags , $i , $references );
2010-05-06 12:25:53 +01:00
break ;
case ']' :
return $output ;
case ',' :
case ' ' :
break ;
default :
$isQuoted = in_array ( $sequence [ $i ], array ( '"' , " ' " ));
2016-02-17 21:09:28 +00:00
$value = self :: parseScalar ( $sequence , $flags , array ( ',' , ']' ), array ( '"' , " ' " ), $i , true , $references );
2010-05-06 12:25:53 +01:00
2014-08-15 13:16:41 +01:00
// the value can be an array if a reference has been resolved to an array var
if ( ! is_array ( $value ) && ! $isQuoted && false !== strpos ( $value , ': ' )) {
2010-05-06 12:25:53 +01:00
// embedded mapping?
2010-05-07 15:09:11 +01:00
try {
2014-08-15 13:16:41 +01:00
$pos = 0 ;
2016-02-17 21:09:28 +00:00
$value = self :: parseMapping ( '{' . $value . '}' , $flags , $pos , $references );
2010-05-07 15:09:11 +01:00
} catch ( \InvalidArgumentException $e ) {
2010-05-06 12:25:53 +01:00
// no, it's not
}
}
$output [] = $value ;
-- $i ;
}
2010-01-04 14:26:20 +00:00
2010-05-06 12:25:53 +01:00
++ $i ;
}
2010-01-04 14:26:20 +00:00
2011-06-14 11:44:54 +01:00
throw new ParseException ( sprintf ( 'Malformed inline YAML string %s' , $sequence ));
2010-05-06 12:25:53 +01:00
}
2010-01-04 14:26:20 +00:00
2010-05-06 12:25:53 +01:00
/**
* Parses a mapping to a YAML string .
*
2012-05-18 18:41:48 +01:00
* @ param string $mapping
2016-02-17 21:09:28 +00:00
* @ param int $flags
2014-08-15 13:16:41 +01:00
* @ param int & $i
* @ param array $references
2010-05-06 12:25:53 +01:00
*
* @ return string A YAML string
*
2014-03-27 09:46:24 +00:00
* @ throws ParseException When malformed inline YAML string is parsed
2010-05-06 12:25:53 +01:00
*/
2016-02-17 21:09:28 +00:00
private static function parseMapping ( $mapping , $flags , & $i = 0 , $references = array ())
2010-01-04 14:26:20 +00:00
{
2010-05-06 12:25:53 +01:00
$output = array ();
$len = strlen ( $mapping );
2015-03-06 19:37:36 +00:00
++ $i ;
2010-05-06 12:25:53 +01:00
// {foo: bar, bar:foo, ...}
2010-05-07 15:09:11 +01:00
while ( $i < $len ) {
2010-05-08 14:32:30 +01:00
switch ( $mapping [ $i ]) {
2010-05-06 12:25:53 +01:00
case ' ' :
case ',' :
++ $i ;
continue 2 ;
case '}' :
2014-03-27 09:46:24 +00:00
if ( self :: $objectForMap ) {
2014-01-23 16:32:31 +00:00
return ( object ) $output ;
}
2010-05-06 12:25:53 +01:00
return $output ;
}
// key
2016-02-17 21:09:28 +00:00
$key = self :: parseScalar ( $mapping , $flags , array ( ':' , ' ' ), array ( '"' , " ' " ), $i , false );
2010-05-06 12:25:53 +01:00
// value
$done = false ;
2014-01-23 16:32:31 +00:00
2010-05-07 15:09:11 +01:00
while ( $i < $len ) {
2010-05-08 14:32:30 +01:00
switch ( $mapping [ $i ]) {
2010-05-06 12:25:53 +01:00
case '[' :
// nested sequence
2016-02-17 21:09:28 +00:00
$value = self :: parseSequence ( $mapping , $flags , $i , $references );
2014-05-15 01:05:21 +01:00
// Spec: Keys MUST be unique; first one wins.
// Parser cannot abort this mapping earlier, since lines
// are processed sequentially.
if ( ! isset ( $output [ $key ])) {
$output [ $key ] = $value ;
}
2010-05-06 12:25:53 +01:00
$done = true ;
break ;
case '{' :
// nested mapping
2016-02-17 21:09:28 +00:00
$value = self :: parseMapping ( $mapping , $flags , $i , $references );
2014-05-15 01:05:21 +01:00
// Spec: Keys MUST be unique; first one wins.
// Parser cannot abort this mapping earlier, since lines
// are processed sequentially.
if ( ! isset ( $output [ $key ])) {
$output [ $key ] = $value ;
}
2010-05-06 12:25:53 +01:00
$done = true ;
break ;
case ':' :
case ' ' :
break ;
default :
2016-02-17 21:09:28 +00:00
$value = self :: parseScalar ( $mapping , $flags , array ( ',' , '}' ), array ( '"' , " ' " ), $i , true , $references );
2014-05-15 01:05:21 +01:00
// Spec: Keys MUST be unique; first one wins.
// Parser cannot abort this mapping earlier, since lines
// are processed sequentially.
if ( ! isset ( $output [ $key ])) {
$output [ $key ] = $value ;
}
2010-05-06 12:25:53 +01:00
$done = true ;
-- $i ;
}
++ $i ;
2010-05-07 15:09:11 +01:00
if ( $done ) {
2010-05-06 12:25:53 +01:00
continue 2 ;
}
}
}
2011-06-14 11:44:54 +01:00
throw new ParseException ( sprintf ( 'Malformed inline YAML string %s' , $mapping ));
2010-01-04 14:26:20 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* Evaluates scalars and replaces magic values .
*
* @ param string $scalar
2016-02-17 21:09:28 +00:00
* @ param int $flags
2014-08-15 13:16:41 +01:00
* @ param array $references
2010-05-06 12:25:53 +01:00
*
* @ return string A YAML string
2014-07-26 13:09:47 +01:00
*
2014-09-03 18:52:14 +01:00
* @ throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved
2010-05-06 12:25:53 +01:00
*/
2016-02-17 21:09:28 +00:00
private static function evaluateScalar ( $scalar , $flags , $references = array ())
2010-05-06 12:25:53 +01:00
{
$scalar = trim ( $scalar );
2014-02-22 14:29:19 +00:00
$scalarLower = strtolower ( $scalar );
2014-08-15 13:16:41 +01:00
if ( 0 === strpos ( $scalar , '*' )) {
if ( false !== $pos = strpos ( $scalar , '#' )) {
$value = substr ( $scalar , 1 , $pos - 2 );
} else {
$value = substr ( $scalar , 1 );
}
2014-09-03 18:52:14 +01:00
// an unquoted *
if ( false === $value || '' === $value ) {
throw new ParseException ( 'A reference must contain at least one character.' );
}
2014-08-15 13:16:41 +01:00
if ( ! array_key_exists ( $value , $references )) {
throw new ParseException ( sprintf ( 'Reference "%s" does not exist.' , $value ));
}
return $references [ $value ];
}
2010-05-07 15:09:11 +01:00
switch ( true ) {
2014-02-25 00:33:01 +00:00
case 'null' === $scalarLower :
case '' === $scalar :
case '~' === $scalar :
2014-04-16 08:15:58 +01:00
return ;
2014-02-22 14:29:19 +00:00
case 'true' === $scalarLower :
2010-05-06 12:25:53 +01:00
return true ;
2014-02-22 14:29:19 +00:00
case 'false' === $scalarLower :
2010-05-06 12:25:53 +01:00
return false ;
2014-02-22 14:29:19 +00:00
// Optimise for returning strings.
case $scalar [ 0 ] === '+' || $scalar [ 0 ] === '-' || $scalar [ 0 ] === '.' || $scalar [ 0 ] === '!' || is_numeric ( $scalar [ 0 ]) :
switch ( true ) {
case 0 === strpos ( $scalar , '!str' ) :
return ( string ) substr ( $scalar , 5 );
case 0 === strpos ( $scalar , '! ' ) :
2016-02-17 21:09:28 +00:00
return ( int ) self :: parseScalar ( substr ( $scalar , 2 ), $flags );
2016-01-20 19:11:57 +00:00
case 0 === strpos ( $scalar , '!php/object:' ) :
if ( self :: $objectSupport ) {
return unserialize ( substr ( $scalar , 12 ));
}
if ( self :: $exceptionOnInvalidType ) {
throw new ParseException ( 'Object support when parsing a YAML file has been disabled.' );
}
return ;
2014-02-22 14:29:19 +00:00
case 0 === strpos ( $scalar , '!!php/object:' ) :
if ( self :: $objectSupport ) {
2016-01-20 19:22:01 +00:00
@ trigger_error ( 'The !!php/object tag to indicate dumped PHP objects is deprecated since version 3.1 and will be removed in 4.0. Use the !php/object tag instead.' , E_USER_DEPRECATED );
2014-02-22 14:29:19 +00:00
return unserialize ( substr ( $scalar , 13 ));
}
if ( self :: $exceptionOnInvalidType ) {
throw new ParseException ( 'Object support when parsing a YAML file has been disabled.' );
}
2014-04-16 08:15:58 +01:00
return ;
2014-09-21 20:22:48 +01:00
case 0 === strpos ( $scalar , '!!float ' ) :
return ( float ) substr ( $scalar , 8 );
2014-02-22 14:29:19 +00:00
case ctype_digit ( $scalar ) :
$raw = $scalar ;
2015-03-07 19:12:23 +00:00
$cast = ( int ) $scalar ;
2014-02-22 14:29:19 +00:00
return '0' == $scalar [ 0 ] ? octdec ( $scalar ) : ((( string ) $raw == ( string ) $cast ) ? $cast : $raw );
case '-' === $scalar [ 0 ] && ctype_digit ( substr ( $scalar , 1 )) :
$raw = $scalar ;
2015-03-07 19:12:23 +00:00
$cast = ( int ) $scalar ;
2014-02-22 14:29:19 +00:00
2015-03-07 19:12:23 +00:00
return '0' == $scalar [ 1 ] ? octdec ( $scalar ) : ((( string ) $raw === ( string ) $cast ) ? $cast : $raw );
2014-02-22 14:29:19 +00:00
case is_numeric ( $scalar ) :
2015-03-30 08:37:12 +01:00
case preg_match ( self :: getHexRegex (), $scalar ) :
2015-03-07 19:12:23 +00:00
return '0x' === $scalar [ 0 ] . $scalar [ 1 ] ? hexdec ( $scalar ) : ( float ) $scalar ;
2014-02-25 00:33:01 +00:00
case '.inf' === $scalarLower :
case '.nan' === $scalarLower :
2014-02-22 14:29:19 +00:00
return - log ( 0 );
2014-02-25 00:33:01 +00:00
case '-.inf' === $scalarLower :
2014-02-22 14:29:19 +00:00
return log ( 0 );
2016-02-20 09:54:12 +00:00
case 0 === strpos ( $scalar , '!!binary ' ) :
return self :: evaluateBinaryScalar ( substr ( $scalar , 9 ));
2014-02-22 14:29:19 +00:00
case preg_match ( '/^(-|\+)?[0-9,]+(\.[0-9]+)?$/' , $scalar ) :
2015-03-07 19:12:23 +00:00
return ( float ) str_replace ( ',' , '' , $scalar );
2014-02-22 14:29:19 +00:00
case preg_match ( self :: getTimestampRegex (), $scalar ) :
2016-02-17 21:09:28 +00:00
if ( Yaml :: PARSE_DATETIME & $flags ) {
return new \DateTime ( $scalar , new \DateTimeZone ( 'UTC' ));
}
2016-02-17 21:15:58 +00:00
$timeZone = date_default_timezone_get ();
date_default_timezone_set ( 'UTC' );
$time = strtotime ( $scalar );
date_default_timezone_set ( $timeZone );
return $time ;
2014-02-22 14:29:19 +00:00
}
2010-05-06 12:25:53 +01:00
default :
return ( string ) $scalar ;
}
}
2016-02-20 09:54:12 +00:00
/**
* @ param string $scalar
*
* @ return string
*
* @ internal
*/
public static function evaluateBinaryScalar ( $scalar )
{
$parsedBinaryData = self :: parseScalar ( preg_replace ( '/\s/' , '' , $scalar ));
if ( 0 !== ( strlen ( $parsedBinaryData ) % 4 )) {
throw new ParseException ( sprintf ( 'The normalized base64 encoded data (data without whitespace characters) length must be a multiple of four (%d bytes given).' , strlen ( $parsedBinaryData )));
}
if ( ! preg_match ( '#^[A-Z0-9+/]+={0,2}$#i' , $parsedBinaryData )) {
throw new ParseException ( sprintf ( 'The base64 encoded data (%s) contains invalid characters.' , $parsedBinaryData ));
}
return base64_decode ( $parsedBinaryData , true );
}
private static function isBinaryString ( $value )
{
return preg_match ( '/[^\x09-\x0d\x20-\xff]/' , $value );
}
2011-02-07 12:57:27 +00:00
/**
2012-12-15 13:02:34 +00:00
* Gets a regex that matches a YAML date .
2011-02-07 12:57:27 +00:00
*
* @ return string The regular expression
2012-12-15 13:02:34 +00:00
*
* @ see http :// www . yaml . org / spec / 1.2 / spec . html #id2761573
2011-02-07 12:57:27 +00:00
*/
2012-07-09 13:50:58 +01:00
private static function getTimestampRegex ()
2010-05-06 12:25:53 +01:00
{
return <<< EOF
~^
( ? P < year > [ 0 - 9 ][ 0 - 9 ][ 0 - 9 ][ 0 - 9 ])
- ( ? P < month > [ 0 - 9 ][ 0 - 9 ] ? )
- ( ? P < day > [ 0 - 9 ][ 0 - 9 ] ? )
( ? : ( ? : [ Tt ] | [ \t ] + )
( ? P < hour > [ 0 - 9 ][ 0 - 9 ] ? )
: ( ? P < minute > [ 0 - 9 ][ 0 - 9 ])
: ( ? P < second > [ 0 - 9 ][ 0 - 9 ])
( ? : \ . ( ? P < fraction > [ 0 - 9 ] * )) ?
( ? : [ \t ] * ( ? P < tz > Z | ( ? P < tz_sign > [ -+ ])( ? P < tz_hour > [ 0 - 9 ][ 0 - 9 ] ? )
( ? :: ( ? P < tz_minute > [ 0 - 9 ][ 0 - 9 ])) ? )) ? ) ?
$ ~ x
2010-01-04 14:26:20 +00:00
EOF ;
2010-05-06 12:25:53 +01:00
}
2015-03-30 08:37:12 +01:00
/**
* Gets a regex that matches a YAML number in hexadecimal notation .
*
* @ return string
*/
private static function getHexRegex ()
{
return '~^0x[0-9a-f]++$~i' ;
}
2010-01-04 14:26:20 +00:00
}