Escaper.php 3.8 KB

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