1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Neimheadh\SonataAnnotationBundle\Annotation\Sonata; |
6
|
|
|
|
7
|
|
|
use Attribute; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Neimheadh\SonataAnnotationBundle\Admin\AnnotationAdmin; |
10
|
|
|
use Neimheadh\SonataAnnotationBundle\Annotation\AbstractAnnotation; |
11
|
|
|
use ReflectionException; |
12
|
|
|
use Sonata\AdminBundle\Datagrid\DatagridInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Admin annotation. |
16
|
|
|
* |
17
|
|
|
* Auto-build the admin service of your model class. |
18
|
|
|
* |
19
|
|
|
* @Annotation |
20
|
|
|
* @Target("CLASS") |
21
|
|
|
* |
22
|
|
|
* @author Marko Kunic <[email protected]> |
23
|
|
|
* @author Mathieu Wambre <[email protected]> |
24
|
|
|
* |
25
|
|
|
* @property FormField[] $formFields |
26
|
|
|
*/ |
27
|
|
|
#[Attribute(Attribute::TARGET_CLASS)] |
28
|
|
|
final class Admin extends AbstractAnnotation |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Sort values option name. |
33
|
|
|
*/ |
34
|
|
|
public const OPTION_SORT_VALUES = 'sort_values'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Admin service class. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
public string $admin = AnnotationAdmin::class; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Admin code. |
45
|
|
|
* |
46
|
|
|
* @var string|null |
47
|
|
|
*/ |
48
|
|
|
public ?string $code = null; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Admin controller. |
52
|
|
|
* |
53
|
|
|
* @var string|null |
54
|
|
|
*/ |
55
|
|
|
public ?string $controller = null; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Default sort order. |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
public string $defaultOrder = 'ASC'; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Default list first page. |
66
|
|
|
* |
67
|
|
|
* @var int |
68
|
|
|
*/ |
69
|
|
|
public int $defaultPage = 1; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Default page size. |
73
|
|
|
* |
74
|
|
|
* @var int|null |
75
|
|
|
*/ |
76
|
|
|
public ?int $defaultPageSize = null; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Default sort field. |
80
|
|
|
* |
81
|
|
|
* @var string|null |
82
|
|
|
*/ |
83
|
|
|
public ?string $defaultSort = null; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Admin group. |
87
|
|
|
* |
88
|
|
|
* @var string|null |
89
|
|
|
*/ |
90
|
|
|
public ?string $group = null; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Admin link icon. |
94
|
|
|
* |
95
|
|
|
* @var string|null |
96
|
|
|
*/ |
97
|
|
|
public ?string $icon = null; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Is admin is kept open. |
101
|
|
|
* |
102
|
|
|
* @var bool |
103
|
|
|
*/ |
104
|
|
|
public bool $keepOpen = false; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Admin label. |
108
|
|
|
* |
109
|
|
|
* @var string|null |
110
|
|
|
*/ |
111
|
|
|
public ?string $label = null; |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Admin label translation catalogue. |
115
|
|
|
* |
116
|
|
|
* @var string|null |
117
|
|
|
*/ |
118
|
|
|
public ?string $labelCatalogue = null; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Admin label translator strategy. |
122
|
|
|
* |
123
|
|
|
* @var string|null |
124
|
|
|
*/ |
125
|
|
|
public ?string $labelTranslatorStrategy = null; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Admin model manager type. |
129
|
|
|
* |
130
|
|
|
* @var string |
131
|
|
|
*/ |
132
|
|
|
public string $managerType = 'orm'; |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Is admin a top menu? |
136
|
|
|
* |
137
|
|
|
* This option put your admin link directly in the menu and not as a |
138
|
|
|
* sub-menu. |
139
|
|
|
* |
140
|
|
|
* @var bool |
141
|
|
|
*/ |
142
|
|
|
public bool $onTop = false; |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Admin pager type. |
146
|
|
|
* |
147
|
|
|
* @var string|null |
148
|
|
|
*/ |
149
|
|
|
public ?string $pagerType = null; |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Admin service id. |
153
|
|
|
* |
154
|
|
|
* @var string|null |
155
|
|
|
*/ |
156
|
|
|
public ?string $serviceId = null; |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Is admin shown in dashboard? |
160
|
|
|
* |
161
|
|
|
* @var bool |
162
|
|
|
*/ |
163
|
|
|
public bool $showInDashboard = true; |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Datagrid fields. |
167
|
|
|
* |
168
|
|
|
* @var array<DatagridField> |
169
|
|
|
*/ |
170
|
|
|
private array $datagridFields = []; |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Export fields. |
174
|
|
|
* |
175
|
|
|
* @var array<ExportField> |
176
|
|
|
*/ |
177
|
|
|
private array $exportFields = []; |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Form fields. |
181
|
|
|
* |
182
|
|
|
* @var array<FormField> |
183
|
|
|
*/ |
184
|
|
|
private array $formFields = []; |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* List fields. |
188
|
|
|
* |
189
|
|
|
* @var array<ListField> |
190
|
|
|
*/ |
191
|
|
|
private array $listFields = []; |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Show fields. |
195
|
|
|
* |
196
|
|
|
* @var array<ShowField> |
197
|
|
|
*/ |
198
|
|
|
private array $showFields = []; |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param array|string|null $label Label or annotation |
202
|
|
|
* parameters. |
203
|
|
|
* @param string $managerType Model manager type. |
204
|
|
|
* @param string|null $group Admin group. |
205
|
|
|
* @param bool $showInDashboard Show in dashboard? |
206
|
|
|
* @param bool $keepOpen Keep open. |
207
|
|
|
* @param bool $onTop Is admin a top menu? |
208
|
|
|
* @param string|null $icon Admin link icon. |
209
|
|
|
* @param string|null $labelTranslatorStrategy Label translator |
210
|
|
|
* strategy. |
211
|
|
|
* @param string|null $labelCatalogue Admin label |
212
|
|
|
* translation |
213
|
|
|
* catalogue. |
214
|
|
|
* @param string|null $pagerType Pager type. |
215
|
|
|
* @param string|null $controller Controller. |
216
|
|
|
* @param string|null $serviceId Service id. |
217
|
|
|
* @param string $admin Service class. |
218
|
|
|
* @param string|null $code Code. |
219
|
|
|
* @param DatagridField[] $datagridFields Datagrid fields. |
220
|
|
|
* @param FormField[] $formFields Form fields. |
221
|
|
|
* @param ListField[] $listFields List fields. |
222
|
|
|
* @param ShowField[] $showFields Show fields. |
223
|
|
|
* |
224
|
|
|
* @throws ReflectionException |
225
|
|
|
*/ |
226
|
|
|
public function __construct( |
227
|
|
|
array|string $label = null, |
228
|
|
|
string $managerType = 'orm', |
229
|
|
|
?string $group = null, |
230
|
|
|
bool $showInDashboard = true, |
231
|
|
|
bool $keepOpen = false, |
232
|
|
|
bool $onTop = false, |
233
|
|
|
?string $icon = null, |
234
|
|
|
?string $labelTranslatorStrategy = null, |
235
|
|
|
?string $labelCatalogue = null, |
236
|
|
|
?string $pagerType = null, |
237
|
|
|
?string $controller = null, |
238
|
|
|
?string $serviceId = null, |
239
|
|
|
string $admin = AnnotationAdmin::class, |
240
|
|
|
?string $code = null, |
241
|
|
|
array $datagridFields = [], |
242
|
|
|
array $formFields = [], |
243
|
|
|
array $listFields = [], |
244
|
|
|
array $showFields = [], |
245
|
|
|
array $exportFields = [], |
246
|
|
|
int $defaultPage = 1, |
247
|
|
|
string $defaultOrder = 'ASC', |
248
|
|
|
?string $defaultSort = null, |
249
|
|
|
?int $defaultPageSize = null, |
250
|
|
|
) { |
251
|
|
|
$this->managerType = $managerType; |
252
|
|
|
$this->group = $group; |
253
|
|
|
$this->showInDashboard = $showInDashboard; |
254
|
|
|
$this->keepOpen = $keepOpen; |
255
|
|
|
$this->onTop = $onTop; |
256
|
|
|
$this->icon = $icon; |
257
|
|
|
$this->labelTranslatorStrategy = $labelTranslatorStrategy; |
258
|
|
|
$this->labelCatalogue = $labelCatalogue; |
259
|
|
|
$this->pagerType = $pagerType; |
260
|
|
|
$this->controller = $controller; |
261
|
|
|
$this->serviceId = $serviceId; |
262
|
|
|
$this->admin = $admin; |
263
|
|
|
$this->code = $code; |
264
|
|
|
$this->defaultPage = $defaultPage; |
265
|
|
|
$this->defaultOrder = $defaultOrder; |
266
|
|
|
$this->defaultSort = $defaultSort; |
267
|
|
|
$this->defaultPageSize = $defaultPageSize; |
268
|
|
|
|
269
|
|
|
$this->setDatagridFields($datagridFields) |
270
|
|
|
->setFormFields($formFields) |
271
|
|
|
->setListFields($listFields) |
272
|
|
|
->setShowFields($showFields) |
273
|
|
|
->setExportFields($exportFields); |
274
|
|
|
|
275
|
|
|
if (is_array($label)) { |
276
|
|
|
$this->initAnnotation($label); |
277
|
|
|
} else { |
278
|
|
|
$this->label = $label; |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Get admin options. |
284
|
|
|
* |
285
|
|
|
* @return array |
286
|
|
|
*/ |
287
|
|
|
public function getOptions(): array |
288
|
|
|
{ |
289
|
|
|
return [ |
290
|
|
|
self::OPTION_SORT_VALUES => [ |
291
|
|
|
DatagridInterface::PAGE => $this->defaultPage, |
292
|
|
|
DatagridInterface::PER_PAGE => $this->defaultPageSize, |
293
|
|
|
DatagridInterface::SORT_BY => $this->defaultSort, |
294
|
|
|
DatagridInterface::SORT_ORDER => $this->defaultOrder, |
295
|
|
|
] |
296
|
|
|
]; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Get service "sonata.admin" tag options. |
301
|
|
|
* |
302
|
|
|
* @return array<string|bool> |
303
|
|
|
*/ |
304
|
|
|
public function getTagOptions(): array |
305
|
|
|
{ |
306
|
|
|
return [ |
307
|
|
|
'code' => $this->code, |
308
|
|
|
'controller' => $this->controller, |
309
|
|
|
'manager_type' => $this->managerType, |
310
|
|
|
'group' => $this->group, |
311
|
|
|
'label' => $this->label, |
312
|
|
|
'show_in_dashboard' => $this->showInDashboard, |
313
|
|
|
'keep_open' => $this->keepOpen, |
314
|
|
|
'on_top' => $this->onTop, |
315
|
|
|
'icon' => $this->icon, |
316
|
|
|
'label_translator_strategy' => $this->labelTranslatorStrategy, |
317
|
|
|
'label_catalogue' => $this->labelCatalogue, |
318
|
|
|
'pager_type' => $this->pagerType, |
319
|
|
|
]; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Get datagrid fields. |
324
|
|
|
* |
325
|
|
|
* @return array<DatagridField> |
326
|
|
|
*/ |
327
|
|
|
public function getDatagridFields(): array |
328
|
|
|
{ |
329
|
|
|
return $this->datagridFields; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Get export fields. |
334
|
|
|
* |
335
|
|
|
* @return array<ExportField> |
336
|
|
|
*/ |
337
|
|
|
public function getExportFields(): array |
338
|
|
|
{ |
339
|
|
|
return $this->exportFields; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Get form fields. |
344
|
|
|
* |
345
|
|
|
* @return array<FormField> |
346
|
|
|
*/ |
347
|
|
|
public function getFormFields(): array |
348
|
|
|
{ |
349
|
|
|
return $this->formFields; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Get list fields. |
354
|
|
|
* |
355
|
|
|
* @return array<ListField> |
356
|
|
|
*/ |
357
|
|
|
public function getListFields(): array |
358
|
|
|
{ |
359
|
|
|
return $this->listFields; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Get show fields. |
364
|
|
|
* |
365
|
|
|
* @return array<ShowField> |
366
|
|
|
*/ |
367
|
|
|
public function getShowFields(): array |
368
|
|
|
{ |
369
|
|
|
return $this->showFields; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Set datagrid fields. |
374
|
|
|
* |
375
|
|
|
* @param array<DatagridField> $datagridFields Datagrid fields. |
376
|
|
|
* |
377
|
|
|
* @return $this |
378
|
|
|
*/ |
379
|
|
|
public function setDatagridFields(array $datagridFields): self |
380
|
|
|
{ |
381
|
|
|
($e = $this->getInvalidArrayException( |
382
|
|
|
DatagridField::class, |
383
|
|
|
$datagridFields |
384
|
|
|
)) && throw $e; |
385
|
|
|
|
386
|
|
|
$this->datagridFields = $datagridFields; |
387
|
|
|
|
388
|
|
|
return $this; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Set export fields. |
393
|
|
|
* |
394
|
|
|
* @param array<ExportField> $exportFields Export fields. |
395
|
|
|
* |
396
|
|
|
* @return $this |
397
|
|
|
*/ |
398
|
|
|
public function setExportFields(array $exportFields): self |
399
|
|
|
{ |
400
|
|
|
($e = $this->getInvalidArrayException( |
401
|
|
|
ExportField::class, |
402
|
|
|
$exportFields |
403
|
|
|
)) && throw $e; |
404
|
|
|
|
405
|
|
|
$this->exportFields = $exportFields; |
406
|
|
|
|
407
|
|
|
return $this; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Set form fields. |
412
|
|
|
* |
413
|
|
|
* @param array<FormField> $formFields Form fields. |
414
|
|
|
* |
415
|
|
|
* @return $this |
416
|
|
|
*/ |
417
|
|
|
public function setFormFields(array $formFields): self |
418
|
|
|
{ |
419
|
|
|
($e = $this->getInvalidArrayException( |
420
|
|
|
FormField::class, |
421
|
|
|
$formFields |
422
|
|
|
)) && throw $e; |
423
|
|
|
|
424
|
|
|
$this->formFields = $formFields; |
425
|
|
|
|
426
|
|
|
return $this; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Set list fields. |
431
|
|
|
* |
432
|
|
|
* @param array<ListField> $listFields List fields. |
433
|
|
|
* |
434
|
|
|
* @return $this |
435
|
|
|
*/ |
436
|
|
|
public function setListFields(array $listFields): self |
437
|
|
|
{ |
438
|
|
|
($e = $this->getInvalidArrayException( |
439
|
|
|
ListField::class, |
440
|
|
|
$listFields |
441
|
|
|
)) && throw $e; |
442
|
|
|
|
443
|
|
|
$this->listFields = $listFields; |
444
|
|
|
|
445
|
|
|
return $this; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* Set show fields. |
450
|
|
|
* |
451
|
|
|
* @param array<ShowField> $showFields Show fields. |
452
|
|
|
* |
453
|
|
|
* @return $this |
454
|
|
|
*/ |
455
|
|
|
public function setShowFields(array $showFields): self |
456
|
|
|
{ |
457
|
|
|
($e = $this->getInvalidArrayException( |
458
|
|
|
ShowField::class, |
459
|
|
|
$showFields |
460
|
|
|
)) && throw $e; |
461
|
|
|
|
462
|
|
|
$this->showFields = $showFields; |
463
|
|
|
|
464
|
|
|
return $this; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* Get invalid argument exception for typed array. |
469
|
|
|
* |
470
|
|
|
* @param string $class Excepted class. |
471
|
|
|
* @param array $array Given array. |
472
|
|
|
* |
473
|
|
|
* @return InvalidArgumentException|null |
474
|
|
|
*/ |
475
|
|
|
private function getInvalidArrayException( |
476
|
|
|
string $class, |
477
|
|
|
array $array |
478
|
|
|
): ?InvalidArgumentException { |
479
|
|
|
foreach ($array as $entry) { |
480
|
|
|
if (!is_object($entry) || !$entry instanceof $class) { |
481
|
|
|
return new InvalidArgumentException( |
482
|
|
|
sprintf( |
483
|
|
|
'Array of %s expected, array contains an %s element.', |
484
|
|
|
$class, |
485
|
|
|
is_object($entry) ? $entry::class : gettype($entry) |
486
|
|
|
) |
487
|
|
|
); |
488
|
|
|
} |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
return null; |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
} |
495
|
|
|
|