-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathApiPaginationComponent.php
More file actions
141 lines (122 loc) · 4.02 KB
/
ApiPaginationComponent.php
File metadata and controls
141 lines (122 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
declare(strict_types=1);
namespace BryanCrowe\ApiPagination\Controller\Component;
use Cake\Controller\Component;
use Cake\Datasource\Paging\PaginatedInterface;
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 array $_defaultConfig = [
'key' => 'pagination',
'aliases' => [],
'visible' => [],
];
/**
* Paging params of paginated result set (if any).
*
* @var array
*/
protected array $pagingParams = [];
/**
* Holds the paging information array from the request.
*
* @var array
*/
protected array $pagingInfo = [];
/**
* Injects the pagination info into the response if the current request is a
* JSON or XML request with pagination.
*
* @param \Cake\Event\Event $event The Controller.beforeRender event.
* @return void
*/
public function beforeRender(Event $event): void
{
if (!$this->isPaginatedApiRequest()) {
return;
}
$subject = $event->getSubject();
$modelName = ucfirst($this->getConfig('model', $subject->getName()));
if (isset($this->pagingParams[$modelName])) {
$this->pagingInfo = $this->pagingParams[$modelName];
}
$config = $this->getConfig();
if (!empty($config['aliases'])) {
$this->setAliases();
}
if (!empty($config['visible'])) {
$this->setVisibility();
}
$subject->set($config['key'], $this->pagingInfo);
$data = $subject->viewBuilder()->getOption('serialize') ?? [];
if (is_array($data)) {
$data[] = $config['key'];
$subject->viewBuilder()->setOption('serialize', $data);
}
}
/**
* Aliases the default pagination keys to the new keys that the user defines
* in the config.
*
* @return void
*/
protected function setAliases(): void
{
foreach ($this->getConfig('aliases') as $key => $value) {
$this->pagingInfo[$value] = $this->pagingInfo[$key];
unset($this->pagingInfo[$key]);
}
}
/**
* Removes any pagination keys that haven't been defined as visible in the
* config.
*
* @return void
*/
protected function setVisibility(): void
{
$visible = $this->getConfig('visible');
foreach ($this->pagingInfo as $key => $value) {
if (!in_array($key, $visible)) {
unset($this->pagingInfo[$key]);
}
}
}
/**
* Checks whether the current request is a JSON or XML request with
* pagination.
*
* @return bool True if JSON or XML with paging, otherwise false.
*/
protected function isPaginatedApiRequest(): bool
{
if (!$this->getController()->getRequest()->is(['json', 'xml'])) {
return false;
}
// Cake 4 way for the people who want to keep embracing paging attribute pattern
if ($this->getController()->getRequest()->getAttribute('paging')) {
$this->pagingParams = $this->getController()->getRequest()->getAttribute('paging');
return !empty($this->pagingParams);
}
// Since cake 5, paging params are no longer part of the request attribute.
// Hence, we check for all the view vars and if paginated interface found then we pick the first one and use it.
// @see https://github.com/cakephp/cakephp/pull/16317#issuecomment-1045873277
foreach ($this->getController()->viewBuilder()->getVars() as $value) {
if ($value instanceof PaginatedInterface) {
$this->pagingParams[$value->pagingParam('alias')] = $value->pagingParams();
break;
}
}
return !empty($this->pagingParams);
}
}