String_.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Node\Scalar;
  3. use PhpParser\Error;
  4. use PhpParser\Node\Scalar;
  5. class String_ extends Scalar
  6. {
  7. /* For use in "kind" attribute */
  8. const KIND_SINGLE_QUOTED = 1;
  9. const KIND_DOUBLE_QUOTED = 2;
  10. const KIND_HEREDOC = 3;
  11. const KIND_NOWDOC = 4;
  12. /** @var string String value */
  13. public $value;
  14. protected static $replacements = [
  15. '\\' => '\\',
  16. '$' => '$',
  17. 'n' => "\n",
  18. 'r' => "\r",
  19. 't' => "\t",
  20. 'f' => "\f",
  21. 'v' => "\v",
  22. 'e' => "\x1B",
  23. ];
  24. /**
  25. * Constructs a string scalar node.
  26. *
  27. * @param string $value Value of the string
  28. * @param array $attributes Additional attributes
  29. */
  30. public function __construct(string $value, array $attributes = []) {
  31. $this->attributes = $attributes;
  32. $this->value = $value;
  33. }
  34. public function getSubNodeNames() : array {
  35. return ['value'];
  36. }
  37. /**
  38. * @internal
  39. *
  40. * Parses a string token.
  41. *
  42. * @param string $str String token content
  43. * @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
  44. *
  45. * @return string The parsed string
  46. */
  47. public static function parse(string $str, bool $parseUnicodeEscape = true) : string {
  48. $bLength = 0;
  49. if ('b' === $str[0] || 'B' === $str[0]) {
  50. $bLength = 1;
  51. }
  52. if ('\'' === $str[$bLength]) {
  53. return str_replace(
  54. ['\\\\', '\\\''],
  55. ['\\', '\''],
  56. substr($str, $bLength + 1, -1)
  57. );
  58. } else {
  59. return self::parseEscapeSequences(
  60. substr($str, $bLength + 1, -1), '"', $parseUnicodeEscape
  61. );
  62. }
  63. }
  64. /**
  65. * @internal
  66. *
  67. * Parses escape sequences in strings (all string types apart from single quoted).
  68. *
  69. * @param string $str String without quotes
  70. * @param null|string $quote Quote type
  71. * @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
  72. *
  73. * @return string String with escape sequences parsed
  74. */
  75. public static function parseEscapeSequences(string $str, $quote, bool $parseUnicodeEscape = true) : string {
  76. if (null !== $quote) {
  77. $str = str_replace('\\' . $quote, $quote, $str);
  78. }
  79. $extra = '';
  80. if ($parseUnicodeEscape) {
  81. $extra = '|u\{([0-9a-fA-F]+)\}';
  82. }
  83. return preg_replace_callback(
  84. '~\\\\([\\\\$nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3}' . $extra . ')~',
  85. function($matches) {
  86. $str = $matches[1];
  87. if (isset(self::$replacements[$str])) {
  88. return self::$replacements[$str];
  89. } elseif ('x' === $str[0] || 'X' === $str[0]) {
  90. return chr(hexdec(substr($str, 1)));
  91. } elseif ('u' === $str[0]) {
  92. return self::codePointToUtf8(hexdec($matches[2]));
  93. } else {
  94. return chr(octdec($str));
  95. }
  96. },
  97. $str
  98. );
  99. }
  100. /**
  101. * Converts a Unicode code point to its UTF-8 encoded representation.
  102. *
  103. * @param int $num Code point
  104. *
  105. * @return string UTF-8 representation of code point
  106. */
  107. private static function codePointToUtf8(int $num) : string {
  108. if ($num <= 0x7F) {
  109. return chr($num);
  110. }
  111. if ($num <= 0x7FF) {
  112. return chr(($num>>6) + 0xC0) . chr(($num&0x3F) + 0x80);
  113. }
  114. if ($num <= 0xFFFF) {
  115. return chr(($num>>12) + 0xE0) . chr((($num>>6)&0x3F) + 0x80) . chr(($num&0x3F) + 0x80);
  116. }
  117. if ($num <= 0x1FFFFF) {
  118. return chr(($num>>18) + 0xF0) . chr((($num>>12)&0x3F) + 0x80)
  119. . chr((($num>>6)&0x3F) + 0x80) . chr(($num&0x3F) + 0x80);
  120. }
  121. throw new Error('Invalid UTF-8 codepoint escape sequence: Codepoint too large');
  122. }
  123. public function getType() : string {
  124. return 'Scalar_String';
  125. }
  126. }