2017 © Pedro Peláez
 

library paginator

Ruwork Paginator

image

ruvents/paginator

Ruwork Paginator

  • Wednesday, December 20, 2017
  • by RUVENTS
  • Repository
  • 3 Watchers
  • 0 Stars
  • 1,138 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 3 % Grown

The README.md

Ruwork Paginator

Description

This library provides a convenient way to paginate any data structure., (*1)

Some definitions: - A section is a set of pages, rendered together. - Proximity is a number of pages, displayed before and after the current page., (*2)

For example, for 10 pages with proximity 2 and current page 5 paginator will have 3 sections with the following page numbers: [1], [3, 4, 5, 6, 7], [10]., (*3)

When the current page is close to one of the edges sections get merged., (*4)

Controller code example

<?php

use Ruwork\Paginator\PaginatorBuilder;
use Ruwork\Paginator\Provider\IterableProvider;

$data = range(1, 100);

$paginator = PaginatorBuilder::create()
    // required
    ->setProvider(new IterableProvider($data))
    // defaults to 1
    // when out of range of estimated pages, PageOutOfRangeException is thrown
    ->setCurrent(2)
    // defaults to 2
    ->setProximity(1)
    // defaults to 10
    ->setPerPage(3)
    ->getPaginator();

// template logic

foreach ($paginator->getItems() as $item) {
    // render the item row
}

if (null !== $previous = $paginator->getPrevious()) {
    echo sprintf('<a href="?page=%d">Previous</a>', $previous->getNumber());
}

foreach ($paginator as $section) {
    foreach ($section as $page) {
        echo sprintf('<a href="?page=%1$d" class="%2$s">%1$d</a>', $page->getNumber(), $page->isCurrent() ? 'active' : '');
    }
}

if (null !== $next = $paginator->getNext()) {
    echo sprintf('<a href="?page=%d">Next</a>', $next->getNumber());
}

Data providers

IterableProvider

Can be used with an array or an object implementing \Traversable., (*5)

DoctrineOrmProvider

Can be used to paginate over Doctrine entities. Internally uses the native Doctrine\ORM\Tools\Pagination\Paginator helper., (*6)

<?php

use Ruwork\Paginator\PaginatorBuilder;
use Ruwork\Paginator\Provider\DoctrineOrmProvider;
use Doctrine\ORM\EntityRepository;

/** @var EntityRepository $repository */

$qb = $repository->createQueryBuilder('entity')
    ->andWhere('entity.id = :id')
    ->setParameters([
        'id' => 1
    ]);

$paginator = PaginatorBuilder::create()
    ->setProvider(new DoctrineOrmProvider($qb))
    ->getPaginator();

Custom

Create your own provider by implementing the Ruwork\Paginator\Provider\ProviderInterface., (*7)

Templates

bootstrap_4.html.twig

{% set route = app.request.attributes.get('_route') %}
{% set route_params = app.request.attributes.get('_route_params', []) %}

{% embed 'twig/bootstrap_4.html.twig' with {paginator: paginator, show_previous_next: false} %}
    {% block href path(route, route_params|merge({page: page.first ? null : page.number})) %}

    {% block previous_label 'Предыдущая' %}

    {% block next_label 'Следующая' %}
{% endembed %}

The Versions

20/12 2017

dev-master

9999999-dev

Ruwork Paginator

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Valentin Udaltsov

12/11 2017

0.1.2

0.1.2.0

RUVENTS Paginator

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Valentin Udaltsov

11/11 2017

0.1.1

0.1.1.0

RUVENTS Paginator

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Valentin Udaltsov

11/11 2017

0.1.0

0.1.0.0

RUVENTS Paginator

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Valentin Udaltsov