Escaper.php 3.8 KB

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