PHP code example of everlutionsk / pagination-bundle

1. Go to this page and download the library: Download everlutionsk/pagination-bundle 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/ */

    

everlutionsk / pagination-bundle example snippets



// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new Everlution\PaginationBundle\EverlutionPaginationBundle(),
        );

        // ...
    }

    // ...
}

    public function listAction()
    {
        return $this->render(
            'AppBundle:User:list.html.twig',
            ['userPage' => $this->get('uc.client.list')->getPage()]
        );
    }



declare(strict_types=1);

namespace Library\UseCase\User;

use Everlution\PaginationBundle\Pagination\MaxResultsExceeded;
use Everlution\PaginationBundle\Pagination\Page;
use Everlution\PaginationBundle\Pagination\Pagination;
use Everlution\PaginationBundle\Pagination\QueryToPagination;
use Library\UseCase\User\Persistence\UserViewListQuery;
use Symfony\Component\HttpFoundation\RequestStack;

class ListUsers
{
    const DEFAULT_PAGINATION_OFFSET = 0;

    /** @var UserViewListQuery */
    private $listQuery;
    /** @var RequestStack */
    private $requestStack;
    /** @var Pagination */
    private $pagination;

    public function __construct(
        UserViewListQuery $listQuery,
        QueryToPagination $pagination,
        RequestStack $requestStack
    ) {
        $this->requestStack = $requestStack;
        $this->listQuery = $listQuery;
        $this->pagination = $pagination;
    }

    public function getPage(): Page
    {
        $request = $this->requestStack->getCurrentRequest();

        try {
            $query = $this->listQuery->getUserViewListQuery();
            $this->pagination->setQueryBuilder($query);

            // limit and offsete here are provided automagically by RequestTransformer from {page} parameter of URL
            return $this->pagination->paginate(
                (int) $request->get('limit', Page::DEFAULT_PAGE_SIZE),
                (int) $request->get('offset', self::DEFAULT_PAGINATION_OFFSET)
            );
        } catch (MaxResultsExceeded $exception) {
            throw new WrongPaginationArgument($exception->getMessage());
        }
    }
}

    public function getUserViewListQuery(): QueryBuilder
    {
        return $this
            ->createQueryBuilder('user')
            ->select('PARTIAL user.{id, firstName, lastName, jobTitle}');
    }



namespace Library\UseCase\User\Sort;

use Doctrine\ORM\QueryBuilder;
use Everlution\PaginationBundle\Pagination\Sort\Column\DoctrineSortColumn;
use Everlution\PaginationBundle\Pagination\Sort\Rule\SortRule;
use Everlution\PaginationBundle\Sort\RequestSortQuery;

class Name implements SortRule
{
    public function accept(QueryBuilder &$builder, RequestSortQuery $query): void
    {
        # getName() is automagically generated getter based on name provided in sortable_header() TWIG function
        if (!$query->getName() instanceof DoctrineSortColumn) {
            return;
        }

        $builder
            ->addOrderBy('user.firstName', $query->getName()->getDirection())
            ->addOrderBy('user.lastName', $query->getName()->getDirection());
    }
}



namespace Library\UseCase\User\Sort;

use Doctrine\ORM\QueryBuilder;
use Everlution\PaginationBundle\Pagination\Sort\Column\DoctrineSortColumn;
use Everlution\PaginationBundle\Pagination\Sort\Rule\SortRule;
use Everlution\PaginationBundle\Sort\RequestSortQuery;

class JobTitle implements SortRule
{
    public function accept(QueryBuilder &$builder, RequestSortQuery $query): void
    {
        # sortable_header() TWIG function accepts underscore naming for query string which are automatically converted to camelCase for getters
        if (!$query->getJobTitle() instanceof DoctrineSortColumn) {
            return;
        }

        $builder->addOrderBy('user.jobTitle', $query->getJobTitle()->getDirection());
    }
}