bug #13262 [Yaml] Improve YAML boolean escaping (petert82, larowlan)
This PR was merged into the 2.3 branch. Discussion ---------- [Yaml] Improve YAML boolean escaping | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #13209 | License | MIT | Doc PR | None This PR ensures that PHP [values which would be interpreted as booleans][1] in older versions of the YAML spec are escaped with single quotes when dumped by the Dumper. For example, dumping this: ```php array( 'country_code' => 'no', 'speaks_norwegian' => 'y', 'heating' => 'on', ) ``` Will produce this YAML: ```yaml country_code: 'no' speaks_norwegian: 'y' heating: 'on' ``` [1]: http://yaml.org/type/bool.html Commits -------8fa056b
Inline private 'is quoting required' methods in Escaperafe827a
Merge pull request #2 from larowlan/patch-2a0ec0fe
Add comment as requested1e0633e
Merge pull request #1 from larowlan/patch-181a8090
Remove duplicate 'require'3760e67
[Yaml] Improve YAML boolean escaping
This commit is contained in:
commit
689cf99c4e
@ -72,7 +72,15 @@ class Escaper
|
|||||||
*/
|
*/
|
||||||
public static function requiresSingleQuoting($value)
|
public static function requiresSingleQuoting($value)
|
||||||
{
|
{
|
||||||
return preg_match('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ \- ? | < > = ! % @ ` ]/x', $value);
|
// Determines if the PHP value contains any single characters that would
|
||||||
|
// cause it to require single quoting in YAML.
|
||||||
|
if (preg_match('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ \- ? | < > = ! % @ ` ]/x', $value)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determines if a PHP value is entirely composed of a value that would
|
||||||
|
// require single quoting in YAML.
|
||||||
|
return in_array(strtolower($value), array('null', '~', 'true', 'false', 'y', 'n', 'yes', 'no', 'on', 'off'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -135,12 +135,10 @@ class Inline
|
|||||||
case Escaper::requiresDoubleQuoting($value):
|
case Escaper::requiresDoubleQuoting($value):
|
||||||
return Escaper::escapeWithDoubleQuotes($value);
|
return Escaper::escapeWithDoubleQuotes($value);
|
||||||
case Escaper::requiresSingleQuoting($value):
|
case Escaper::requiresSingleQuoting($value):
|
||||||
|
case preg_match(self::getTimestampRegex(), $value):
|
||||||
return Escaper::escapeWithSingleQuotes($value);
|
return Escaper::escapeWithSingleQuotes($value);
|
||||||
case '' == $value:
|
case '' == $value:
|
||||||
return "''";
|
return "''";
|
||||||
case preg_match(self::getTimestampRegex(), $value):
|
|
||||||
case in_array(strtolower($value), array('null', '~', 'true', 'false')):
|
|
||||||
return "'$value'";
|
|
||||||
default:
|
default:
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
@ -190,6 +190,14 @@ class InlineTest extends \PHPUnit_Framework_TestCase
|
|||||||
"'#cfcfcf'" => '#cfcfcf',
|
"'#cfcfcf'" => '#cfcfcf',
|
||||||
'::form_base.html.twig' => '::form_base.html.twig',
|
'::form_base.html.twig' => '::form_base.html.twig',
|
||||||
|
|
||||||
|
// Pre-YAML-1.2 booleans
|
||||||
|
"'y'" => 'y',
|
||||||
|
"'n'" => 'n',
|
||||||
|
"'yes'" => 'yes',
|
||||||
|
"'no'" => 'no',
|
||||||
|
"'on'" => 'on',
|
||||||
|
"'off'" => 'off',
|
||||||
|
|
||||||
'2007-10-30' => mktime(0, 0, 0, 10, 30, 2007),
|
'2007-10-30' => mktime(0, 0, 0, 10, 30, 2007),
|
||||||
'2007-10-30T02:59:43Z' => gmmktime(2, 59, 43, 10, 30, 2007),
|
'2007-10-30T02:59:43Z' => gmmktime(2, 59, 43, 10, 30, 2007),
|
||||||
'2007-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 2007),
|
'2007-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 2007),
|
||||||
@ -257,6 +265,14 @@ class InlineTest extends \PHPUnit_Framework_TestCase
|
|||||||
"'-dash'" => '-dash',
|
"'-dash'" => '-dash',
|
||||||
"'-'" => '-',
|
"'-'" => '-',
|
||||||
|
|
||||||
|
// Pre-YAML-1.2 booleans
|
||||||
|
"'y'" => 'y',
|
||||||
|
"'n'" => 'n',
|
||||||
|
"'yes'" => 'yes',
|
||||||
|
"'no'" => 'no',
|
||||||
|
"'on'" => 'on',
|
||||||
|
"'off'" => 'off',
|
||||||
|
|
||||||
// sequences
|
// sequences
|
||||||
'[foo, bar, false, null, 12]' => array('foo', 'bar', false, null, 12),
|
'[foo, bar, false, null, 12]' => array('foo', 'bar', false, null, 12),
|
||||||
'[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),
|
'[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),
|
||||||
|
Reference in New Issue
Block a user