1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Sylius package. |
4
|
|
|
* |
5
|
|
|
* (c) Paweł Jędrzejewski |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Sylius\Component\Grid\Definition; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Grid |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $code; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $driver; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $driverConfiguration; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $sorting = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private $limits = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private $fields = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
private $filters = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
private $actionGroups = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $code |
60
|
|
|
* @param string $driver |
61
|
|
|
* @param array $driverConfiguration |
62
|
|
|
*/ |
63
|
|
|
private function __construct($code, $driver, array $driverConfiguration) |
64
|
|
|
{ |
65
|
|
|
$this->code = $code; |
66
|
|
|
$this->driver = $driver; |
67
|
|
|
$this->driverConfiguration = $driverConfiguration; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $code |
72
|
|
|
* @param string $driver |
73
|
|
|
* @param array $driverConfiguration |
74
|
|
|
* |
75
|
|
|
* @return self |
76
|
|
|
*/ |
77
|
|
|
public static function fromCodeAndDriverConfiguration($code, $driver, array $driverConfiguration) |
78
|
|
|
{ |
79
|
|
|
return new self($code, $driver, $driverConfiguration); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function getCode() |
86
|
|
|
{ |
87
|
|
|
return $this->code; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getDriver() |
94
|
|
|
{ |
95
|
|
|
return $this->driver; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
public function getDriverConfiguration() |
102
|
|
|
{ |
103
|
|
|
return $this->driverConfiguration; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param array $driverConfiguration |
108
|
|
|
*/ |
109
|
|
|
public function setDriverConfiguration(array $driverConfiguration) |
110
|
|
|
{ |
111
|
|
|
$this->driverConfiguration = $driverConfiguration; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function getSorting() |
118
|
|
|
{ |
119
|
|
|
return $this->sorting; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param array $sorting |
124
|
|
|
*/ |
125
|
|
|
public function setSorting(array $sorting) |
126
|
|
|
{ |
127
|
|
|
$this->sorting = $sorting; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
|
public function getLimits() |
134
|
|
|
{ |
135
|
|
|
return $this->limits; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param array $limits |
140
|
|
|
*/ |
141
|
|
|
public function setLimits(array $limits) |
142
|
|
|
{ |
143
|
|
|
$this->limits = $limits; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
public function getFields() |
150
|
|
|
{ |
151
|
|
|
return $this->fields; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param Field $field |
156
|
|
|
*/ |
157
|
|
|
public function addField(Field $field) |
158
|
|
|
{ |
159
|
|
|
$name = $field->getName(); |
160
|
|
|
|
161
|
|
|
if ($this->hasField($name)) { |
162
|
|
|
throw new \InvalidArgumentException(sprintf('Field "%s" already exists.', $name)); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$this->fields[$name] = $field; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param string $name |
170
|
|
|
*/ |
171
|
|
|
public function removeField($name) |
172
|
|
|
{ |
173
|
|
|
if ($this->hasField($name)) { |
174
|
|
|
unset($this->fields[$name]); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param string $name |
180
|
|
|
* |
181
|
|
|
* @return Field |
182
|
|
|
*/ |
183
|
|
|
public function getField($name) |
184
|
|
|
{ |
185
|
|
|
if (!$this->hasField($name)) { |
186
|
|
|
throw new \InvalidArgumentException(sprintf('Field "%s" does not exist.', $name)); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $this->fields[$name]; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param Field $field |
194
|
|
|
*/ |
195
|
|
|
public function setField(Field $field) |
196
|
|
|
{ |
197
|
|
|
$name = $field->getName(); |
198
|
|
|
|
199
|
|
|
$this->fields[$name] = $field; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string $name |
204
|
|
|
* |
205
|
|
|
* @return bool |
206
|
|
|
*/ |
207
|
|
|
public function hasField($name) |
208
|
|
|
{ |
209
|
|
|
return array_key_exists($name, $this->fields); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return array |
214
|
|
|
*/ |
215
|
|
|
public function getActionGroups() |
216
|
|
|
{ |
217
|
|
|
return $this->actionGroups; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param ActionGroup $actionGroup |
222
|
|
|
*/ |
223
|
|
|
public function addActionGroup(ActionGroup $actionGroup) |
224
|
|
|
{ |
225
|
|
|
$name = $actionGroup->getName(); |
226
|
|
|
|
227
|
|
|
if ($this->hasActionGroup($name)) { |
228
|
|
|
throw new \InvalidArgumentException(sprintf('ActionGroup "%s" already exists.', $name)); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$this->actionGroups[$name] = $actionGroup; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param string $name |
236
|
|
|
*/ |
237
|
|
|
public function removeActionGroup($name) |
238
|
|
|
{ |
239
|
|
|
if ($this->hasActionGroup($name)) { |
240
|
|
|
unset($this->actionGroups[$name]); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @param string $name |
246
|
|
|
* |
247
|
|
|
* @return ActionGroup |
248
|
|
|
*/ |
249
|
|
|
public function getActionGroup($name) |
250
|
|
|
{ |
251
|
|
|
if (!$this->hasActionGroup($name)) { |
252
|
|
|
throw new \InvalidArgumentException(sprintf('ActionGroup "%s" does not exist.', $name)); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return $this->actionGroups[$name]; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param ActionGroup $actionGroup |
260
|
|
|
*/ |
261
|
|
|
public function setActionGroup(ActionGroup $actionGroup) |
262
|
|
|
{ |
263
|
|
|
$name = $actionGroup->getName(); |
264
|
|
|
|
265
|
|
|
$this->actionGroups[$name] = $actionGroup; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @param string $groupName |
270
|
|
|
* |
271
|
|
|
* @return Action[] |
272
|
|
|
*/ |
273
|
|
|
public function getActions($groupName) |
274
|
|
|
{ |
275
|
|
|
return $this->getActionGroup($groupName)->getActions(); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @param string $name |
280
|
|
|
* |
281
|
|
|
* @return bool |
282
|
|
|
*/ |
283
|
|
|
public function hasActionGroup($name) |
284
|
|
|
{ |
285
|
|
|
return array_key_exists($name, $this->actionGroups); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @return array |
290
|
|
|
*/ |
291
|
|
|
public function getFilters() |
292
|
|
|
{ |
293
|
|
|
return $this->filters; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @param Filter $filter |
298
|
|
|
*/ |
299
|
|
|
public function addFilter(Filter $filter) |
300
|
|
|
{ |
301
|
|
|
if ($this->hasFilter($name = $filter->getName())) { |
302
|
|
|
throw new \InvalidArgumentException(sprintf('Filter "%s" already exists.', $name)); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
$this->filters[$name] = $filter; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @param string $name |
310
|
|
|
*/ |
311
|
|
|
public function removeFilter($name) |
312
|
|
|
{ |
313
|
|
|
if ($this->hasFilter($name)) { |
314
|
|
|
unset($this->filters[$name]); |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @param string $name |
320
|
|
|
* |
321
|
|
|
* @return Filter |
322
|
|
|
*/ |
323
|
|
|
public function getFilter($name) |
324
|
|
|
{ |
325
|
|
|
if (!$this->hasFilter($name)) { |
326
|
|
|
throw new \InvalidArgumentException(sprintf('Filter "%s" does not exist.', $name)); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
return $this->filters[$name]; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @param Filter $filter |
334
|
|
|
*/ |
335
|
|
|
public function setFilter(Filter $filter) |
336
|
|
|
{ |
337
|
|
|
$name = $filter->getName(); |
338
|
|
|
|
339
|
|
|
$this->filters[$name] = $filter; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @param string $name |
344
|
|
|
* |
345
|
|
|
* @return bool |
346
|
|
|
*/ |
347
|
|
|
public function hasFilter($name) |
348
|
|
|
{ |
349
|
|
|
return array_key_exists($name, $this->filters); |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
|