Escaper.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. public const REGEX_CHARACTER_TO_ESCAPE = "[\\x00-\\x1f]|\x7f|\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 const ESCAPEES = ['\\', '\\\\', '\\"', '"',
  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. "\x7f",
  33. "\xc2\x85", "\xc2\xa0", "\xe2\x80\xa8", "\xe2\x80\xa9",
  34. ];
  35. private const ESCAPED = ['\\\\', '\\"', '\\\\', '\\"',
  36. '\\0', '\\x01', '\\x02', '\\x03', '\\x04', '\\x05', '\\x06', '\\a',
  37. '\\b', '\\t', '\\n', '\\v', '\\f', '\\r', '\\x0e', '\\x0f',
  38. '\\x10', '\\x11', '\\x12', '\\x13', '\\x14', '\\x15', '\\x16', '\\x17',
  39. '\\x18', '\\x19', '\\x1a', '\\e', '\\x1c', '\\x1d', '\\x1e', '\\x1f',
  40. '\\x7f',
  41. '\\N', '\\_', '\\L', '\\P',
  42. ];
  43. /**
  44. * Determines if a PHP value would require double quoting in YAML.
  45. *
  46. * @param string $value A PHP value
  47. */
  48. public static function requiresDoubleQuoting(string $value): bool
  49. {
  50. return 0 < preg_match('/'.self::REGEX_CHARACTER_TO_ESCAPE.'/u', $value);
  51. }
  52. /**
  53. * Escapes and surrounds a PHP value with double quotes.
  54. *
  55. * @param string $value A PHP value
  56. */
  57. public static function escapeWithDoubleQuotes(string $value): string
  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. public static function requiresSingleQuoting(string $value): bool
  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), ['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 0 < preg_match('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ \- ? | < > = ! % @ ` \p{Zs}]/xu', $value);
  76. }
  77. /**
  78. * Escapes and surrounds a PHP value with single quotes.
  79. *
  80. * @param string $value A PHP value
  81. */
  82. public static function escapeWithSingleQuotes(string $value): string
  83. {
  84. return sprintf("'%s'", str_replace('\'', '\'\'', $value));
  85. }
  86. }