1 | <?php |
||
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 $pagingInfo = []; |
||
32 | |||
33 | /** |
||
34 | * {@inheritDoc} |
||
35 | * |
||
36 | * @return array |
||
37 | */ |
||
38 | 3 | public function implementedEvents() |
|
42 | |||
43 | /** |
||
44 | * Injects the pagination info into the response if the current request is a |
||
45 | * JSON or XML request with pagination. |
||
46 | * |
||
47 | * @param Event $event The Controller.beforeRender event. |
||
48 | * @return void |
||
49 | */ |
||
50 | 18 | public function beforeRender(Event $event) |
|
51 | { |
||
52 | 18 | $subject = $event->subject(); |
|
53 | |||
54 | 18 | if (!$this->isPaginatedApiRequest()) { |
|
55 | 3 | return; |
|
56 | } |
||
57 | |||
58 | 15 | $this->pagingInfo = $this->request->params['paging'][$subject->name]; |
|
59 | 15 | $config = $this->config(); |
|
60 | |||
61 | 15 | if (!empty($config['aliases'])) { |
|
62 | 6 | $this->setAliases(); |
|
63 | 6 | } |
|
64 | |||
65 | 15 | if (!empty($config['visible'])) { |
|
66 | 6 | $this->setVisibility(); |
|
67 | 6 | } |
|
68 | |||
69 | 15 | $subject->set($config['key'], $this->pagingInfo); |
|
70 | 15 | $subject->viewVars['_serialize'][] = $config['key']; |
|
71 | 15 | } |
|
72 | |||
73 | /** |
||
74 | * Aliases the default pagination keys to the new keys that the user defines |
||
75 | * in the config. |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | 6 | protected function setAliases() |
|
86 | |||
87 | /** |
||
88 | * Removes any pagination keys that haven't been defined as visible in the |
||
89 | * config. |
||
90 | * |
||
91 | * @return void |
||
92 | */ |
||
93 | 6 | protected function setVisibility() |
|
102 | |||
103 | /** |
||
104 | * Checks whether the current request is a JSON or XML request with |
||
105 | * pagination. |
||
106 | * |
||
107 | * @return bool True if JSON or XML with paging, otherwise false. |
||
108 | */ |
||
109 | 18 | protected function isPaginatedApiRequest() |
|
119 | } |
||
120 |