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

https://gitlab.com/kimting254/wbms · PHP · 119 lines · 66 code · 15 blank · 38 comment · 8 complexity · 802ab05026bc5014bd30b27139ce2068 MD5 · raw file

  1. <?php
  2. namespace PhpParser\Node\Scalar;
  3. use PhpParser\Node\Scalar;
  4. class String_ extends Scalar
  5. {
  6. /** @var string String value */
  7. public $value;
  8. protected static $replacements = array(
  9. '\\' => '\\',
  10. '$' => '$',
  11. 'n' => "\n",
  12. 'r' => "\r",
  13. 't' => "\t",
  14. 'f' => "\f",
  15. 'v' => "\v",
  16. 'e' => "\x1B",
  17. );
  18. /**
  19. * Constructs a string scalar node.
  20. *
  21. * @param string $value Value of the string
  22. * @param array $attributes Additional attributes
  23. */
  24. public function __construct($value = '', array $attributes = array()) {
  25. parent::__construct(null, $attributes);
  26. $this->value = $value;
  27. }
  28. public function getSubNodeNames() {
  29. return array('value');
  30. }
  31. /**
  32. * @internal
  33. *
  34. * Parses a string token.
  35. *
  36. * @param string $str String token content
  37. *
  38. * @return string The parsed string
  39. */
  40. public static function parse($str) {
  41. $bLength = 0;
  42. if ('b' === $str[0]) {
  43. $bLength = 1;
  44. }
  45. if ('\'' === $str[$bLength]) {
  46. return str_replace(
  47. array('\\\\', '\\\''),
  48. array( '\\', '\''),
  49. substr($str, $bLength + 1, -1)
  50. );
  51. } else {
  52. return self::parseEscapeSequences(substr($str, $bLength + 1, -1), '"');
  53. }
  54. }
  55. /**
  56. * @internal
  57. *
  58. * Parses escape sequences in strings (all string types apart from single quoted).
  59. *
  60. * @param string $str String without quotes
  61. * @param null|string $quote Quote type
  62. *
  63. * @return string String with escape sequences parsed
  64. */
  65. public static function parseEscapeSequences($str, $quote) {
  66. if (null !== $quote) {
  67. $str = str_replace('\\' . $quote, $quote, $str);
  68. }
  69. return preg_replace_callback(
  70. '~\\\\([\\\\$nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3})~',
  71. array(__CLASS__, 'parseCallback'),
  72. $str
  73. );
  74. }
  75. private static function parseCallback($matches) {
  76. $str = $matches[1];
  77. if (isset(self::$replacements[$str])) {
  78. return self::$replacements[$str];
  79. } elseif ('x' === $str[0] || 'X' === $str[0]) {
  80. return chr(hexdec($str));
  81. } else {
  82. return chr(octdec($str));
  83. }
  84. }
  85. /**
  86. * @internal
  87. *
  88. * Parses a constant doc string.
  89. *
  90. * @param string $startToken Doc string start token content (<<<SMTHG)
  91. * @param string $str String token content
  92. *
  93. * @return string Parsed string
  94. */
  95. public static function parseDocString($startToken, $str) {
  96. // strip last newline (thanks tokenizer for sticking it into the string!)
  97. $str = preg_replace('~(\r\n|\n|\r)\z~', '', $str);
  98. // nowdoc string
  99. if (false !== strpos($startToken, '\'')) {
  100. return $str;
  101. }
  102. return self::parseEscapeSequences($str, null);
  103. }
  104. }