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