8000 bug #13262 [Yaml] Improve YAML boolean escaping (petert82, larowlan) · symfony/symfony@689cf99 · GitHub
[go: up one dir, main page]

Skip to content

Commit 689cf99

Browse files
committed
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 Escaper afe827a Merge pull request #2 from larowlan/patch-2 a0ec0fe Add comment as requested 1e0633e Merge pull request #1 from larowlan/patch-1 81a8090 Remove duplicate 'require' 3760e67 [Yaml] Improve YAML boolean escaping
2 parents 62f3a29 + 8fa056b commit 689cf99

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

src/Symfony/Component/Yaml/Escaper.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,15 @@ public static function escapeWithDoubleQuotes($value)
7272
*/
7373
public static function requiresSingleQuoting($value)
7474
{
75-
return preg_match('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ \- ? | < > = ! % @ ` ]/x', $value);
75+
// Determines if the PHP value contains any single characters that would
76+
// cause it to require single quoting in YAML.
77+
if (preg_match('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ \- ? | < > = ! % @ ` ]/x', $value)) {
78+
return true;
79+
}
80+
81+
// Determines if a PHP value is entirely composed of a value that would
82+
// require single quoting in YAML.
83+
return in_array(strtolower($value), array('null', '~', 'true', 'false', 'y', 'n', 'yes', 'no', 'on', 'off'));
7684
}
7785

7886
/**

src/Symfony/Component/Yaml/Inline.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,10 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp
135135
case Escaper::requiresDoubleQuoting($value):
136136
return Escaper::escapeWithDoubleQuotes($value);
137137
case Escaper::requiresSingleQuoting($value):
138+
case preg_match(self::getTimestampRegex(), $value):
138139
return Escaper::escapeWithSingleQuotes($value);
139140
case '' == $value:
140141
return "''";
141-
case preg_match(self::getTimestampRegex(), $value):
142-
case in_array(strtolower($value), array('null', '~', 'true', 'false')):
143-
return "'$value'";
144142
default:
145143
return $value;
146144
}

src/Symfony/Component/Yaml/Tests/InlineTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,14 @@ protected function getTestsForParse()
190190
"'#cfcfcf'" => '#cfcfcf',
191191
'::form_base.html.twig' => '::form_base.html.twig',
192192

193+
// Pre-YAML-1.2 booleans
194+
"'y'" => 'y',
195+
"'n'" => 'n',
196+
"'yes'" => 'yes',
197+
"'no'" => 'no',
198+
"'on'" => 'on',
199+
"'off'" => 'off',
200+
193201
'2007-10-30' => mktime(0, 0, 0, 10, 30, 2007),
194202
'2007-10-30T02:59:43Z' => gmmktime(2, 59, 43, 10, 30, 2007),
195203
'2007-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 2007),
@@ -257,6 +265,14 @@ protected function getTestsForDump()
257265
"'-dash'" => '-dash',
258266
"'-'" => '-',
259267

268+
// Pre-YAML-1.2 booleans
269+
"'y'" => 'y',
270+
"'n'" => 'n',
271+
"'yes'" => 'yes',
272+
"'no'" => 'no',
273+
"'on'" => 'on',
274+
"'off'" => 'off',
275+
260276
// sequences
261277
'[foo, bar, false, null, 12]' => array('foo', 'bar', false, null, 12),
262278
'[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),

0 commit comments

Comments
 (0)
0