public function testConstructor() { $expr = new Twig_Node_Expression_Constant('foo', 1); $name = new Twig_Node_Expression_Constant('upper', 1); $args = new Twig_Node(); $node = new Twig_Node_Expression_Filter($expr, $name, $args, 1); $this->assertEquals($expr, $node->getNode('node')); $this->assertEquals($name, $node->getNode('filter')); $this->assertEquals($args, $node->getNode('arguments')); }
private function preEscapeFilterNode(Twig_Node_Expression_Filter $filter, Twig_Environment $env) { $name = $filter->getNode('filter')->getAttribute('value'); $type = $env->getFilter($name)->getPreEscape(); if (null === $type) { return $filter; } $node = $filter->getNode('node'); if ($this->isSafeFor($type, $node, $env)) { return $filter; } $filter->setNode('node', $this->getEscaperFilter($type, $node)); return $filter; }
/** * TODO: faire des check sur les value possible * * @param \Twig_Node_Expression_Filter $node * @return null */ protected function getTypeFromNode(\Twig_Node_Expression_Filter $node) { $type = null; $argumentsNode = $node->getNode('arguments'); if ($argumentsNode instanceof \Twig_Node) { $argumentsArray = $this->getNode($argumentsNode, '0'); if ($argumentsArray instanceof \Twig_Node_Expression_Array) { $argumentsKey = $this->getNode($argumentsArray, '0'); $argumentsValue = $this->getNode($argumentsArray, '1'); if ($argumentsKey instanceof \Twig_Node_Expression_Constant && $argumentsValue instanceof \Twig_Node_Expression_Constant) { $type = 'type' === $argumentsKey->getAttribute('value') ? $argumentsValue->getAttribute('value') : null; } } } return $type; }
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 compile(JsCompiler $compiler, \Twig_Node_Expression_Filter $node) { if (!($locale = $compiler->getDefine('locale')) || !$this->translator instanceof Translator) { return false; } // unfortunately, the Translation component does not provide a better // way to retrieve these $this->loadCatalogueRef = new \ReflectionMethod($this->translator, 'loadCatalogue'); $this->loadCatalogueRef->setAccessible(true); $this->catalogueRef = new \ReflectionProperty($this->translator, 'catalogues'); $this->catalogueRef->setAccessible(true); // ignore dynamic messages, we cannot resolve these // users can still apply a runtime trans filter to do this $subNode = $node->getNode('node'); if (!$subNode instanceof \Twig_Node_Expression_Constant) { return false; } $id = $subNode->getAttribute('value'); $domain = 'messages'; $hasParams = false; $arguments = $node->getNode('arguments'); if (count($arguments) > 0) { $hasParams = count($arguments->getNode(0)) > 0; if ($arguments->hasNode(1)) { $domainNode = $arguments->getNode(1); if (!$domainNode instanceof \Twig_Node_Expression_Constant) { return false; } $domain = $domainNode->getAttribute('value'); } } $catalogue = $this->getCatalogue($locale); if (!$hasParams) { $compiler->string($catalogue->get($id, $domain)); return; } $compiler->raw('twig.filter.replace(')->string($catalogue->get($id, $domain))->raw(", ")->subcompile($arguments->getNode(0))->raw(')'); }
/** * @covers Twig_Node_Expression_Filter::compile * @dataProvider getTests */ public function testCompile($node, $source, $environment = null) { parent::testCompile($node, $source, $environment); $expr = new Twig_Node_Expression_Constant('foo', 0); $filters = new Twig_Node(array(new Twig_Node_Expression_Constant('foobar', 0), new Twig_Node(array(new Twig_Node_Expression_Constant('bar', 0), new Twig_Node_Expression_Constant('foobar', 0)))), array(), 0); $node = new Twig_Node_Expression_Filter($expr, $filters, 0); $tests[] = array($node, '$this->resolveMissingFilter("foobar", array("foo", "bar", "foobar"))'); try { $node->compile($this->getCompiler()); $this->fail(); } catch (Exception $e) { $this->assertEquals('Twig_SyntaxError', get_class($e)); } }
private function assertNodeFilterHasSlice(\Twig_Node_Expression_Filter $nodeFilter, $maxPerPage, $currentNumberOfPagination = 0) { $this->assertInstanceOf('Twig_Node_Expression_Constant', $nodeFilter->getNode('filter')); $this->assertSame('slice', $nodeFilter->getNode('filter')->getAttribute('value')); $arguments = $nodeFilter->getNode('arguments'); $this->assertInstanceOf('Twig_Node_Expression_Name', $arguments->getNode(0)); $this->assertSame("__offset_{$currentNumberOfPagination}__", $arguments->getNode(0)->getAttribute('name')); $this->assertInstanceOf('Twig_Node_Expression_Constant', $arguments->getNode(1)); $this->assertSame($maxPerPage, $arguments->getNode(1)->getAttribute('value')); }
/** * Parses arguments of a Filter node and pushes entry into list of extracted strings. * * @param Twig_Node_Expression_Filter $node * @param mixed Variable number of arguments representing roles of the Twig filter arguments. */ protected function pushFilter(Twig_Node_Expression_Filter $node) { $args = func_get_args(); array_shift($args); $valueNodes = array(self::MSGID => $node->getNode('node')); foreach ($node->getNode('arguments') as $i => $argumentNode) { if (!isset($args[$i])) { break; } $valueNodes[$args[$i]] = $argumentNode; } $this->pushEntry($node, $valueNodes); }
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; }