|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TypeLang\PHPDoc\Tag; |
| 6 | + |
| 7 | +use TypeLang\Parser\Node\Stmt\TypeStatement; |
| 8 | +use TypeLang\Parser\ParserInterface as TypesParserInterface; |
| 9 | +use TypeLang\PHPDoc\Exception\InvalidTagException; |
| 10 | +use TypeLang\PHPDoc\Parser\Description\DescriptionParserInterface; |
| 11 | +use TypeLang\PHPDoc\Tag\Content\OptionalVariableNameApplicator; |
| 12 | +use TypeLang\PHPDoc\Tag\Content\TypeParserApplicator; |
| 13 | +use TypeLang\PHPDoc\Tag\Content\VariableNameApplicator; |
| 14 | + |
| 15 | +class Content implements \Stringable |
| 16 | +{ |
| 17 | + private readonly string $original; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var int<0, max> |
| 21 | + * @psalm-readonly-allow-private-mutation |
| 22 | + */ |
| 23 | + public int $offset = 0; |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + public string $value, |
| 27 | + ) { |
| 28 | + $this->original = $this->value; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @param int<0, max> $offset |
| 33 | + */ |
| 34 | + public function shift(int $offset, bool $ltrim = true): void |
| 35 | + { |
| 36 | + if ($offset <= 0) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + $size = \strlen($this->value); |
| 41 | + $this->value = \substr($this->value, $offset); |
| 42 | + |
| 43 | + if ($ltrim) { |
| 44 | + $this->value = \ltrim($this->value); |
| 45 | + } |
| 46 | + |
| 47 | + /** @psalm-suppress InvalidPropertyAssignmentValue */ |
| 48 | + $this->offset += $size - \strlen($this->value); |
| 49 | + } |
| 50 | + |
| 51 | + public function getTagException(string $message, \Throwable $previous = null): InvalidTagException |
| 52 | + { |
| 53 | + return new InvalidTagException( |
| 54 | + source: $this->original, |
| 55 | + offset: $this->offset, |
| 56 | + message: $message, |
| 57 | + previous: $previous, |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @api |
| 63 | + * @param non-empty-string $tag |
| 64 | + */ |
| 65 | + public function nextType(string $tag, TypesParserInterface $parser): TypeStatement |
| 66 | + { |
| 67 | + return $this->apply(new TypeParserApplicator($tag, $parser)); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @api |
| 72 | + * @param non-empty-string $tag |
| 73 | + * @return non-empty-string |
| 74 | + */ |
| 75 | + public function nextVariable(string $tag): string |
| 76 | + { |
| 77 | + return $this->apply(new VariableNameApplicator($tag)); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @api |
| 82 | + * @return non-empty-string|null |
| 83 | + */ |
| 84 | + public function nextOptionalVariable(): ?string |
| 85 | + { |
| 86 | + return $this->apply(new OptionalVariableNameApplicator()); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @template T of mixed |
| 91 | + * @param callable(Content):T $applicator |
| 92 | + * @return T |
| 93 | + */ |
| 94 | + public function apply(callable $applicator): mixed |
| 95 | + { |
| 96 | + return $applicator($this); |
| 97 | + } |
| 98 | + |
| 99 | + public function toDescription(DescriptionParserInterface $descriptions): DescriptionInterface |
| 100 | + { |
| 101 | + return $descriptions->parse($this->value); |
| 102 | + } |
| 103 | + |
| 104 | + public function __toString(): string |
| 105 | + { |
| 106 | + return $this->value; |
| 107 | + } |
| 108 | +} |
0 commit comments