Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.1.x] Added new feature for pagination and also optional sets the aliases #10985

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- Method `isSetOption` in `Phalcon\Validation\ValidatorInterface` marked as deprecated, please use `hasOption`
- Added internal check "allowEmpty" before calling a validator. If it option is true and the value of empty, the validator is skipped
- Added default header: `Content-Type: "application/json; charset=UTF-8"` in method `Phalcon\Http\Response::setJsonContent`
- Added `Phalcon\Paginator\RepositoryInterface` for repository the current state of `paginator` and also optional sets the aliases for properties repository [#10985](https://github.com/phalcon/cphalcon/pull/10985)

# [2.0.8](https://github.com/phalcon/cphalcon/releases/tag/phalcon-v2.0.8) (2015-09-19)
- Added `Phalcon\Security\Random::base58` - to generate a random base58 string
Expand Down
51 changes: 50 additions & 1 deletion phalcon/paginator/adapter.zep
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Phalcon\Paginator;
/**
* Phalcon\Paginator\Adapter
*/
abstract class Adapter
abstract class Adapter implements AdapterInterface
{

/**
Expand All @@ -35,6 +35,30 @@ abstract class Adapter
*/
protected _page = null;

/**
* Repository for pagination
* @var RepositoryInterface
*/
private _repository;

/**
* Phalcon\Paginator\Adapter\Model constructor
*/
public function __construct(array! config)
{
if isset config["limit"] {
this->setLimit(config["limit"]);
}

if isset config["page"] {
this->setCurrentPage(config["page"]);
}

if isset config["repository"] {
this->setRepository(config["repository"]);
}
}

/**
* Set the current page number
*/
Expand All @@ -60,4 +84,29 @@ abstract class Adapter
{
return this->_limitRows;
}

/**
* Sets current repository for pagination
*/
public function setRepository(<RepositoryInterface> repository) -> <Adapter>
{
let this->_repository = repository;
return this;
}

/**
* Gets current repository for pagination
*/
protected function getRepository(array properties = null) -> <RepositoryInterface>
{
if !this->_repository {
let this->_repository = new Repository();
}

if properties !== null {
this->_repository->setProperties(properties);
}

return this->_repository;
}
}
42 changes: 16 additions & 26 deletions phalcon/paginator/adapter/model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Phalcon\Paginator\Adapter;

use Phalcon\Paginator\Exception;
use Phalcon\Paginator\Adapter;
use Phalcon\Paginator\AdapterInterface;
use Phalcon\Paginator\RepositoryInterface;

/**
* Phalcon\Paginator\Adapter\Model
Expand All @@ -40,7 +40,7 @@ use Phalcon\Paginator\AdapterInterface;
* $paginate = $paginator->getPaginate();
*</code>
*/
class Model extends Adapter implements AdapterInterface
class Model extends Adapter
{

/**
Expand All @@ -53,25 +53,16 @@ class Model extends Adapter implements AdapterInterface
*/
public function __construct(array! config)
{
var page, limit;

parent::__construct(config);
let this->_config = config;

if fetch limit, config["limit"] {
let this->_limitRows = limit;
}

if fetch page, config["page"] {
let this->_page = page;
}
}

/**
* Returns a slice of the resultset to show in the pagination
*/
public function getPaginate() -> <\stdclass>
public function getPaginate() -> <RepositoryInterface>
{
var config, items, pageItems, page;
var config, items, pageItems;
int pageNumber, show, n, start, lastShowPage,
i, next, totalPages, before;

Expand Down Expand Up @@ -139,17 +130,16 @@ class Model extends Adapter implements AdapterInterface
let before = 1;
}

let page = new \stdClass(),
page->items = pageItems,
page->first = 1,
page->before = before,
page->current = pageNumber,
page->last = totalPages,
page->next = next,
page->total_pages = totalPages,
page->total_items = n,
page->limit = this->_limitRows;

return page;
return this->getRepository([
RepositoryInterface::PROPERTY_ITEMS : pageItems,
RepositoryInterface::PROPERTY_TOTAL_PAGES : totalPages,
RepositoryInterface::PROPERTY_TOTAL_ITEMS : n,
RepositoryInterface::PROPERTY_LIMIT : this->_limitRows,
RepositoryInterface::PROPERTY_FIRST_PAGE : 1,
RepositoryInterface::PROPERTY_PREVIOUS_PAGE : before,
RepositoryInterface::PROPERTY_CURRENT_PAGE : pageNumber,
RepositoryInterface::PROPERTY_NEXT_PAGE : next,
RepositoryInterface::PROPERTY_LAST_PAGE : totalPages
]);
}
}
44 changes: 17 additions & 27 deletions phalcon/paginator/adapter/nativearray.zep
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Phalcon\Paginator\Adapter;

use Phalcon\Paginator\Exception;
use Phalcon\Paginator\Adapter;
use Phalcon\Paginator\AdapterInterface;
use Phalcon\Paginator\RepositoryInterface;

