Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Yaml; Chris@0: Chris@0: /** Chris@0: * Escaper encapsulates escaping rules for single and double-quoted Chris@0: * YAML strings. Chris@0: * Chris@0: * @author Matthew Lewinski Chris@0: * Chris@0: * @internal Chris@0: */ Chris@0: class Escaper Chris@0: { Chris@0: // Characters that would cause a dumped string to require double quoting. Chris@0: const REGEX_CHARACTER_TO_ESCAPE = "[\\x00-\\x1f]|\xc2\x85|\xc2\xa0|\xe2\x80\xa8|\xe2\x80\xa9"; Chris@0: Chris@0: // Mapping arrays for escaping a double quoted string. The backslash is Chris@0: // first to ensure proper escaping because str_replace operates iteratively Chris@0: // on the input arrays. This ordering of the characters avoids the use of strtr, Chris@0: // which performs more slowly. Chris@17: private static $escapees = ['\\', '\\\\', '\\"', '"', Chris@0: "\x00", "\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07", Chris@0: "\x08", "\x09", "\x0a", "\x0b", "\x0c", "\x0d", "\x0e", "\x0f", Chris@0: "\x10", "\x11", "\x12", "\x13", "\x14", "\x15", "\x16", "\x17", Chris@0: "\x18", "\x19", "\x1a", "\x1b", "\x1c", "\x1d", "\x1e", "\x1f", Chris@0: "\xc2\x85", "\xc2\xa0", "\xe2\x80\xa8", "\xe2\x80\xa9", Chris@17: ]; Chris@17: private static $escaped = ['\\\\', '\\"', '\\\\', '\\"', Chris@0: '\\0', '\\x01', '\\x02', '\\x03', '\\x04', '\\x05', '\\x06', '\\a', Chris@0: '\\b', '\\t', '\\n', '\\v', '\\f', '\\r', '\\x0e', '\\x0f', Chris@0: '\\x10', '\\x11', '\\x12', '\\x13', '\\x14', '\\x15', '\\x16', '\\x17', Chris@0: '\\x18', '\\x19', '\\x1a', '\\e', '\\x1c', '\\x1d', '\\x1e', '\\x1f', Chris@0: '\\N', '\\_', '\\L', '\\P', Chris@17: ]; Chris@0: Chris@0: /** Chris@0: * Determines if a PHP value would require double quoting in YAML. Chris@0: * Chris@0: * @param string $value A PHP value Chris@0: * Chris@0: * @return bool True if the value would require double quotes Chris@0: */ Chris@0: public static function requiresDoubleQuoting($value) Chris@0: { Chris@0: return 0 < preg_match('/'.self::REGEX_CHARACTER_TO_ESCAPE.'/u', $value); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Escapes and surrounds a PHP value with double quotes. Chris@0: * Chris@0: * @param string $value A PHP value Chris@0: * Chris@0: * @return string The quoted, escaped string Chris@0: */ Chris@0: public static function escapeWithDoubleQuotes($value) Chris@0: { Chris@0: return sprintf('"%s"', str_replace(self::$escapees, self::$escaped, $value)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Determines if a PHP value would require single quoting in YAML. Chris@0: * Chris@0: * @param string $value A PHP value Chris@0: * Chris@0: * @return bool True if the value would require single quotes Chris@0: */ Chris@0: public static function requiresSingleQuoting($value) Chris@0: { Chris@0: // Determines if a PHP value is entirely composed of a value that would Chris@0: // require single quoting in YAML. Chris@17: if (\in_array(strtolower($value), ['null', '~', 'true', 'false', 'y', 'n', 'yes', 'no', 'on', 'off'])) { Chris@0: return true; Chris@0: } Chris@0: Chris@0: // Determines if the PHP value contains any single characters that would Chris@0: // cause it to require single quoting in YAML. Chris@0: return 0 < preg_match('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ \- ? | < > = ! % @ ` ]/x', $value); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Escapes and surrounds a PHP value with single quotes. Chris@0: * Chris@0: * @param string $value A PHP value Chris@0: * Chris@0: * @return string The quoted, escaped string Chris@0: */ Chris@0: public static function escapeWithSingleQuotes($value) Chris@0: { Chris@0: return sprintf("'%s'", str_replace('\'', '\'\'', $value)); Chris@0: } Chris@0: }