1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
//---------------------------------------------------------------------- |
4
|
|
|
// |
5
|
|
|
// Copyright (C) 2015-2016 Artem Rodygin |
6
|
|
|
// |
7
|
|
|
// This file is part of DataTables Symfony bundle. |
8
|
|
|
// |
9
|
|
|
// You should have received a copy of the MIT License along with |
10
|
|
|
// the bundle. If not, see <http://opensource.org/licenses/MIT>. |
11
|
|
|
// |
12
|
|
|
//---------------------------------------------------------------------- |
13
|
|
|
|
14
|
|
|
namespace DataTables; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* A draw query from DataTables plugin. |
18
|
|
|
* |
19
|
|
|
* @see https://www.datatables.net/manual/server-side |
20
|
|
|
* |
21
|
|
|
* @property-read int $start Index of first row to return, zero-based. |
22
|
|
|
* @property-read int $length Total number of rows to return (-1 to return all rows). |
23
|
|
|
* @property-read Search $search Global search value. |
24
|
|
|
* @property-read Order[] $order Columns ordering (zero-based column index and direction). |
25
|
|
|
* @property-read Column[] $columns Columns information (searchable, orderable, search value, etc). |
26
|
|
|
*/ |
27
|
|
|
class DataTableQuery extends ValueObject implements \JsonSerializable |
28
|
|
|
{ |
29
|
|
|
protected $start; |
30
|
|
|
protected $length; |
31
|
|
|
protected $search; |
32
|
|
|
protected $order; |
33
|
|
|
protected $columns; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Initializing constructor. |
37
|
|
|
* |
38
|
|
|
* @param Parameters $params |
39
|
|
|
*/ |
40
|
5 |
|
public function __construct(Parameters $params) |
41
|
|
|
{ |
42
|
5 |
|
$this->start = (int) $params->start; |
43
|
5 |
|
$this->length = (int) $params->length; |
44
|
|
|
|
45
|
5 |
|
$this->search = new Search( |
46
|
5 |
|
$params->search['value'], |
47
|
5 |
|
(bool) $params->search['regex'] |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
$this->order = array_map(function (array $order) { |
51
|
2 |
|
return new Order( |
52
|
2 |
|
(int) $order['column'], |
53
|
2 |
|
$order['dir'] |
54
|
|
|
); |
55
|
5 |
|
}, $params->order); |
56
|
|
|
|
57
|
|
|
$this->columns = array_map(function (array $column) { |
58
|
2 |
|
return new Column( |
59
|
2 |
|
$column['data'], |
60
|
2 |
|
$column['name'], |
61
|
2 |
|
(bool) $column['searchable'], |
62
|
2 |
|
(bool) $column['orderable'], |
63
|
2 |
|
new Search($column['search']['value'], (bool) $column['search']['regex']) |
64
|
|
|
); |
65
|
5 |
|
}, $params->columns); |
66
|
5 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function jsonSerialize() |
72
|
|
|
{ |
73
|
1 |
|
$callback = function (\JsonSerializable $item) { |
74
|
1 |
|
return $item->jsonSerialize(); |
75
|
1 |
|
}; |
76
|
|
|
|
77
|
|
|
return [ |
78
|
1 |
|
'start' => $this->start, |
79
|
1 |
|
'length' => $this->length, |
80
|
1 |
|
'search' => $this->search->jsonSerialize(), |
81
|
1 |
|
'order' => array_map($callback, $this->order), |
82
|
1 |
|
'columns' => array_map($callback, $this->columns), |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|