|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Field\Configuration; |
|
4
|
|
|
|
|
5
|
|
|
use JK\Sam\Configuration\Configuration; |
|
6
|
|
|
use LogicException; |
|
7
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
8
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
9
|
|
|
|
|
10
|
|
|
class ActionCollectionConfiguration extends Configuration |
|
11
|
|
|
{ |
|
12
|
|
|
public function configureOptions(OptionsResolver $resolver) |
|
13
|
|
|
{ |
|
14
|
|
|
$resolver |
|
15
|
|
|
->setRequired('actions') |
|
16
|
|
|
->setAllowedTypes('actions', 'array') |
|
17
|
|
|
->setNormalizer('actions', function (Options $options, $actions) { |
|
18
|
|
|
|
|
19
|
|
|
$normalizedActions = []; |
|
20
|
|
|
|
|
21
|
|
|
foreach ($actions as $action => $options) { |
|
22
|
|
|
$this->normalizeIcon($options, $action); |
|
23
|
|
|
$this->normalizeClass($options, $action); |
|
24
|
|
|
$this->normalizeText($options, $action); |
|
25
|
|
|
|
|
26
|
|
|
$normalizedActions[$action] = $options; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return $normalizedActions; |
|
30
|
|
|
}) |
|
31
|
|
|
; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
View Code Duplication |
protected function normalizeIcon(array &$options, $action) |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
$iconMapping = [ |
|
37
|
|
|
'delete' => 'fa fa-times', |
|
38
|
|
|
'edit' => 'fa fa-pencil', |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
if (!array_key_exists('icon', $options)) { |
|
42
|
|
|
$options['icon'] = ''; |
|
43
|
|
|
|
|
44
|
|
|
if (array_key_exists($action, $iconMapping)) { |
|
45
|
|
|
$options['icon'] = $iconMapping[$action]; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
View Code Duplication |
protected function normalizeClass(array &$options, $action) |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
|
|
$classMapping = [ |
|
53
|
|
|
'delete' => 'btn btn-danger', |
|
54
|
|
|
'edit' => 'btn btn-default', |
|
55
|
|
|
]; |
|
56
|
|
|
|
|
57
|
|
|
if (!array_key_exists('class', $options)) { |
|
58
|
|
|
$options['class'] = ''; |
|
59
|
|
|
|
|
60
|
|
|
if (array_key_exists($action, $classMapping)) { |
|
61
|
|
|
$options['class'] = $classMapping[$action]; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected function normalizeRoute(array &$options, $action) |
|
67
|
|
|
{ |
|
68
|
|
|
if (!array_key_exists('route', $options)) { |
|
69
|
|
|
throw new LogicException( |
|
70
|
|
|
'You should provide a route for the Action "'.$action.'" in ActionCollection Field' |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (array_key_exists('parameters', $options)) { |
|
75
|
|
|
|
|
76
|
|
|
if (!is_array($options['parameters'])) { |
|
77
|
|
|
throw new LogicException( |
|
78
|
|
|
'You should provide an array of parameters for route configuration in action "'.$action.'"' |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function normalizeText(array &$options, $action) |
|
85
|
|
|
{ |
|
86
|
|
|
if (!array_key_exists('text', $options)) { |
|
87
|
|
|
$options['text'] = ucfirst($action); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.