|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the jquery-datatables-bundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2018 WEBEWEB |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WBW\Bundle\JQuery\DataTablesBundle\Factory; |
|
13
|
|
|
|
|
14
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesColumnInterface; |
|
15
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesOrder; |
|
16
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesOrderInterface; |
|
17
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesSearch; |
|
18
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesSearchInterface; |
|
19
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesWrapper; |
|
20
|
|
|
use WBW\Library\Core\Argument\BooleanHelper; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* DataTables factory. |
|
24
|
|
|
* |
|
25
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
26
|
|
|
* @package WBW\Bundle\JQuery\DataTablesBundle\Factory |
|
27
|
|
|
*/ |
|
28
|
|
|
class DataTablesFactory { |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Parse a raw column. |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $rawColumn The raw column. |
|
34
|
|
|
* @param DataTableWrapper $wrapper The wrapper. |
|
35
|
|
|
* @return DataTablesColumnInterface Returns the column. |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function parseColumn(array $rawColumn, DataTablesWrapper $wrapper) { |
|
38
|
|
|
|
|
39
|
|
|
// Determines if the raw column is valid. |
|
40
|
|
|
if (false === array_key_exists(DataTablesColumnInterface::DATATABLES_PARAMETER_DATA, $rawColumn)) { |
|
41
|
|
|
return null; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// Check the column. |
|
45
|
|
|
$dtColumn = $wrapper->getColumn($rawColumn[DataTablesColumnInterface::DATATABLES_PARAMETER_DATA]); |
|
46
|
|
|
if (null === $dtColumn) { |
|
47
|
|
|
return null; |
|
48
|
|
|
} |
|
49
|
|
|
if ($dtColumn->getName() !== $rawColumn[DataTablesColumnInterface::DATATABLES_PARAMETER_NAME]) { |
|
50
|
|
|
return null; |
|
51
|
|
|
} |
|
52
|
|
|
if (false === $dtColumn->getSearchable()) { |
|
53
|
|
|
$dtColumn->setSearch(static::parseSearch([])); // Set a default search. |
|
54
|
|
|
return $dtColumn; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// Set the search. |
|
58
|
|
|
$dtColumn->setSearch(static::parseSearch($rawColumn[DataTablesColumnInterface::DATATABLES_PARAMETER_SEARCH])); |
|
59
|
|
|
|
|
60
|
|
|
// Return the column. |
|
61
|
|
|
return $dtColumn; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Parse a raw columns. |
|
66
|
|
|
* |
|
67
|
|
|
* @param array $rawColumns The raw columns. |
|
68
|
|
|
* @param DataTablesWrapper $wrapper The wrapper. |
|
69
|
|
|
* @return DataTablesColumnInterface[] Returns the columns. |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function parseColumns(array $rawColumns, DataTablesWrapper $wrapper) { |
|
72
|
|
|
|
|
73
|
|
|
// Initialize the columns. |
|
74
|
|
|
$dtColumns = []; |
|
75
|
|
|
|
|
76
|
|
|
// Handle each column. |
|
77
|
|
|
foreach ($rawColumns as $current) { |
|
78
|
|
|
|
|
79
|
|
|
// Parse the column. |
|
80
|
|
|
$dtColumn = static::parseColumn($current, $wrapper); |
|
81
|
|
|
|
|
82
|
|
|
// Check the column. |
|
83
|
|
|
if (null === $dtColumn) { |
|
84
|
|
|
continue; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// Add the column. |
|
88
|
|
|
$dtColumns[] = $dtColumn; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// Returns the columns. |
|
92
|
|
|
return $dtColumns; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Parse a raw order. |
|
97
|
|
|
* |
|
98
|
|
|
* @param array $rawOrder The raw order. |
|
99
|
|
|
* @return DataTablesOrderInterface Returns the order. |
|
100
|
|
|
*/ |
|
101
|
|
|
public static function parseOrder(array $rawOrder) { |
|
102
|
|
|
|
|
103
|
|
|
// Initialize an order. |
|
104
|
|
|
$dtOrder = new DataTablesOrder(); |
|
105
|
|
|
|
|
106
|
|
|
// Determines if the raw order is valid. |
|
107
|
|
|
if (false === array_key_exists(DataTablesOrderInterface::DATATABLES_PARAMETER_COLUMN, $rawOrder)) { |
|
108
|
|
|
return $dtOrder; |
|
109
|
|
|
} |
|
110
|
|
|
if (false === array_key_exists(DataTablesOrderInterface::DATATABLES_PARAMETER_DIR, $rawOrder)) { |
|
111
|
|
|
return $dtOrder; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
// Set the order. |
|
115
|
|
|
$dtOrder->setColumn(intval($rawOrder[DataTablesOrderInterface::DATATABLES_PARAMETER_COLUMN])); |
|
116
|
|
|
$dtOrder->setDir($rawOrder[DataTablesOrderInterface::DATATABLES_PARAMETER_DIR]); |
|
117
|
|
|
|
|
118
|
|
|
// Return the order. |
|
119
|
|
|
return $dtOrder; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Parse raw orders. |
|
124
|
|
|
* |
|
125
|
|
|
* @param array $rawOrders The raw orders. |
|
126
|
|
|
* @return DataTablesOrderInterface[] Returns the orders. |
|
127
|
|
|
*/ |
|
128
|
|
|
public static function parseOrders(array $rawOrders) { |
|
129
|
|
|
|
|
130
|
|
|
// Initialize the orders. |
|
131
|
|
|
$dtOrders = []; |
|
132
|
|
|
|
|
133
|
|
|
// Handle each raw order. |
|
134
|
|
|
foreach ($rawOrders as $current) { |
|
135
|
|
|
$dtOrders[] = static::parseOrder($current); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
// Return the orders. |
|
139
|
|
|
return $dtOrders; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Parse a raw search. |
|
144
|
|
|
* |
|
145
|
|
|
* @param array $rawSearch The raw search. |
|
146
|
|
|
* @return DataTablesSearchInterface Returns the search. |
|
147
|
|
|
*/ |
|
148
|
|
|
public static function parseSearch(array $rawSearch) { |
|
149
|
|
|
|
|
150
|
|
|
// Initialize a search. |
|
151
|
|
|
$dtSearch = new DataTablesSearch(); |
|
152
|
|
|
|
|
153
|
|
|
// Determines if the raw search is valid. |
|
154
|
|
|
if (false === array_key_exists(DataTablesSearchInterface::DATATABLES_PARAMETER_REGEX, $rawSearch)) { |
|
155
|
|
|
return $dtSearch; |
|
156
|
|
|
} |
|
157
|
|
|
if (false === array_key_exists(DataTablesSearchInterface::DATATABLES_PARAMETER_VALUE, $rawSearch)) { |
|
158
|
|
|
return $dtSearch; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
// Set the search. |
|
162
|
|
|
$dtSearch->setRegex(BooleanHelper::parseString($rawSearch[DataTablesSearchInterface::DATATABLES_PARAMETER_REGEX])); |
|
163
|
|
|
$dtSearch->setValue($rawSearch[DataTablesSearchInterface::DATATABLES_PARAMETER_VALUE]); |
|
164
|
|
|
|
|
165
|
|
|
// Return the search. |
|
166
|
|
|
return $dtSearch; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
|