PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/twig/twig/lib/Twig/Node/Expression/Test/Defined.php

https://bitbucket.org/gruenwaldt/loquitur-web
PHP | 54 lines | 27 code | 7 blank | 20 comment | 3 complexity | 28345503b665a4d6edbbd7b06428718a MD5 | raw file
Possible License(s): BSD-3-Clause
  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 defined in the current context.
  12. *
  13. * <pre>
  14. * {# defined works with variable names and variable attributes #}
  15. * {% if foo is defined %}
  16. * {# ... #}
  17. * {% endif %}
  18. * </pre>
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. class Twig_Node_Expression_Test_Defined extends Twig_Node_Expression_Test
  23. {
  24. public function __construct(Twig_NodeInterface $node, $name, Twig_NodeInterface $arguments = null, $lineno)
  25. {
  26. parent::__construct($node, $name, $arguments, $lineno);
  27. if ($node instanceof Twig_Node_Expression_Name) {
  28. $node->setAttribute('is_defined_test', true);
  29. } elseif ($node instanceof Twig_Node_Expression_GetAttr) {
  30. $node->setAttribute('is_defined_test', true);
  31. $this->changeIgnoreStrictCheck($node);
  32. } else {
  33. throw new Twig_Error_Syntax('The "defined" test only works with simple variables', $this->getLine());
  34. }
  35. }
  36. protected function changeIgnoreStrictCheck(Twig_Node_Expression_GetAttr $node)
  37. {
  38. $node->setAttribute('ignore_strict_check', true);
  39. if ($node->getNode('node') instanceof Twig_Node_Expression_GetAttr) {
  40. $this->changeIgnoreStrictCheck($node->getNode('node'));
  41. }
  42. }
  43. public function compile(Twig_Compiler $compiler)
  44. {
  45. $compiler->subcompile($this->getNode('node'));
  46. }
  47. }