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
|
|
|
if (false === array_key_exists(DataTablesColumnInterface::DATATABLES_PARAMETER_NAME, $rawColumn)) { |
44
|
|
|
return null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Check the column. |
48
|
|
|
$dtColumn = $wrapper->getColumn($rawColumn[DataTablesColumnInterface::DATATABLES_PARAMETER_DATA]); |
49
|
|
|
if (null === $dtColumn) { |
50
|
|
|
return null; |
51
|
|
|
} |
52
|
|
|
if ($dtColumn->getName() !== $rawColumn[DataTablesColumnInterface::DATATABLES_PARAMETER_NAME]) { |
53
|
|
|
return null; |
54
|
|
|
} |
55
|
|
|
if (false === $dtColumn->getSearchable()) { |
56
|
|
|
$dtColumn->setSearch(static::parseSearch([])); // Set a default search. |
57
|
|
|
return $dtColumn; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Set the search. |
61
|
|
|
$dtColumn->setSearch(static::parseSearch($rawColumn[DataTablesColumnInterface::DATATABLES_PARAMETER_SEARCH])); |
62
|
|
|
|
63
|
|
|
// Return the column. |
64
|
|
|
return $dtColumn; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Parse a raw columns. |
69
|
|
|
* |
70
|
|
|
* @param array $rawColumns The raw columns. |
71
|
|
|
* @param DataTablesWrapper $wrapper The wrapper. |
72
|
|
|
* @return DataTablesColumnInterface[] Returns the columns. |
73
|
|
|
*/ |
74
|
|
|
public static function parseColumns(array $rawColumns, DataTablesWrapper $wrapper) { |
75
|
|
|
|
76
|
|
|
// Initialize the columns. |
77
|
|
|
$dtColumns = []; |
78
|
|
|
|
79
|
|
|
// Handle each column. |
80
|
|
|
foreach ($rawColumns as $current) { |
81
|
|
|
|
82
|
|
|
// Parse the column. |
83
|
|
|
$dtColumn = static::parseColumn($current, $wrapper); |
84
|
|
|
|
85
|
|
|
// Check the column. |
86
|
|
|
if (null === $dtColumn) { |
87
|
|
|
continue; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// Add the column. |
91
|
|
|
$dtColumns[] = $dtColumn; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Returns the columns. |
95
|
|
|
return $dtColumns; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Parse a raw order. |
100
|
|
|
* |
101
|
|
|
* @param array $rawOrder The raw order. |
102
|
|
|
* @return DataTablesOrderInterface Returns the order. |
103
|
|
|
*/ |
104
|
|
|
public static function parseOrder(array $rawOrder) { |
105
|
|
|
|
106
|
|
|
// Initialize an order. |
107
|
|
|
$dtOrder = new DataTablesOrder(); |
108
|
|
|
|
109
|
|
|
// Determines if the raw order is valid. |
110
|
|
|
if (false === array_key_exists(DataTablesOrderInterface::DATATABLES_PARAMETER_COLUMN, $rawOrder)) { |
111
|
|
|
return $dtOrder; |
112
|
|
|
} |
113
|
|
|
if (false === array_key_exists(DataTablesOrderInterface::DATATABLES_PARAMETER_DIR, $rawOrder)) { |
114
|
|
|
return $dtOrder; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// Set the order. |
118
|
|
|
$dtOrder->setColumn(intval($rawOrder[DataTablesOrderInterface::DATATABLES_PARAMETER_COLUMN])); |
119
|
|
|
$dtOrder->setDir($rawOrder[DataTablesOrderInterface::DATATABLES_PARAMETER_DIR]); |
120
|
|
|
|
121
|
|
|
// Return the order. |
122
|
|
|
return $dtOrder; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Parse raw orders. |
127
|
|
|
* |
128
|
|
|
* @param array $rawOrders The raw orders. |
129
|
|
|
* @return DataTablesOrderInterface[] Returns the orders. |
130
|
|
|
*/ |
131
|
|
|
public static function parseOrders(array $rawOrders) { |
132
|
|
|
|
133
|
|
|
// Initialize the orders. |
134
|
|
|
$dtOrders = []; |
135
|
|
|
|
136
|
|
|
// Handle each raw order. |
137
|
|
|
foreach ($rawOrders as $current) { |
138
|
|
|
$dtOrders[] = static::parseOrder($current); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// Return the orders. |
142
|
|
|
return $dtOrders; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Parse a raw search. |
147
|
|
|
* |
148
|
|
|
* @param array $rawSearch The raw search. |
149
|
|
|
* @return DataTablesSearchInterface Returns the search. |
150
|
|
|
*/ |
151
|
|
|
public static function parseSearch(array $rawSearch) { |
152
|
|
|
|
153
|
|
|
// Initialize a search. |
154
|
|
|
$dtSearch = new DataTablesSearch(); |
155
|
|
|
|
156
|
|
|
// Determines if the raw search is valid. |
157
|
|
|
if (false === array_key_exists(DataTablesSearchInterface::DATATABLES_PARAMETER_REGEX, $rawSearch)) { |
158
|
|
|
return $dtSearch; |
159
|
|
|
} |
160
|
|
|
if (false === array_key_exists(DataTablesSearchInterface::DATATABLES_PARAMETER_VALUE, $rawSearch)) { |
161
|
|
|
return $dtSearch; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
// Set the search. |
165
|
|
|
$dtSearch->setRegex(BooleanHelper::parseString($rawSearch[DataTablesSearchInterface::DATATABLES_PARAMETER_REGEX])); |
166
|
|
|
$dtSearch->setValue($rawSearch[DataTablesSearchInterface::DATATABLES_PARAMETER_VALUE]); |
167
|
|
|
|
168
|
|
|
// Return the search. |
169
|
|
|
return $dtSearch; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
} |
173
|
|
|
|