-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerSideRequest.php
More file actions
322 lines (286 loc) · 8.57 KB
/
Copy pathServerSideRequest.php
File metadata and controls
322 lines (286 loc) · 8.57 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
/**
* Italix DataSets - ServerSideRequest
*
* @package Italix\DataSets
* @license MPL-2.0
*/
declare(strict_types=1);
namespace Italix\DataSets;
use Italix\Contracts\DataContainer;
/**
* Parses server-side datatable request parameters.
*
* Extracts pagination, sorting, searching, and filtering info from
* the HTTP request (typically $_GET or $_POST).
*
* Example:
*
* // From superglobals
* $request = ServerSideRequest::from_globals();
*
* // From a custom array
* $request = new ServerSideRequest($params);
*
* // Use with your query builder
* $query->order_by($request->sort_column(), $request->sort_direction())
* ->limit($request->per_page())
* ->offset($request->offset());
*/
class ServerSideRequest
{
/** @var array */
private $params;
/**
* @param array $params The request parameters
*/
public function __construct(array $params)
{
$this->params = $params;
}
/**
* Create from PHP superglobals ($_GET for GET, $_POST for POST).
*
* @return self
*/
public static function from_globals(): self
{
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
$params = strtoupper($method) === 'POST' ? $_POST : $_GET;
return new self($params);
}
/**
* Create from a custom array (useful for testing or framework integration).
*
* @param array $params
* @return self
*/
public static function from_array(array $params): self
{
return new self($params);
}
/**
* Create from a DataContainer (e.g. from RequestInput::get()).
*
* @param DataContainer $data
* @return self
*/
public static function from(DataContainer $data): self
{
return new self($data->to_array());
}
// =========================================================================
// Pagination
// =========================================================================
/**
* Get the requested page number (1-based).
*
* @return int
*/
public function page(): int
{
$page = (int)($this->params['page'] ?? 1);
return max(1, $page);
}
/**
* Get the number of rows per page.
*
* @param int $default
* @return int
*/
public function per_page(int $default = 25): int
{
$size = (int)($this->params['per_page'] ?? $this->params['size'] ?? $default);
return max(1, $size);
}
/**
* Get the calculated offset for SQL OFFSET.
*
* @param int $default_per_page
* @return int
*/
public function offset(int $default_per_page = 25): int
{
return ($this->page() - 1) * $this->per_page($default_per_page);
}
// =========================================================================
// Sorting
// =========================================================================
/**
* Get the primary sort column name.
*
* For multi-column sorting, use sorts() instead.
*
* @param string|null $default
* @return string|null
*/
public function sort_column(?string $default = null): ?string
{
// Multi-sort format: sort[0][field]=name&sort[0][dir]=asc
$sorts = $this->sorts();
if (!empty($sorts)) {
return $sorts[0]['column'];
}
return $this->params['sort'] ?? $this->params['sort_column'] ?? $default;
}
/**
* Get the primary sort direction.
*
* For multi-column sorting, use sorts() instead.
*
* @param string $default 'asc' or 'desc'
* @return string
*/
public function sort_direction(string $default = 'asc'): string
{
$sorts = $this->sorts();
if (!empty($sorts)) {
return $sorts[0]['direction'];
}
$dir = self::direction_code($this->params['sort_dir'] ?? $this->params['sort_direction'] ?? $default, $default);
return $dir;
}
/**
* Normalise anything at all into `asc` or `desc`.
*
* The input is whatever was in the query string, and a query string can
* hold an array: `?sort_dir[]=asc` is a URL anybody can type, and it
* reached `strtolower()` as an array and threw a TypeError — a 500 on
* demand. Non-scalars are refused here rather than cast, because casting an
* array to string yields the literal word "Array", which is not a
* direction either but fails much later and much less clearly.
*
* @param mixed $value
*/
private static function direction_code($value, string $default): string
{
if (!is_scalar($value)) {
return $default;
}
$dir = strtolower((string) $value);
return in_array($dir, ['asc', 'desc'], true) ? $dir : $default;
}
/**
* Get all sort columns (for multi-column sorting).
*
* Returns an array of ['column' => string, 'direction' => string] pairs.
* The JS bootstrap sends these as sort[0][field]=name&sort[0][dir]=asc.
*
* @return array<array{column: string, direction: string}>
*/
public function sorts(): array
{
$sorts_param = $this->params['sorts'] ?? null;
if (!is_array($sorts_param) || empty($sorts_param)) {
return [];
}
$result = [];
foreach ($sorts_param as $sort) {
if (!is_array($sort) || !isset($sort['field'])) {
continue;
}
// A field sent as `sort[0][field][]=x` arrives as an array, and
// casting one to string is a warning plus the word "Array".
if (!is_scalar($sort['field'])) {
continue;
}
$result[] = [
'column' => (string) $sort['field'],
'direction' => self::direction_code($sort['dir'] ?? 'asc', 'asc'),
];
}
return $result;
}
// =========================================================================
// Searching
// =========================================================================
/**
* Get the global search query.
*
* @return string|null
*/
public function search(): ?string
{
$q = $this->params['search'] ?? $this->params['q'] ?? null;
if ($q === null || $q === '') {
return null;
}
return (string)$q;
}
/**
* Get the columns to apply the global search to.
*
* The JS bootstrap sends these as search_columns[]=name&search_columns[]=email.
* If not provided, the backend should search all searchable columns.
*
* @return string[]
*/
public function search_columns(): array
{
$columns = $this->params['search_columns'] ?? [];
if (!is_array($columns)) {
return [];
}
return array_values(array_filter($columns, 'is_string'));
}
// =========================================================================
// Filtering
// =========================================================================
/**
* Get column-level filters.
*
* Expects filters as an associative array: filters[column]=value
*
* @return array<string, string>
*/
public function filters(): array
{
$filters = $this->params['filters'] ?? [];
if (!is_array($filters)) {
return [];
}
// Remove empty filter values
return array_filter($filters, function ($value) {
return $value !== '' && $value !== null;
});
}
/**
* Get a specific filter value.
*
* @param string $column
* @return string|null
*/
public function filter(string $column): ?string
{
$filters = $this->filters();
return isset($filters[$column]) ? (string)$filters[$column] : null;
}
// =========================================================================
// Raw Access
// =========================================================================
/**
* Get any raw parameter by key.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get(string $key, $default = null)
{
return $this->params[$key] ?? $default;
}
/**
* Get all raw parameters.
*
* @return array
*/
public function all(): array
{
return $this->params;
}
}