2017 © Pedro Peláez
 

library porpaginas

Library that generically solves several pagination issues with DAO/repository abstractions.

image

beberlei/porpaginas

Library that generically solves several pagination issues with DAO/repository abstractions.

  • Friday, November 17, 2017
  • by beberlei
  • Repository
  • 12 Watchers
  • 143 Stars
  • 64,070 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 10 Forks
  • 3 Open issues
  • 4 Versions
  • 18 % Grown

The README.md

Porpaginas

Build Status, (*1)

This library solves a bunch of issues that comes up with APIs of Repository classes alot:, (*2)

  • You need different methods for paginatable and non-paginatable finder methods.
  • You need to expose the underlying data-source and return query objects from your repository.
  • Serialization of paginators should be easily possible for REST APIs

Both Pagerfanta and KnpLabs Pager don't solve this issue and their APIs are really problematic in this regard. You need to return the query objects or adapters for paginators from your repositories to get it working and then use an API on the controller side to turn them into a paginated result set., (*3)

Porpaginas solves this by introducing a sane abstraction for paginated results. For rendering purposes you can integrate with either Pagerfanta or KnpLabs Pager, this library is not about reimplementating the view part of pagination., (*4)

Central part of this library is the interface Result:, (*5)

<?php
namespace Porpaginas;

interface Result extends Countable, IteratorAggregate
{
    /**
     * @param int $offset
     * @param int $limit
     * @return Page
     */
    public function take($offset, $limit);

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

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

This API offers you two ways to iterate over the paginatable result, either the full result or a paginated window of the result using take(). One drawback is that the query is always lazily executed inside the Result and not directly in the repository., (*6)

The Page interface returned from Result#take() looks like this:, (*7)

<?php

namespace Porpaginas;

interface Page extends Countable, IteratorAggregate
{
    /**
     * Return the number of results on the current page.

     * @return int
     */
    public function count();

    /**
     * Return the number of ALL results in the paginatable.

     * @return int
     */
    public function totalCount();

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

You can use the IteratorPage in your own implementations, if the underlying data-source is itself an iterator., (*8)

Supported Backends

  • Array
  • Doctrine ORM

Example

Take the following example using Doctrine ORM:, (*9)

<?php
class UserRepository extends EntityRepository
{
    /**
     * @return \Porpaginas\Result
     */
    public function findAllUsers()
    {
        $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) * 20, 20);

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

Now in the template for porpaginasListAction using the porpaginas Twig extension for example:, (*10)

We found a total of <strong>{{ porpaginas_total(users) }}</strong> users:



    {% for user in users %}
  • {{ user.name }}
  • {% endfor %}
{{ porpaginas_render(users) }}

Pager Library Support

  • For Pagerfanta use the Porpaginas\Pagerfanta\PorpaginasAdapter and pass it a result as first argument.

Embedded Pager

You can use the Porpaginas\Pager class to help you get a slice of previous and next pages to display:, (*11)

``` php $pager = Porpaginas\Pager::fromPage($page);, (*12)


Passed to a twig template: ``` jinja <nav class="pages"> <ul> {% for page in pager.pages() %} <li class="{{ pager.isCurrent(page) ? 'active' }}"> <a href="{{ page }}">{{ page }}</a> </li> {% endfor %} </ul> </nav>

The Versions

17/11 2017

dev-master

9999999-dev

Library that generically solves several pagination issues with DAO/repository abstractions.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Benjamin Eberlei

page paging pagination paginator

31/08 2017

v1.2

1.2.0.0

Library that generically solves several pagination issues with DAO/repository abstractions.

  Sources   Download

MIT

The Development Requires

by Benjamin Eberlei

page paging pagination paginator

16/07 2017

v1.1

1.1.0.0

Library that generically solves several pagination issues with DAO/repository abstractions.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Benjamin Eberlei

page paging pagination paginator

17/09 2015

v1.0

1.0.0.0

Library that generically solves several pagination issues with DAO/repository abstractions.

  Sources   Download

MIT

The Development Requires

by Benjamin Eberlei

page paging pagination paginator