1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace WebServCo\Framework\DataTables; |
||
6 | |||
7 | use WebServCo\Framework\ArrayObject\Items; |
||
8 | |||
9 | class RequestHelper extends AbstractHelper |
||
10 | { |
||
11 | /** |
||
12 | * @param array<string,mixed> $data |
||
13 | * @param array<int,string> $required |
||
14 | */ |
||
15 | public static function init(array $data, array $required = []): Request |
||
16 | { |
||
17 | $required = $required; // reserved for future use. |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
18 | |||
19 | parent::validate($data, ['draw', 'columns', 'order', 'start', 'length', 'search']); |
||
20 | |||
21 | foreach (['columns', 'order', 'search'] as $item) { |
||
22 | if (!\is_array($data[$item])) { |
||
23 | throw new \InvalidArgumentException(\sprintf('Invalid parameter: %s.', $item)); |
||
24 | } |
||
25 | } |
||
26 | |||
27 | $columns = new Items(new ColumnArrayObject()); |
||
28 | foreach ($data['columns'] as $item) { |
||
29 | if (!\array_key_exists('data', $item) || !\array_key_exists('name', $item)) { |
||
30 | //continue; |
||
31 | } |
||
32 | $columnItem = new Column( |
||
33 | $item['data'], |
||
34 | $item['name'], |
||
35 | \filter_var($item['searchable'], \FILTER_VALIDATE_BOOLEAN), |
||
36 | \filter_var($item['orderable'], \FILTER_VALIDATE_BOOLEAN), |
||
37 | SearchHelper::init($item['search']), |
||
38 | ); |
||
39 | $columns->set(null, $columnItem); |
||
40 | } |
||
41 | |||
42 | $order = new Items(new OrderArrayObject()); |
||
43 | foreach ($data['order'] as $item) { |
||
44 | if (!\array_key_exists('column', $item) || !\array_key_exists('dir', $item)) { |
||
45 | continue; |
||
46 | } |
||
47 | $orderItem = new Order($item['column'], $item['dir']); |
||
48 | $order->set(null, $orderItem); |
||
49 | } |
||
50 | |||
51 | return new Request( |
||
52 | (int) $data['draw'], |
||
53 | $columns, |
||
54 | $order, |
||
55 | (int) $data['start'], |
||
56 | (int) $data['length'], |
||
57 | SearchHelper::init($data['search']), |
||
58 | ); |
||
59 | } |
||
60 | } |
||
61 |