annotate vendor/symfony/yaml/Escaper.php @ 19:fa3358dc1485 tip

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