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\Infix; 13 14use Twig\ExpressionParser\AbstractExpressionParser; 15use Twig\ExpressionParser\ExpressionParserDescriptionInterface; 16use Twig\ExpressionParser\InfixAssociativity; 17use Twig\ExpressionParser\InfixExpressionParserInterface; 18use Twig\ExpressionParser\PrecedenceChange; 19use Twig\Node\Expression\AbstractExpression; 20use Twig\Node\Expression\Binary\AbstractBinary; 21use Twig\Parser; 22use Twig\Token; 23 24/** 25 * @internal 26 */ 27class BinaryOperatorExpressionParser extends AbstractExpressionParser implements InfixExpressionParserInterface, ExpressionParserDescriptionInterface 28{ 29 public function __construct( 30 /** @var class-string<AbstractBinary> */ 31 private string $nodeClass, 32 private string $name, 33 private int $precedence, 34 private InfixAssociativity $associativity = InfixAssociativity::Left, 35 private ?PrecedenceChange $precedenceChange = null, 36 private ?string $description = null, 37 private array $aliases = [], 38 ) { 39 } 40 41 /** 42 * @return AbstractBinary 43 */ 44 public function parse(Parser $parser, AbstractExpression $left, Token $token): AbstractExpression 45 { 46 $right = $parser->parseExpression(InfixAssociativity::Left === $this->getAssociativity() ? $this->getPrecedence() + 1 : $this->getPrecedence()); 47 48 return new ($this->nodeClass)($left, $right, $token->getLine()); 49 } 50 51 public function getAssociativity(): InfixAssociativity 52 { 53 return $this->associativity; 54 } 55 56 public function getName(): string 57 { 58 return $this->name; 59 } 60 61 public function getDescription(): string 62 { 63 return $this->description ?? ''; 64 } 65 66 public function getPrecedence(): int 67 { 68 return $this->precedence; 69 } 70 71 public function getPrecedenceChange(): ?PrecedenceChange 72 { 73 return $this->precedenceChange; 74 } 75 76 public function getAliases(): array 77 { 78 return $this->aliases; 79 } 80} 81