Debian Codegraph @

vendor/symfony/yaml/Escaper.php

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