Skip to content

Commit

Permalink
Add ApiPaginationComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
bcrowe committed Jul 26, 2015
1 parent 06fc079 commit 072adc5
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions src/Controller/Component/ApiPaginationComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
namespace BryanCrowe\ApiPagination\Controller\Component;

use Cake\Controller\Component;
use Cake\Controller\Controller;
use Cake\Event\Event;

/**
* This is a simple component that injects pagination info into responses when
* using CakePHP's PaginatorComponent alongside of CakePHP's JsonView or XmlView
* classes.
*/
class ApiPaginationComponent extends Component
{
/**
* Default config.
*
* @var array
*/
protected $_defaultConfig = [
'key' => 'pagination',
'aliases' => [],
'visible' => []
];

/**
* Holds the paging information.
*
* @var array
*/
protected $paging = [];

/**
* Injects the pagination info into the response if the current request is a
* JSON or XML request with pagination.
*
* @param Event $event The Controller.beforeRender event.
* @return void
*/
public function beforeRender(Event $event)
{
$controller = $event->subject();

if (!$this->isPaginatedApiRequest($controller)) {
return;
}

$this->paging = $controller->request->params['paging'][$controller->name];

if (!empty($this->config('aliases'))) {
$this->setAliases();
}

if (!empty($this->config('visible'))) {
$this->setVisible();
}

$controller->set($this->config('key'), $this->paging);
$controller->viewVars['_serialize'][] = $this->config('key');
}

/**
* Aliases the default pagination keys to the new keys that the user defines
* in the config.
*
* @return void
*/
protected function setAliases()
{
$aliases = $this->config('aliases');
foreach ($aliases as $key => $value) {
$this->paging[$value] = $this->paging[$key];
unset($this->paging[$key]);
}
}

/**
* Removes any pagination keys that haven't been defined as visible in the
* config.
*
* @return void
*/
protected function setVisible()
{
$visible = $this->config('visible');
foreach ($this->paging as $key => $value) {
if (!in_array($key, $visible)) {
unset($this->paging[$key]);
}
}
}

/**
* Checks whether the current request is a JSON or XML request with
* pagination.
*
* @param \Cake\Controller\Controller $controller A reference to the
* instantiating controller object
* @return bool True if JSON or XML with paging, otherwise false.
*/
protected function isPaginatedApiRequest(Controller $controller)
{
if (
$controller->request->is('json') ||
$controller->request->is('xml') &&
isset($controller->request->params['paging'])
) {
return true;
}

return false;
}
}

0 comments on commit 072adc5

Please sign in to comment.