This is a PHP 5.3 paginator with a totally different core concept.
Note: it is still experimental, any ideas on structural design are very welcome
How is it different? First of all, it uses Symfony's event dispatcher to paginate whatever is needed. The pagination process involves triggering events which hit the subscribers. If the subscriber knows how to paginate the given object, it does. Finally, some subscriber must initialize the pagination view object, which will be the result of pagination request. Pagination view can be anything which will be responsible for how to render the pagination.
Magic? no! only KISS principle
Why reinvent the wheel? Can someone tell me what's the definition of wheel in the software world?
$paginator = new Knp\Component\Pager\Paginator;
$target = range('a', 'u');
// uses event subscribers to paginate $target
$pagination = $paginator->paginate($target, 2/*page*/, 10/*limit*/);
// iterate paginated items
foreach ($pagination as $item) {
//...
}
echo $pagination; // renders pagination
// overriding view rendering
$pagination->renderer = function($data) use ($template) {
return $twig->render($template, $data);
};
echo $pagination;
// or paginate Doctrine ORM query
$pagination = $paginator->paginate($em->createQuery('SELECT a FROM Entity\Article a'), 1, 10);