public function __construct(Twig_Node_Expression_Filter $node) { if (!self::isDefaultFilter($node)) { throw new Twig_Error('The default filter node cannot be created from the given node.', $node->getLine()); } $test = new Twig_Node_Expression_Test(clone $node->getNode('node'), 'defined', new Twig_Node(), $node->getLine()); $default = count($node->getNode('arguments')) ? $node->getNode('arguments')->getNode(0) : new Twig_Node_Expression_Constant('', $node->getLine()); $node = new Twig_Node_Expression_Conditional($test, $node, $default, $node->getLine()); parent::__construct(array('node' => $node), array(), $node->getLine()); }
public function __construct(Twig_NodeInterface $node, Twig_Node_Expression_Constant $filterName, Twig_NodeInterface $arguments, $lineno, $tag = null) { $default = new Twig_Node_Expression_Filter($node, new Twig_Node_Expression_Constant('_default', $node->getLine()), $arguments, $node->getLine()); if ('default' === $filterName->getAttribute('value') && ($node instanceof Twig_Node_Expression_Name || $node instanceof Twig_Node_Expression_GetAttr)) { $test = new Twig_Node_Expression_Test_Defined(clone $node, 'defined', new Twig_Node(), $node->getLine()); $false = count($arguments) ? $arguments->getNode(0) : new Twig_Node_Expression_Constant('', $node->getLine()); $node = new Twig_Node_Expression_Conditional($test, $default, $false, $node->getLine()); } else { $node = $default; } parent::__construct($node, $filterName, $arguments, $lineno, $tag); }
public function parseFilterExpressionRaw($node, $tag = null) { while (true) { $token = $this->parser->getStream()->expect(Twig_Token::NAME_TYPE); $name = new Twig_Node_Expression_Constant($token->getValue(), $token->getLine()); if (!$this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, '(')) { $arguments = new Twig_Node(); } else { $arguments = $this->parseArguments(); } $node = new Twig_Node_Expression_Filter($node, $name, $arguments, $token->getLine(), $tag); // The default filter is intercepted when the filtered value // is a name (like obj) or an attribute (like obj.attr) // In such a case, it's compiled to {{ obj is defined ? obj|default('bar') : 'bar' }} if ('default' === $token->getValue() && ($node->getNode('node') instanceof Twig_Node_Expression_Name || $node->getNode('node') instanceof Twig_Node_Expression_GetAttr)) { $test = new Twig_Node_Expression_Test(clone $node->getNode('node'), 'defined', new Twig_Node(), $node->getLine()); $default = count($node->getNode('arguments')) ? $node->getNode('arguments')->getNode(0) : new Twig_Node_Expression_Constant('', $node->getLine()); $node = new Twig_Node_Expression_Conditional($test, $node, $default, $node->getLine()); } if (!$this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, '|')) { break; } $this->parser->getStream()->next(); } return $node; }