• Stars
    star
    159
  • Rank 235,916 (Top 5 %)
  • Language
    PHP
  • Created almost 11 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple abstraction for paginated and non-paginated results

Porpaginas

Build Status

This library solves a bunch of issues that comes up with APIs of Repository classes alot:

  • 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.

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.

Central part of this library is the interface Result:

<?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.

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

<?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.

Supported Backends

  • Array
  • Doctrine ORM

Example

Take the following example using Doctrine ORM:

<?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:

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

<ul>
{% for user in users %}
    <li>{{ user.name }}</li>
{% endfor %}
</ul>

{{ 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:

$pager = Porpaginas\Pager::fromPage($page);

Passed to a twig template:

<nav class="pages">
    <ul>
        {% for page in pager.pages() %}
            <li class="{{ pager.isCurrent(page) ? 'active' }}">
                <a href="{{ page }}">{{ page }}</a>
            </li>
        {% endfor %}
    </ul>
</nav>

More Repositories

1

assert

Thin assertion library for use in libraries and business-model
PHP
2,407
star
2

DoctrineExtensions

A set of Doctrine 2 extensions
PHP
1,956
star
3

litecqrs-php

Small convention based CQRS library for PHP
PHP
553
star
4

metrics

Simple library that abstracts different metrics collectors. I find this necessary to have a consistent and simple metrics (functional) API that doesn't cause vendor lock-in.
PHP
316
star
5

composer-monorepo-plugin

Integrates Composer into monolithic repositories with many packages.
PHP
305
star
6

AcmePizzaBundle

Acme Form Experimental Bundle
PHP
131
star
7

php-rfc-watch

Interactive voting results for PHP RFC process.
HTML
127
star
8

zf-doctrine

A Zend Framework 1.x and Doctrine 1.2 Integration - UNMAINTAINED
PHP
102
star
9

netbeans-php-enhancements

Netbeans PHP Enhancements, such as PHP Code Sniffer Support
Java
102
star
10

fastcgi-serve

Small webserver to use in front of fast-cgi servers (php-fpm, hhvm, ...)
Go
92
star
11

symfony-minimal-distribution

This contains the code from my blog post http://whitewashing.de/2014/10/26/symfony_all_the_things_web.html
PHP
67
star
12

bankaccount

Sample application used for PHPUnit training.
PHP
46
star
13

vim-php-refactor

Some simple vim refactoring functions for your PHP code
Vim Script
40
star
14

xdebug-trace-gui

Fork of Trace GUI by Thomas Hambach
PHP
35
star
15

license-manager

License Switch Project - Helping open source projects to switch licenses
CSS
34
star
16

Doctrine-Workflow

A Doctrine 2 persistence layer for ezcWorkflow
PHP
33
star
17

DoctrineCodeGenerator

Prototype of an AST based Event Driven CodeGenerator
PHP
31
star
18

http-client-middleware

Missing interfaces for HTTP-Client middlewares using PSR-7 messages.
PHP
28
star
19

Doctrine-ActiveEntity

ActiveRecord ORM (Mod) on top of Doctrine2
PHP
25
star
20

env

PHP PECL extension for loading 12factor env variables from a file
C
24
star
21

Whitewashing

Symfony2 Bundle that powers the www.whitewashing.de blog
PHP
23
star
22

azure-blob-storage

Small platform-independent library to access Microsoft Windows Azure Blob Storage with a Service or a StreamWrapper.
PHP
22
star
23

WhitewashingZFMvcCompatBundle

PHP
21
star
24

zelten

A social network website based on tent.io protocol
JavaScript
17
star
25

TentPHP

A Tent.io client written in PHP
PHP
16
star
26

githubpr_to_jira

Converts Github PR to Jira Issues
PHP
16
star
27

pearanha

Pearanha is deprecated, use Composer intead
PHP
15
star
28

context

DEPRECATED
PHP
15
star
29

WhitewashingLogglyBundle

loggly.com logger for Monolog integrated into Symfony2 - not really maintained anymore
PHP
15
star
30

php-compiletime-poc

Extension that counts the time spent in PHP file compilation during the execution of a script
C
13
star
31

php-ast-tracer-poc

C
13
star
32

doctrine-example-app

Example App Setup for Training/Workshops
PHP
13
star
33

dbdeploy-php

DBDeploy PHP clone
PHP
12
star
34

ZendNavigationBundle

A Zend Navigation Bundle for Symfony2
PHP
11
star
35

phpricot

A forgiving HTML Parsing library written in PHP
PHP
11
star
36

hdrhistogram-php

A PHP extension wrapper for the C hdrhistogram API.
C
10
star
37

symfony-azure-edition

Symfony2 on Windows Azure Edition
PHP
10
star
38

ReviewSquawkBundle

Symfony2 app that provides a static-code-analysis as a service platform
JavaScript
9
star
39

ZetaBundle

A Zeta Components Bundle for Symfony2
PHP
8
star
40

websocket-proxy-example

Example Go microservice that acts as a Websocket proxy for other server applications
Go
6
star
41

stdlib

Some fun
PHP
6
star
42

compilefile-ext

C
5
star
43

php8-benchmark-doctrine

PHP
5
star
44

Zend_Db-Adapter-for-ext-mysql

Legacy applications often work with ext/mysql all over the place. This Zend_Db adapter allows to share the resources and benefit from Zend_Db.
PHP
5
star
45

ZetaWorkflowCouchDB

Zeta Components CouchDB Backend for Workflow
PHP
4
star
46

php-overload-poc

C
4
star
47

php-couch-content-repository

PHP
3
star
48

flow3-doctrine2

prototype hack for doctrine2 as persistence manager for flow3
PHP
3
star
49

funcall

Fork of the pecl extension "funcall"
C
3
star
50

redmine_gherkinviewer

Redmine Plugin: Display feature files from project repository directly in a "Features" tab in the project.
Ruby
3
star
51

AzureTaskDemoBundle

Demo bundle for Azure Functionality with a Symfony+Doctrine application
JavaScript
2
star
52

beberlei

1
star
53

deprecations

PHP
1
star
54

pecl_http

PHP
1
star
55

cj-push-server

PubSubHubBub Server in Clojure
Clojure
1
star
56

interrupt-sampler-poc

C
1
star
57

hdrhistogram-php-stubs

Adding support for hdrhistogram in IDEs and Scrutinizer
PHP
1
star