1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) Fabien Potencier
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Twig\ExpressionParser\Prefix;
13
14use Twig\ExpressionParser\AbstractExpressionParser;
15use Twig\ExpressionParser\ExpressionParserDescriptionInterface;
16use Twig\ExpressionParser\PrecedenceChange;
17use Twig\ExpressionParser\PrefixExpressionParserInterface;
18use Twig\Node\Expression\AbstractExpression;
19use Twig\Node\Expression\Unary\AbstractUnary;
20use Twig\Parser;
21use Twig\Token;
22
23/**
24 * @internal
25 */
26final class UnaryOperatorExpressionParser extends AbstractExpressionParser implements PrefixExpressionParserInterface, ExpressionParserDescriptionInterface
27{
28    public function __construct(
29        /** @var class-string<AbstractUnary> */
30        private string $nodeClass,
31        private string $name,
32        private int $precedence,
33        private ?PrecedenceChange $precedenceChange = null,
34        private ?string $description = null,
35        private array $aliases = [],
36        private ?int $operandPrecedence = null,
37    ) {
38    }
39
40    /**
41     * @return AbstractUnary
42     */
43    public function parse(Parser $parser, Token $token): AbstractExpression
44    {
45        return new ($this->nodeClass)($parser->parseExpression($this->operandPrecedence ?? $this->precedence), $token->getLine());
46    }
47
48    public function getName(): string
49    {
50        return $this->name;
51    }
52
53    public function getDescription(): string
54    {
55        return $this->description ?? '';
56    }
57
58    public function getPrecedence(): int
59    {
60        return $this->precedence;
61    }
62
63    public function getPrecedenceChange(): ?PrecedenceChange
64    {
65        return $this->precedenceChange;
66    }
67
68    public function getAliases(): array
69    {
70        return $this->aliases;
71    }
72}
73