php-parser/lib/PhpParser/Node/Scalar/String_.php

161 lines
4.7 KiB
PHP
Raw Normal View History

<?php
namespace PhpParser\Node\Scalar;
use PhpParser\Error;
use PhpParser\Node\Scalar;
class String_ extends Scalar
{
/* For use in "kind" attribute */
const KIND_SINGLE_QUOTED = 1;
const KIND_DOUBLE_QUOTED = 2;
const KIND_HEREDOC = 3;
const KIND_NOWDOC = 4;
/** @var string String value */
public $value;
protected static $replacements = [
'\\' => '\\',
'$' => '$',
'n' => "\n",
'r' => "\r",
't' => "\t",
'f' => "\f",
'v' => "\v",
'e' => "\x1B",
];
/**
* Constructs a string scalar node.
*
* @param string $value Value of the string
* @param array $attributes Additional attributes
*/
public function __construct(string $value, array $attributes = []) {
2015-05-02 22:17:34 +02:00
parent::__construct($attributes);
$this->value = $value;
}
2017-04-28 21:40:59 +02:00
public function getSubNodeNames() : array {
return ['value'];
}
/**
2014-09-30 20:23:25 +02:00
* @internal
*
* Parses a string token.
*
* @param string $str String token content
* @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
2011-06-01 22:37:10 +02:00
*
* @return string The parsed string
*/
2017-04-28 21:40:59 +02:00
public static function parse(string $str, bool $parseUnicodeEscape = true) : string {
$bLength = 0;
2016-04-02 14:15:49 +02:00
if ('b' === $str[0] || 'B' === $str[0]) {
$bLength = 1;
}
if ('\'' === $str[$bLength]) {
return str_replace(
['\\\\', '\\\''],
[ '\\', '\''],
substr($str, $bLength + 1, -1)
);
} else {
return self::parseEscapeSequences(
substr($str, $bLength + 1, -1), '"', $parseUnicodeEscape
);
}
}
/**
2014-09-30 20:23:25 +02:00
* @internal
*
* Parses escape sequences in strings (all string types apart from single quoted).
*
* @param string $str String without quotes
* @param null|string $quote Quote type
* @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
2011-06-01 22:37:10 +02:00
*
* @return string String with escape sequences parsed
*/
2017-04-28 21:40:59 +02:00
public static function parseEscapeSequences(string $str, $quote, bool $parseUnicodeEscape = true) : string {
if (null !== $quote) {
$str = str_replace('\\' . $quote, $quote, $str);
}
$extra = '';
if ($parseUnicodeEscape) {
$extra = '|u\{([0-9a-fA-F]+)\}';
}
return preg_replace_callback(
'~\\\\([\\\\$nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3}' . $extra . ')~',
2015-05-02 22:35:15 +02:00
function($matches) {
$str = $matches[1];
if (isset(self::$replacements[$str])) {
return self::$replacements[$str];
} elseif ('x' === $str[0] || 'X' === $str[0]) {
return chr(hexdec($str));
} elseif ('u' === $str[0]) {
return self::codePointToUtf8(hexdec($matches[2]));
2015-05-02 22:35:15 +02:00
} else {
return chr(octdec($str));
}
},
$str
);
}
2017-01-24 08:38:55 +01:00
/**
* Converts a Unicode code point to its UTF-8 encoded representation.
*
* @param int $num Code point
*
* @return string UTF-8 representation of code point
2017-01-24 08:38:55 +01:00
*/
2017-04-28 21:40:59 +02:00
private static function codePointToUtf8(int $num) : string {
if ($num <= 0x7F) {
return chr($num);
}
if ($num <= 0x7FF) {
return chr(($num>>6) + 0xC0) . chr(($num&0x3F) + 0x80);
}
if ($num <= 0xFFFF) {
return chr(($num>>12) + 0xE0) . chr((($num>>6)&0x3F) + 0x80) . chr(($num&0x3F) + 0x80);
}
if ($num <= 0x1FFFFF) {
return chr(($num>>18) + 0xF0) . chr((($num>>12)&0x3F) + 0x80)
. chr((($num>>6)&0x3F) + 0x80) . chr(($num&0x3F) + 0x80);
}
throw new Error('Invalid UTF-8 codepoint escape sequence: Codepoint too large');
}
/**
2014-09-30 20:23:25 +02:00
* @internal
*
* Parses a constant doc string.
*
* @param string $startToken Doc string start token content (<<<SMTHG)
* @param string $str String token content
* @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
*
* @return string Parsed string
*/
2017-04-28 21:40:59 +02:00
public static function parseDocString(string $startToken, string $str, bool $parseUnicodeEscape = true) : string {
// strip last newline (thanks tokenizer for sticking it into the string!)
2015-09-19 16:05:23 +02:00
$str = preg_replace('~(\r\n|\n|\r)\z~', '', $str);
// nowdoc string
if (false !== strpos($startToken, '\'')) {
return $str;
}
return self::parseEscapeSequences($str, null, $parseUnicodeEscape);
}
}