anh/paginator

Paginator library

Installs: 206

Dependents: 2

Stars: 2

Watchers: 2

Language: PHP

v0.2.2 2015-04-27 10:44 UTC

README

Build Status Scrutinizer Code Quality SensioLabsInsight

Installation

$ php composer.phar require 'anh/paginator:0.2.*'

Usage

Create paginator and call it's paginate() method with data, page number and number of elements per page. It will find appropriate adapter and return Page instance, filled with paginated data, it can be used in foreach directly. Also you can pass manually created adapter instead of data.

<?php

use Anh\Paginator\Paginator;

$query = /* ORM Query or QueryBuilder for fetching users */;

$paginator = new Paginator();
$users = $paginator->paginate($query, 1, 10);

foreach ($users as $user) {
    // do something with user
}

If you need to pass options to adapter, pass it to paginate() as fourth parameter. Note each adapter has own options.

<?php

use Anh\Paginator\Paginator;
use Anh\Paginator\Adapter\ArrayAdapter;

$data = array(/* elements */);

$paginator = new Paginator();
$elements = $paginator->paginate($data, 3, 20, array('preserveKeys' => true));

foreach ($elements as $key => $element) {
    // do something with element
}

You can create custom adapters and add them to AdapterResolver. All adapters must implement AdapterInterface interface.

<?php

use Anh\Paginator\Paginator;
use Anh\Paginator\AdapterResolver;

$adapterResolver = new AdapterResolver();
$adapterResolver->addAdapter('Some\Custom\Adapter1');
$adapterResolver->addAdapter('Some\Custom\Adapter2');

$paginator = new Paginator($adapterResolver);
$elements = $paginator->paginate($data, $pageNumber, $elementsPerPage);

foreach ($elements as $element) {
    // do something with element
}

Rendering

<?php

use Anh\Paginator\Paginator;
use Anh\Paginator\View\AdaptiveView;

$paginator = new Paginator();
$posts = $paginator->paginate(/* ... */);

$view = new AdaptiveView();
$view->setTemplates(array(/* templates */));
$view->setOptions(array(/* options */));

echo $view->render($posts, '/some/url/page-%page%', array(/* options */));

For complete list of templates and options see sources. Also you can use resource/pagination.css for paginator styling. It is based on pagination from foundation zurb.

Available adapters

  • EmptyDataAdapter
  • ArrayAdapter
  • DoctrineOrmAdapter

Versioning

Library uses semantic versioning.

License

MIT