1 <?php declare(strict_types=1);
3 namespace PhpParser\Node\Scalar;
6 use PhpParser\Node\Scalar;
8 class String_ extends Scalar
10 /* For use in "kind" attribute */
11 const KIND_SINGLE_QUOTED = 1;
12 const KIND_DOUBLE_QUOTED = 2;
13 const KIND_HEREDOC = 3;
14 const KIND_NOWDOC = 4;
16 /** @var string String value */
19 protected static $replacements = [
31 * Constructs a string scalar node.
33 * @param string $value Value of the string
34 * @param array $attributes Additional attributes
36 public function __construct(string $value, array $attributes = []) {
37 parent::__construct($attributes);
38 $this->value = $value;
41 public function getSubNodeNames() : array {
48 * Parses a string token.
50 * @param string $str String token content
51 * @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
53 * @return string The parsed string
55 public static function parse(string $str, bool $parseUnicodeEscape = true) : string {
57 if ('b' === $str[0] || 'B' === $str[0]) {
61 if ('\'' === $str[$bLength]) {
65 substr($str, $bLength + 1, -1)
68 return self::parseEscapeSequences(
69 substr($str, $bLength + 1, -1), '"', $parseUnicodeEscape
77 * Parses escape sequences in strings (all string types apart from single quoted).
79 * @param string $str String without quotes
80 * @param null|string $quote Quote type
81 * @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
83 * @return string String with escape sequences parsed
85 public static function parseEscapeSequences(string $str, $quote, bool $parseUnicodeEscape = true) : string {
86 if (null !== $quote) {
87 $str = str_replace('\\' . $quote, $quote, $str);
91 if ($parseUnicodeEscape) {
92 $extra = '|u\{([0-9a-fA-F]+)\}';
95 return preg_replace_callback(
96 '~\\\\([\\\\$nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3}' . $extra . ')~',
100 if (isset(self::$replacements[$str])) {
101 return self::$replacements[$str];
102 } elseif ('x' === $str[0] || 'X' === $str[0]) {
103 return chr(hexdec($str));
104 } elseif ('u' === $str[0]) {
105 return self::codePointToUtf8(hexdec($matches[2]));
107 return chr(octdec($str));
115 * Converts a Unicode code point to its UTF-8 encoded representation.
117 * @param int $num Code point
119 * @return string UTF-8 representation of code point
121 private static function codePointToUtf8(int $num) : string {
126 return chr(($num>>6) + 0xC0) . chr(($num&0x3F) + 0x80);
128 if ($num <= 0xFFFF) {
129 return chr(($num>>12) + 0xE0) . chr((($num>>6)&0x3F) + 0x80) . chr(($num&0x3F) + 0x80);
131 if ($num <= 0x1FFFFF) {
132 return chr(($num>>18) + 0xF0) . chr((($num>>12)&0x3F) + 0x80)
133 . chr((($num>>6)&0x3F) + 0x80) . chr(($num&0x3F) + 0x80);
135 throw new Error('Invalid UTF-8 codepoint escape sequence: Codepoint too large');
138 public function getType() : string {
139 return 'Scalar_String';