Carew\Twig\NodeVisitor\Paginator::enterPaginationFilterNode PHP Method

enterPaginationFilterNode() public method

public enterPaginationFilterNode ( Twig_Node_Expression_Function $node, Twig_Environment $env )
$node Twig_Node_Expression_Function
$env Twig_Environment
    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;
    }