Skip to content

Commit 072adc5

Browse files
committed
Add ApiPaginationComponent
1 parent 06fc079 commit 072adc5

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
namespace BryanCrowe\ApiPagination\Controller\Component;
3+
4+
use Cake\Controller\Component;
5+
use Cake\Controller\Controller;
6+
use Cake\Event\Event;
7+
8+
/**
9+
* This is a simple component that injects pagination info into responses when
10+
* using CakePHP's PaginatorComponent alongside of CakePHP's JsonView or XmlView
11+
* classes.
12+
*/
13+
class ApiPaginationComponent extends Component
14+
{
15+
/**
16+
* Default config.
17+
*
18+
* @var array
19+
*/
20+
protected $_defaultConfig = [
21+
'key' => 'pagination',
22+
'aliases' => [],
23+
'visible' => []
24+
];
25+
26+
/**
27+
* Holds the paging information.
28+
*
29+
* @var array
30+
*/
31+
protected $paging = [];
32+
33+
/**
34+
* Injects the pagination info into the response if the current request is a
35+
* JSON or XML request with pagination.
36+
*
37+
* @param Event $event The Controller.beforeRender event.
38+
* @return void
39+
*/
40+
public function beforeRender(Event $event)
41+
{
42+
$controller = $event->subject();
43+
44+
if (!$this->isPaginatedApiRequest($controller)) {
45+
return;
46+
}
47+
48+
$this->paging = $controller->request->params['paging'][$controller->name];
49+
50+
if (!empty($this->config('aliases'))) {
51+
$this->setAliases();
52+
}
53+
54+
if (!empty($this->config('visible'))) {
55+
$this->setVisible();
56+
}
57+
58+
$controller->set($this->config('key'), $this->paging);
59+
$controller->viewVars['_serialize'][] = $this->config('key');
60+
}
61+
62+
/**
63+
* Aliases the default pagination keys to the new keys that the user defines
64+
* in the config.
65+
*
66+
* @return void
67+
*/
68+
protected function setAliases()
69+
{
70+
$aliases = $this->config('aliases');
71+
foreach ($aliases as $key => $value) {
72+
$this->paging[$value] = $this->paging[$key];
73+
unset($this->paging[$key]);
74+
}
75+
}
76+
77+
/**
78+
* Removes any pagination keys that haven't been defined as visible in the
79+
* config.
80+
*
81+
* @return void
82+
*/
83+
protected function setVisible()
84+
{
85+
$visible = $this->config('visible');
86+
foreach ($this->paging as $key => $value) {
87+
if (!in_array($key, $visible)) {
88+
unset($this->paging[$key]);
89+
}
90+
}
91+
}
92+
93+
/**
94+
* Checks whether the current request is a JSON or XML request with
95+
* pagination.
96+
*
97+
* @param \Cake\Controller\Controller $controller A reference to the
98+
* instantiating controller object
99+
* @return bool True if JSON or XML with paging, otherwise false.
100+
*/
101+
protected function isPaginatedApiRequest(Controller $controller)
102+
{
103+
if (
104+
$controller->request->is('json') ||
105+
$controller->request->is('xml') &&
106+
isset($controller->request->params['paging'])
107+
) {
108+
return true;
109+
}
110+
111+
return false;
112+
}
113+
}

0 commit comments

Comments
 (0)