PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/renta/jobeet2.loc
PHP | 55 lines | 27 code | 7 blank | 21 comment | 3 complexity | 7e3d6687ba75592cce7ddca2c113336a MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0, LGPL-2.1, BSD-2-Clause, Apache-2.0, CC-BY-3.0
  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. * @package twig
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. */
  23. class Twig_Node_Expression_Test_Defined extends Twig_Node_Expression_Test
  24. {
  25. public function __construct(Twig_NodeInterface $node, $name, Twig_NodeInterface $arguments = null, $lineno)
  26. {
  27. parent::__construct($node, $name, $arguments, $lineno);
  28. if ($node instanceof Twig_Node_Expression_Name) {
  29. $node->setAttribute('is_defined_test', true);
  30. } elseif ($node instanceof Twig_Node_Expression_GetAttr) {
  31. $node->setAttribute('is_defined_test', true);
  32. $this->changeIgnoreStrictCheck($node);
  33. } else {
  34. throw new Twig_Error_Syntax('The "defined" test only works with simple variables', $this->getLine(), $compiler->getFilename());
  35. }
  36. }
  37. protected function changeIgnoreStrictCheck(Twig_Node_Expression_GetAttr $node)
  38. {
  39. $node->setAttribute('ignore_strict_check', true);
  40. if ($node->getNode('node') instanceof Twig_Node_Expression_GetAttr) {
  41. $this->changeIgnoreStrictCheck($node->getNode('node'));
  42. }
  43. }
  44. public function compile(Twig_Compiler $compiler)
  45. {
  46. $compiler->subcompile($this->getNode('node'));
  47. }
  48. }