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
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
|
|
public function getEnabledFields() |
158
|
|
|
{ |
159
|
|
|
return $this->getEnabledItems($this->getFields()); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param Field $field |
164
|
|
|
*/ |
165
|
|
|
public function addField(Field $field) |
166
|
|
|
{ |
167
|
|
|
$name = $field->getName(); |
168
|
|
|
|
169
|
|
|
if ($this->hasField($name)) { |
170
|
|
|
throw new \InvalidArgumentException(sprintf('Field "%s" already exists.', $name)); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$this->fields[$name] = $field; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param string $name |
178
|
|
|
*/ |
179
|
|
|
public function removeField($name) |
180
|
|
|
{ |
181
|
|
|
if ($this->hasField($name)) { |
182
|
|
|
unset($this->fields[$name]); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param string $name |
188
|
|
|
* |
189
|
|
|
* @return Field |
190
|
|
|
*/ |
191
|
|
|
public function getField($name) |
192
|
|
|
{ |
193
|
|
|
if (!$this->hasField($name)) { |
194
|
|
|
throw new \InvalidArgumentException(sprintf('Field "%s" does not exist.', $name)); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $this->fields[$name]; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param Field $field |
202
|
|
|
*/ |
203
|
|
|
public function setField(Field $field) |
204
|
|
|
{ |
205
|
|
|
$name = $field->getName(); |
206
|
|
|
|
207
|
|
|
$this->fields[$name] = $field; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param string $name |
212
|
|
|
* |
213
|
|
|
* @return bool |
214
|
|
|
*/ |
215
|
|
|
public function hasField($name) |
216
|
|
|
{ |
217
|
|
|
return array_key_exists($name, $this->fields); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return array |
222
|
|
|
*/ |
223
|
|
|
public function getActionGroups() |
224
|
|
|
{ |
225
|
|
|
return $this->actionGroups; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @return array |
230
|
|
|
*/ |
231
|
|
|
public function getEnabledActionGroups() |
232
|
|
|
{ |
233
|
|
|
return $this->getEnabledItems($this->getActionGroups()); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param ActionGroup $actionGroup |
238
|
|
|
*/ |
239
|
|
|
public function addActionGroup(ActionGroup $actionGroup) |
240
|
|
|
{ |
241
|
|
|
$name = $actionGroup->getName(); |
242
|
|
|
|
243
|
|
|
if ($this->hasActionGroup($name)) { |
244
|
|
|
throw new \InvalidArgumentException(sprintf('ActionGroup "%s" already exists.', $name)); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
$this->actionGroups[$name] = $actionGroup; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param string $name |
252
|
|
|
*/ |
253
|
|
|
public function removeActionGroup($name) |
254
|
|
|
{ |
255
|
|
|
if ($this->hasActionGroup($name)) { |
256
|
|
|
unset($this->actionGroups[$name]); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @param string $name |
262
|
|
|
* |
263
|
|
|
* @return ActionGroup |
264
|
|
|
*/ |
265
|
|
|
public function getActionGroup($name) |
266
|
|
|
{ |
267
|
|
|
if (!$this->hasActionGroup($name)) { |
268
|
|
|
throw new \InvalidArgumentException(sprintf('ActionGroup "%s" does not exist.', $name)); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return $this->actionGroups[$name]; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @param ActionGroup $actionGroup |
276
|
|
|
*/ |
277
|
|
|
public function setActionGroup(ActionGroup $actionGroup) |
278
|
|
|
{ |
279
|
|
|
$name = $actionGroup->getName(); |
280
|
|
|
|
281
|
|
|
$this->actionGroups[$name] = $actionGroup; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @param string $groupName |
286
|
|
|
* |
287
|
|
|
* @return Action[] |
288
|
|
|
*/ |
289
|
|
|
public function getActions($groupName) |
290
|
|
|
{ |
291
|
|
|
return $this->getActionGroup($groupName)->getActions(); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @return array |
296
|
|
|
*/ |
297
|
|
|
public function getEnabledActions($groupName) |
298
|
|
|
{ |
299
|
|
|
return $this->getEnabledItems($this->getActions($groupName)); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @param string $name |
304
|
|
|
* |
305
|
|
|
* @return bool |
306
|
|
|
*/ |
307
|
|
|
public function hasActionGroup($name) |
308
|
|
|
{ |
309
|
|
|
return array_key_exists($name, $this->actionGroups); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @return array |
314
|
|
|
*/ |
315
|
|
|
public function getFilters() |
316
|
|
|
{ |
317
|
|
|
return $this->filters; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @return array |
322
|
|
|
*/ |
323
|
|
|
public function getEnabledFilters() |
324
|
|
|
{ |
325
|
|
|
return $this->getEnabledItems($this->getFilters()); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @param Filter $filter |
330
|
|
|
*/ |
331
|
|
|
public function addFilter(Filter $filter) |
332
|
|
|
{ |
333
|
|
|
if ($this->hasFilter($name = $filter->getName())) { |
334
|
|
|
throw new \InvalidArgumentException(sprintf('Filter "%s" already exists.', $name)); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
$this->filters[$name] = $filter; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* @param string $name |
342
|
|
|
*/ |
343
|
|
|
public function removeFilter($name) |
344
|
|
|
{ |
345
|
|
|
if ($this->hasFilter($name)) { |
346
|
|
|
unset($this->filters[$name]); |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @param string $name |
352
|
|
|
* |
353
|
|
|
* @return Filter |
354
|
|
|
*/ |
355
|
|
|
public function getFilter($name) |
356
|
|
|
{ |
357
|
|
|
if (!$this->hasFilter($name)) { |
358
|
|
|
throw new \InvalidArgumentException(sprintf('Filter "%s" does not exist.', $name)); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
return $this->filters[$name]; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @param Filter $filter |
366
|
|
|
*/ |
367
|
|
|
public function setFilter(Filter $filter) |
368
|
|
|
{ |
369
|
|
|
$name = $filter->getName(); |
370
|
|
|
|
371
|
|
|
$this->filters[$name] = $filter; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @param string $name |
376
|
|
|
* |
377
|
|
|
* @return bool |
378
|
|
|
*/ |
379
|
|
|
public function hasFilter($name) |
380
|
|
|
{ |
381
|
|
|
return array_key_exists($name, $this->filters); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* @param array $items |
386
|
|
|
* |
387
|
|
|
* @return array |
388
|
|
|
*/ |
389
|
|
|
private function getEnabledItems(array $items) |
390
|
|
|
{ |
391
|
|
|
$filteredItems = []; |
392
|
|
|
foreach ($items as $item) { |
393
|
|
|
if ($item->isEnabled()) { |
394
|
|
|
$filteredItems[] = $item; |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
return $filteredItems; |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|