1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\PhpDocParser\Ast\ConstExpr;
4:
5: use PHPStan\PhpDocParser\Ast\NodeAttributes;
6: use function sprintf;
7: use function str_replace;
8: use function strlen;
9: use function substr;
10:
11: class DoctrineConstExprStringNode extends ConstExprStringNode
12: {
13:
14: use NodeAttributes;
15:
16: /** @var string */
17: public $value;
18:
19: public function __construct(string $value)
20: {
21: parent::__construct($value);
22: $this->value = $value;
23: }
24:
25: public function __toString(): string
26: {
27: return self::escape($this->value);
28: }
29:
30: public static function unescape(string $value): string
31: {
32: // from https://github.com/doctrine/annotations/blob/a9ec7af212302a75d1f92fa65d3abfbd16245a2a/lib/Doctrine/Common/Annotations/DocLexer.php#L103-L107
33: return str_replace('""', '"', substr($value, 1, strlen($value) - 2));
34: }
35:
36: private static function escape(string $value): string
37: {
38: // from https://github.com/phpstan/phpdoc-parser/issues/205#issuecomment-1662323656
39: return sprintf('"%s"', str_replace('"', '""', $value));
40: }
41:
42: }
43: