ConditionalNode.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. array('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. }