/**
* Phalcon\Paginator\Adapter\NativeArray
Expand All @@ -45,7 +45,7 @@ use Phalcon\Paginator\AdapterInterface;
*</code>
*
*/
class NativeArray extends Adapter implements AdapterInterface
class NativeArray extends Adapter
{

/**
Expand All @@ -56,27 +56,18 @@ class NativeArray extends Adapter implements AdapterInterface
/**
* Phalcon\Paginator\Adapter\NativeArray constructor
*/
public function __construct(array config)
public function __construct(array! config)
{
var page, limit;

parent::__construct(config);
let this->_config = config;

if fetch limit, config["limit"] {
let this->_limitRows = limit;
}

if fetch page, config["page"] {
let this->_page = page;
}
}

/**
* Returns a slice of the resultset to show in the pagination
*/
public function getPaginate() -> <\stdClass>
public function getPaginate() -> <RepositoryInterface>
{
var config, items, page;
var config, items;
int show, pageNumber, totalPages, number, before, next;
double roundedTotal;

Expand Down Expand Up @@ -123,17 +114,16 @@ class NativeArray extends Adapter implements AdapterInterface
let before = 1;
}

let page = new \stdClass(),
page->items = items,
page->first = 1,
page->before = before,
page->current = pageNumber,
page->last = totalPages,
page->next = next,
page->total_pages = totalPages,
page->total_items = number,
page->limit = this->_limitRows;

return page;
return this->getRepository([
RepositoryInterface::PROPERTY_ITEMS : items,
RepositoryInterface::PROPERTY_TOTAL_PAGES : totalPages,
RepositoryInterface::PROPERTY_TOTAL_ITEMS : number,
RepositoryInterface::PROPERTY_LIMIT : this->_limitRows,
RepositoryInterface::PROPERTY_FIRST_PAGE : 1,
RepositoryInterface::PROPERTY_PREVIOUS_PAGE : before,
RepositoryInterface::PROPERTY_CURRENT_PAGE : pageNumber,
RepositoryInterface::PROPERTY_NEXT_PAGE : next,
RepositoryInterface::PROPERTY_LAST_PAGE : totalPages
]);
}
}
45 changes: 19 additions & 26 deletions phalcon/paginator/adapter/querybuilder.zep
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Phalcon\Paginator\Adapter;

use Phalcon\Mvc\Model\Query\Builder;
use Phalcon\Paginator\Adapter;
use Phalcon\Paginator\AdapterInterface;
use Phalcon\Paginator\RepositoryInterface;
use Phalcon\Paginator\Exception;

/**
Expand All @@ -42,7 +42,7 @@ use Phalcon\Paginator\Exception;
* ));
*</code>
*/
class QueryBuilder extends Adapter implements AdapterInterface
class QueryBuilder extends Adapter
{
/**
* Configuration of paginator by model
Expand All @@ -59,24 +59,18 @@ class QueryBuilder extends Adapter implements AdapterInterface
*/
public function __construct(array config)
{
var builder, limit, page;

parent::__construct(config);
let this->_config = config;

if !fetch builder, config["builder"] {
if !isset config["builder"] {
throw new Exception("Parameter 'builder' is required");
}

if !fetch limit, config["limit"] {
if !isset config["limit"] {
throw new Exception("Parameter 'limit' is required");
}

this->setQueryBuilder(builder);
this->setLimit(limit);

if fetch page, config["page"] {
this->setCurrentPage(page);
}
this->setQueryBuilder(config["builder"]);
}

/**
Expand Down Expand Up @@ -108,10 +102,10 @@ class QueryBuilder extends Adapter implements AdapterInterface
/**
* Returns a slice of the resultset to show in the pagination
*/
public function getPaginate() -> <\stdClass>
public function getPaginate() -> <RepositoryInterface>
{
var originalBuilder, builder, totalBuilder, totalPages,
limit, numberPage, number, query, page, before, items, totalQuery,
limit, numberPage, number, query, before, items, totalQuery,
result, row, rowcount, next;

let originalBuilder = this->_builder;
Expand Down Expand Up @@ -186,18 +180,17 @@ class QueryBuilder extends Adapter implements AdapterInterface
let next = totalPages;
}

let page = new \stdClass(),
page->items = items,
page->first = 1,
page->before = before,
page->current = numberPage,
page->last = totalPages,
page->next = next,
page->total_pages = totalPages,
page->total_items = rowcount,
page->limit = this->_limitRows;

return page;
return this->getRepository([
RepositoryInterface::PROPERTY_ITEMS : items,
RepositoryInterface::PROPERTY_TOTAL_PAGES : totalPages,
RepositoryInterface::PROPERTY_TOTAL_ITEMS : rowcount,
RepositoryInterface::PROPERTY_LIMIT : this->_limitRows,
RepositoryInterface::PROPERTY_FIRST_PAGE : 1,
RepositoryInterface::PROPERTY_PREVIOUS_PAGE : before,
RepositoryInterface::PROPERTY_CURRENT_PAGE : numberPage,
RepositoryInterface::PROPERTY_NEXT_PAGE : next,
RepositoryInterface::PROPERTY_LAST_PAGE : totalPages
]);
}

}
2 changes: 1 addition & 1 deletion phalcon/paginator/adapterinterface.zep
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ interface AdapterInterface
/**
* Returns a slice of the resultset to show in the pagination
*/
public function getPaginate() -> <\stdClass>;
public function getPaginate() -> <PaginateInterface>;

/**
* Set current rows limit
Expand Down
Loading