/libs/Twig/Node/Expression/Test/Constant.php

https://github.com/davidajnered/simple-post-preview · PHP · 46 lines · 23 code · 4 blank · 19 comment · 1 complexity · cea24b966c64017afa46413e189956b7 MD5 · raw file

  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. }