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