Constant.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2011 Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Checks if a variable is the exact same value as a constant.
  12. *
  13. * <pre>
  14. * {% if post.status is constant('Post::PUBLISHED') %}
  15. * the status attribute is exactly the same as Post::PUBLISHED
  16. * {% endif %}
  17. * </pre>
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. class Twig_Node_Expression_Test_Constant extends Twig_Node_Expression_Test
  22. {
  23. public function compile(Twig_Compiler $compiler)
  24. {
  25. $compiler
  26. ->raw('(')
  27. ->subcompile($this->getNode('node'))
  28. ->raw(' === constant(')
  29. ;
  30. if ($this->getNode('arguments')->hasNode(1)) {
  31. $compiler
  32. ->raw('get_class(')
  33. ->subcompile($this->getNode('arguments')->getNode(1))
  34. ->raw(')."::".')
  35. ;
  36. }
  37. $compiler
  38. ->subcompile($this->getNode('arguments')->getNode(0))
  39. ->raw('))')
  40. ;
  41. }
  42. }