Escaper.php 3.4 KB

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