Home

Awesome

Phalcon Collection Paginator

A Phalcon paginator adapter for Phalcon Collections Build Status Codacy Badge Code Climate License

Why create this?

Being familiar with the various Pagination data adapters available to the Phalcon\Mvc\Model, I expected to see similar adapters for Phalcon\Mvc\Collection. This project is to provide a pagination data adapter for ODM in Phalcon.

Installation

  1. Use composer
  2. Run
    composer require angelxmoreno/phalcon-collection-paginator
    
  3. Make sure to include Composer's autoload.php file as part of your bootstrapping process
    require 'vendor/autoload.php';
    

Configuration

PropertyDescriptionOptionalDefault
limitThe number of items to fetch at a timeyes30
pageThe current page set to fetchyes1
collectionThe collection class' FQN to use for fetchingnonone
find_queryAn array to use when building the query for the page set. Must be a find() compatible array.yes[]

Example

<?php

use App\Models\Cars;
use Phalcon\Paginator\Adapter\Collection as CollectionPaginator;

// In a controller you could have something like so:

// An array of defaults settings
$default_pagination_settings = [
   'limit' => CollectionPaginator::DEFAULT_LIMIT,
   'page' => 1,
   'collection' => Cars::class,
   'find_query' => [
       'year' => 1980
   ]
];

// Merge the default array with the query string params
$pagination_settings = array_merge($default_pagination_settings, $this->request->getQuery());

// Create a Collection Paginator instance with the pagination settings
$paginator = CollectionPaginator($pagination_settings);

// Get the paginated results \stdClass and set it to the view
$this->view->results = $paginator->getPaginate();

CollectionPaginator->getPaginate() returns an \stdClass.

Running tests

docker-comose up
./bin/run_tests.sh
docker-comose down

Contributing

  1. Fork the repo
  2. Branch from master
  3. Open a PR

License

(The MIT license)

Copyright (c) 2017 Angel S. Moreno

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.