8000 [Yaml] Improve YAML boolean escaping · symfony/symfony@3760e67 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3760e67

Browse files
committed
[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.
1 parent b4c1e89 commit 3760e67

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

src/Symfony/Component/Yaml/Escaper.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static function escapeWithDoubleQuotes($value)
7272
*/
7373
public static function requiresSingleQuoting($value)
7474
{
75-
return preg_match('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ \- ? | < > = ! % @ ` ]/x', $value);
75+
return self::containsCharRequiresSingleQuoting($value) || self::isValueRequiresSingleQuoting($value);
7676
}
7777

7878
/**
@@ -86,4 +86,28 @@ public static function escapeWithSingleQuotes($value)
8686
{
8787
return sprintf("'%s'", str_replace('\'', '\'\'', $value));
8888
}
89+
90+
/**
91+
* Determines if a PHP value contains any single characters that would cause
92+
* the value to require single quoting in YAML.
93+
*
94+
* @param string $value A PHP value
95+
* @return bool True if the value would require single quotes.
96+
*/
97+
private static function containsCharRequiresSingleQuoting($value)
98+
{
99+
return preg_match('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ \- ? | < > = ! % @ ` ]/x', $value);
100+
}
101+
102+
/**
103+
* Determines if a PHP value is entirely composed of a value that would
104+
* require require single quoting in YAML.
105+
*
106+
* @param string $value A PHP value
107+
* @return bool True if the value would require single quotes.
108+
*/
109+
private static function isValueRequiresSingleQuoting($value)
110+
{
111+
return in_array(strtolower($value), array('null', '~', 'true', 'false', 'y', 'n', 'yes', 'no', 'on', 'off'));
112+
}
89113
}

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