public function enterPaginationFilterNode(\Twig_Node_Expression_Function $node, \Twig_Environment $env) { $args = $node->getNode('arguments'); if (!$args->hasNode(0)) { throw new \Twig_Error_Syntax('Missing first argument of "paginate" function.'); } // extract $maxPerPage; if ($args->hasNode(1)) { $arg = $args->getNode(1); if (!$arg instanceof \Twig_Node_Expression_Constant) { throw new \Twig_Error_Syntax('Second argument (optional) of "paginate" function should be an integer.'); } $maxPerPage = (int) $arg->getAttribute('value'); } else { $maxPerPage = $this->maxPerPage; } $this->alterRenderDocumentsWithPagination(); $nodeToPaginate = $args->getNode(0); // Set-up the PaginationNode $extra = $this->currentModule->getNode('blocks'); if (!$extra->hasNode('pagination')) { $extra->setNode('pagination', new PaginationNode()); } $extra->getNode('pagination')->addNodeToPaginate($nodeToPaginate, $maxPerPage); // Filter the node with "|slice(offset, maxPerPage)" $slicedNode = new \Twig_Node_Expression_Filter($nodeToPaginate, new \Twig_Node_Expression_Constant('slice', 1), new \Twig_Node(array(new \Twig_Node_Expression_Name(sprintf('__offset_%s__', $this->currentNumberOfPagination), 1), new \Twig_Node_Expression_Constant($maxPerPage, 1))), 1); return $slicedNode; }
/** * @covers Twig_Node_Expression_Function::__construct */ public function testConstructor() { $name = 'function'; $args = new Twig_Node(); $node = new Twig_Node_Expression_Function($name, $args, 0); $this->assertEquals($name, $node->getAttribute('name')); $this->assertEquals($args, $node->getNode('arguments')); }
/** * @param \Twig_Node_Expression_Function $node * @return mixed * @throws \Twig_Error */ private function resolve(\Twig_Node_Expression_Function $node) { $args = $node->getNode('arguments'); if ($args instanceof \Twig_Node && 1 === $args->count()) { $constNode = $args->getNode(0); if ($constNode instanceof \Twig_Node_Expression_Constant && null !== ($value = $constNode->getAttribute('value'))) { if (!defined($value)) { throw new \Twig_Error(sprintf("Can't resolve constant('%s')", $value)); } if ($this->evaluate) { return new \Twig_Node_Expression_Constant(constant($value), $node->getLine()); } else { return new StaticConstantExpression($value, $node->getLine()); } } } return $node; }
/** * Parses arguments of a Function node and pushes entry into list of extracted strings. * * @param Twig_Node_Expression_Function $node * @param mixed Variable number of arguments representing roles of the Twig function arguments. */ protected function pushFunction(Twig_Node_Expression_Function $node) { $args = func_get_args(); array_shift($args); $valueNodes = array(); foreach ($node->getNode('arguments') as $i => $argumentNode) { if (!isset($args[$i])) { break; } $valueNodes[$args[$i]] = $argumentNode; } $this->pushEntry($node, $valueNodes); }