1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | * This file is part of the Symfony package. |
---|
5 | * (c) Fabien Potencier <fabien@symfony.com> |
---|
6 | * |
---|
7 | * For the full copyright and license information, please view the LICENSE |
---|
8 | * file that was distributed with this source code. |
---|
9 | */ |
---|
10 | |
---|
11 | namespace Symfony\Component\Yaml; |
---|
12 | |
---|
13 | /** |
---|
14 | * Escaper encapsulates escaping rules for single and double-quoted |
---|
15 | * YAML strings. |
---|
16 | * |
---|
17 | * @author Matthew Lewinski <matthew@lewinski.org> |
---|
18 | */ |
---|
19 | class Escaper |
---|
20 | { |
---|
21 | // Characters that would cause a dumped string to require double quoting. |
---|
22 | const REGEX_CHARACTER_TO_ESCAPE = "[\\x00-\\x1f]|\xc2\x85|\xc2\xa0|\xe2\x80\xa8|\xe2\x80\xa9"; |
---|
23 | |
---|
24 | // Mapping arrays for escaping a double quoted string. The backslash is |
---|
25 | // first to ensure proper escaping because str_replace operates iteratively |
---|
26 | // on the input arrays. This ordering of the characters avoids the use of strtr, |
---|
27 | // which performs more slowly. |
---|
28 | static private $escapees = array('\\\\', '\\"', |
---|
29 | "\x00", "\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07", |
---|
30 | "\x08", "\x09", "\x0a", "\x0b", "\x0c", "\x0d", "\x0e", "\x0f", |
---|
31 | "\x10", "\x11", "\x12", "\x13", "\x14", "\x15", "\x16", "\x17", |
---|
32 | "\x18", "\x19", "\x1a", "\x1b", "\x1c", "\x1d", "\x1e", "\x1f", |
---|
33 | "\xc2\x85", "\xc2\xa0", "\xe2\x80\xa8", "\xe2\x80\xa9"); |
---|
34 | static private $escaped = array('\\"', '\\\\', |
---|
35 | "\\0", "\\x01", "\\x02", "\\x03", "\\x04", "\\x05", "\\x06", "\\a", |
---|
36 | "\\b", "\\t", "\\n", "\\v", "\\f", "\\r", "\\x0e", "\\x0f", |
---|
37 | "\\x10", "\\x11", "\\x12", "\\x13", "\\x14", "\\x15", "\\x16", "\\x17", |
---|
38 | "\\x18", "\\x19", "\\x1a", "\\e", "\\x1c", "\\x1d", "\\x1e", "\\x1f", |
---|
39 | "\\N", "\\_", "\\L", "\\P"); |
---|
40 | |
---|
41 | /** |
---|
42 | * Determines if a PHP value would require double quoting in YAML. |
---|
43 | * |
---|
44 | * @param string $value A PHP value |
---|
45 | * |
---|
46 | * @return Boolean True if the value would require double quotes. |
---|
47 | */ |
---|
48 | static public function requiresDoubleQuoting($value) |
---|
49 | { |
---|
50 | return preg_match('/'.self::REGEX_CHARACTER_TO_ESCAPE.'/u', $value); |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * Escapes and surrounds a PHP value with double quotes. |
---|
55 | * |
---|
56 | * @param string $value A PHP value |
---|
57 | * |
---|
58 | * @return string The quoted, escaped string |
---|
59 | */ |
---|
60 | static public function escapeWithDoubleQuotes($value) |
---|
61 | { |
---|
62 | return sprintf('"%s"', str_replace(self::$escapees, self::$escaped, $value)); |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * Determines if a PHP value would require single quoting in YAML. |
---|
67 | * |
---|
68 | * @param string $value A PHP value |
---|
69 | * |
---|
70 | * @return Boolean True if the value would require single quotes. |
---|
71 | */ |
---|
72 | static public function requiresSingleQuoting($value) |
---|
73 | { |
---|
74 | return preg_match('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ - ? | < > = ! % @ ` ]/x', $value); |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * Escapes and surrounds a PHP value with single quotes. |
---|
79 | * |
---|
80 | * @param string $value A PHP value |
---|
81 | * |
---|
82 | * @return string The quoted, escaped string |
---|
83 | */ |
---|
84 | static public function escapeWithSingleQuotes($value) |
---|
85 | { |
---|
86 | return sprintf("'%s'", str_replace('\'', '\'\'', $value)); |
---|
87 | } |
---|
88 | } |
---|