From 3760e67cb234b940abddf5d348eb6e62411d62eb Mon Sep 17 00:00:00 2001 From: Peter Thompson Date: Mon, 5 Jan 2015 11:24:54 +0100 Subject: [PATCH 1/4] [Yaml] Improve YAML boolean escaping - Moves dumping single-quoting logic into Yaml\Escaper - Ensures that PHP values which would be interpreted as booleans in older versions of the YAML spec are escaped with single quotes when dumped by the Dumper. --- src/Symfony/Component/Yaml/Escaper.php | 26 ++++++++++++++++++- src/Symfony/Component/Yaml/Inline.php | 4 +-- .../Component/Yaml/Tests/InlineTest.php | 16 ++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Yaml/Escaper.php b/src/Symfony/Component/Yaml/Escaper.php index 6f9785886f14..e4a7f07f34af 100644 --- a/src/Symfony/Component/Yaml/Escaper.php +++ b/src/Symfony/Component/Yaml/Escaper.php @@ -72,7 +72,7 @@ public static function escapeWithDoubleQuotes($value) */ public static function requiresSingleQuoting($value) { - return preg_match('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ \- ? | < > = ! % @ ` ]/x', $value); + return self::containsCharRequiresSingleQuoting($value) || self::isValueRequiresSingleQuoting($value); } /** @@ -86,4 +86,28 @@ public static function escapeWithSingleQuotes($value) { return sprintf("'%s'", str_replace('\'', '\'\'', $value)); } + + /** + * Determines if a PHP value contains any single characters that would cause + * the value to require single quoting in YAML. + * + * @param string $value A PHP value + * @return bool True if the value would require single quotes. + */ + private static function containsCharRequiresSingleQuoting($value) + { + return preg_match('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ \- ? | < > = ! % @ ` ]/x', $value); + } + + /** + * Determines if a PHP value is entirely composed of a value that would + * require require single quoting in YAML. + * + * @param string $value A PHP value + * @return bool True if the value would require single quotes. + */ + private static function isValueRequiresSingleQuoting($value) + { + return in_array(strtolower($value), array('null', '~', 'true', 'false', 'y', 'n', 'yes', 'no', 'on', 'off')); + } } diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index df873bf2974a..1bdcf8762340 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -135,12 +135,10 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp case Escaper::requiresDoubleQuoting($value): return Escaper::escapeWithDoubleQuotes($value); case Escaper::requiresSingleQuoting($value): + case preg_match(self::getTimestampRegex(), $value): return Escaper::escapeWithSingleQuotes($value); case '' == $value: return "''"; - case preg_match(self::getTimestampRegex(), $value): - case in_array(strtolower($value), array('null', '~', 'true', 'false')): - return "'$value'"; default: return $value; } diff --git a/src/Symfony/Component/Yaml/Tests/InlineTest.php b/src/Symfony/Component/Yaml/Tests/InlineTest.php index 31572481a583..0cbea85ded81 100644 --- a/src/Symfony/Component/Yaml/Tests/InlineTest.php +++ b/src/Symfony/Component/Yaml/Tests/InlineTest.php @@ -190,6 +190,14 @@ protected function getTestsForParse() "'#cfcfcf'" => '#cfcfcf', '::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-30T02:59:43Z' => 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 @@ protected function getTestsForDump() "'-dash'" => '-dash', "'-'" => '-', + // Pre-YAML-1.2 booleans + "'y'" => 'y', + "'n'" => 'n', + "'yes'" => 'yes', + "'no'" => 'no', + "'on'" => 'on', + "'off'" => 'off', + // sequences '[foo, bar, false, null, 12]' => array('foo', 'bar', false, null, 12), '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'), From 81a8090dde6c997a71d87604d4a421f58b514ca0 Mon Sep 17 00:00:00 2001 From: Lee Rowlands Date: Tue, 6 Jan 2015 14:18:13 +1000 Subject: [PATCH 2/4] Remove duplicate 'require' --- src/Symfony/Component/Yaml/Escaper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Yaml/Escaper.php b/src/Symfony/Component/Yaml/Escaper.php index e4a7f07f34af..e3039f57e9bd 100644 --- a/src/Symfony/Component/Yaml/Escaper.php +++ b/src/Symfony/Component/Yaml/Escaper.php @@ -101,7 +101,7 @@ private static function containsCharRequiresSingleQuoting($value) /** * Determines if a PHP value is entirely composed of a value that would - * require require single quoting in YAML. + * require single quoting in YAML. * * @param string $value A PHP value * @return bool True if the value would require single quotes. From a0ec0fe40240f8737bae4692b618c534bb4cd2a4 Mon Sep 17 00:00:00 2001 From: Lee Rowlands Date: Tue, 6 Jan 2015 19:37:09 +1000 Subject: [PATCH 3/4] Add comment as requested --- src/Symfony/Component/Yaml/Escaper.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Symfony/Component/Yaml/Escaper.php b/src/Symfony/Component/Yaml/Escaper.php index e3039f57e9bd..be846e50c7eb 100644 --- a/src/Symfony/Component/Yaml/Escaper.php +++ b/src/Symfony/Component/Yaml/Escaper.php @@ -108,6 +108,8 @@ private static function containsCharRequiresSingleQuoting($value) */ private static function isValueRequiresSingleQuoting($value) { + // Note that whilst 'y' and 'n' are not supported as valid Booleans, + // they are escaped here for interoperability. return in_array(strtolower($value), array('null', '~', 'true', 'false', 'y', 'n', 'yes', 'no', 'on', 'off')); } } From 8fa056bc95abe1c323be5460fda446329a402185 Mon Sep 17 00:00:00 2001 From: pthompson Date: Fri, 16 Jan 2015 14:12:56 +0100 Subject: [PATCH 4/4] Inline private 'is quoting required' methods in Escaper --- src/Symfony/Component/Yaml/Escaper.php | 36 +++++++------------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/src/Symfony/Component/Yaml/Escaper.php b/src/Symfony/Component/Yaml/Escaper.php index be846e50c7eb..2820778feac9 100644 --- a/src/Symfony/Component/Yaml/Escaper.php +++ b/src/Symfony/Component/Yaml/Escaper.php @@ -72,7 +72,15 @@ public static function escapeWithDoubleQuotes($value) */ public static function requiresSingleQuoting($value) { - return self::containsCharRequiresSingleQuoting($value) || self::isValueRequiresSingleQuoting($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')); } /** @@ -86,30 +94,4 @@ public static function escapeWithSingleQuotes($value) { return sprintf("'%s'", str_replace('\'', '\'\'', $value)); } - - /** - * Determines if a PHP value contains any single characters that would cause - * the value to require single quoting in YAML. - * - * @param string $value A PHP value - * @return bool True if the value would require single quotes. - */ - private static function containsCharRequiresSingleQuoting($value) - { - return preg_match('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ \- ? | < > = ! % @ ` ]/x', $value); - } - - /** - * Determines if a PHP value is entirely composed of a value that would - * require single quoting in YAML. - * - * @param string $value A PHP value - * @return bool True if the value would require single quotes. - */ - private static function isValueRequiresSingleQuoting($value) - { - // Note that whilst 'y' and 'n' are not supported as valid Booleans, - // they are escaped here for interoperability. - return in_array(strtolower($value), array('null', '~', 'true', 'false', 'y', 'n', 'yes', 'no', 'on', 'off')); - } }