vendor/twig/twig/src/Node/Expression/ConditionalExpression.php line 22

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  6. * (c) Armin Ronacher
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Twig\Node\Expression;
  12. use Twig\Compiler;
  13. use Twig\Node\Expression\Ternary\ConditionalTernary;
  14. class ConditionalExpression extends AbstractExpression implements OperatorEscapeInterface
  15. {
  16. public function __construct(AbstractExpression $expr1, AbstractExpression $expr2, AbstractExpression $expr3, int $lineno)
  17. {
  18. trigger_deprecation('twig/twig', '3.17', \sprintf('"%s" is deprecated; use "%s" instead.', __CLASS__, ConditionalTernary::class));
  19. parent::__construct(['expr1' => $expr1, 'expr2' => $expr2, 'expr3' => $expr3], [], $lineno);
  20. }
  21. public function compile(Compiler $compiler): void
  22. {
  23. // Ternary with no then uses Elvis operator
  24. if ($this->getNode('expr1') === $this->getNode('expr2')) {
  25. $compiler
  26. ->raw('((')
  27. ->subcompile($this->getNode('expr1'))
  28. ->raw(') ?: (')
  29. ->subcompile($this->getNode('expr3'))
  30. ->raw('))');
  31. } else {
  32. $compiler
  33. ->raw('((')
  34. ->subcompile($this->getNode('expr1'))
  35. ->raw(') ? (')
  36. ->subcompile($this->getNode('expr2'))
  37. ->raw(') : (')
  38. ->subcompile($this->getNode('expr3'))
  39. ->raw('))');
  40. }
  41. }
  42. public function getOperandNamesToEscape(): array
  43. {
  44. return ['expr2', 'expr3'];
  45. }
  46. }