ConditionalNode.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\ExpressionLanguage\Node;
  11. use Symfony\Component\ExpressionLanguage\Compiler;
  12. /**
  13. * @author Fabien Potencier <fabien@symfony.com>
  14. *
  15. * @internal
  16. */
  17. class ConditionalNode extends Node
  18. {
  19. public function __construct(Node $expr1, Node $expr2, Node $expr3)
  20. {
  21. parent::__construct(
  22. ['expr1' => $expr1, 'expr2' => $expr2, 'expr3' => $expr3]
  23. );
  24. }
  25. public function compile(Compiler $compiler)
  26. {
  27. $compiler
  28. ->raw('((')
  29. ->compile($this->nodes['expr1'])
  30. ->raw(') ? (')
  31. ->compile($this->nodes['expr2'])
  32. ->raw(') : (')
  33. ->compile($this->nodes['expr3'])
  34. ->raw('))')
  35. ;
  36. }
  37. public function evaluate($functions, $values)
  38. {
  39. if ($this->nodes['expr1']->evaluate($functions, $values)) {
  40. return $this->nodes['expr2']->evaluate($functions, $values);
  41. }
  42. return $this->nodes['expr3']->evaluate($functions, $values);
  43. }
  44. public function toArray()
  45. {
  46. return ['(', $this->nodes['expr1'], ' ? ', $this->nodes['expr2'], ' : ', $this->nodes['expr3'], ')'];
  47. }
  48. }