PHP code example of zenstruck / porpaginas

1. Go to this page and download the library: Download zenstruck/porpaginas library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zenstruck / porpaginas example snippets


namespace Zenstruck\Porpaginas;

interface Result extends \Countable, \IteratorAggregate, \JsonSerializable
{
    public function take(int $offset, int $limit): Page;

    /**
     * Return the number of all results in the paginatable.
     */
    public function count(): int;

    /**
     * Return an iterator over all results of the paginatable.
     */
    public function getIterator(): \Iterator;
}

namespace Zenstruck\Porpaginas;

interface Page extends \Countable, \IteratorAggregate, \JsonSerializable
{
    /**
     * Return the number of results on the currrent page.
     */
    public function count(): int;

    /**
     * Return the number of ALL results in the paginatable..
     */
    public function totalCount(): int;

    /**
     * Return an iterator over selected windows of results of the paginatable.
     */
    public function getIterator(): \Iterator;
}

use Zenstruck\Porpaginas\Result;

class UserRepository extends EntityRepository
{
    public function findAllUsers(): Result
    {
        $qb = $this->createQueryBuilder('u')->orderBy('u.username');

        return new ORMQueryResult($qb);
    }
}

class UserController
{
    /**
     * @var UserRepository
     */
    private $userRepository;

    public function listAction(Request $request)
    {
        $result = $this->userRepository->findAllUsers();
        // no filtering by page, iterate full result

        return array('users' => $result);
    }

    public function porpaginasListAction(Request $request)
    {
        $result = $this->userRepository->findAllUsers();

        $paginator = $result->take(($request->get('page', 1)-1) * 20, 20);

        return array('users' => $paginator);
    }
}

use Zenstruck\Porpaginas\Pager;

class UserController
{
    /**
     * @var UserRepository
     */
    private $userRepository;

    public function porpaginasListAction(Request $request)
    {
        $result = $this->userRepository->findAllUsers();

        return array('users' => $result->paginate($request->get('page', 1)));
    }
}