1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Configuration; |
4
|
|
|
|
5
|
|
|
use JK\Configuration\Configuration; |
6
|
|
|
use LAG\Component\StringUtils\StringUtils; |
7
|
|
|
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; |
8
|
|
|
use Symfony\Component\OptionsResolver\Options; |
9
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
10
|
|
|
|
11
|
|
|
class MenuItemConfiguration extends Configuration |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $name; |
17
|
|
|
|
18
|
|
|
public function __construct(string $name) |
19
|
|
|
{ |
20
|
|
|
$this->name = $name; |
21
|
|
|
|
22
|
|
|
parent::__construct(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Define allowed parameters and values for this configuration, using optionsResolver component. |
27
|
|
|
*/ |
28
|
|
|
public function configureOptions(OptionsResolver $resolver) |
29
|
|
|
{ |
30
|
|
|
// user can defined an admin name |
31
|
|
|
$resolver |
32
|
|
|
->setDefaults([ |
33
|
|
|
'admin' => null, |
34
|
|
|
'action' => null, |
35
|
|
|
'route' => null, |
36
|
|
|
'route_parameters' => [], |
37
|
|
|
'uri' => null, |
38
|
|
|
'attributes' => [ |
39
|
|
|
//'class' => 'list-group-item list-group-item-action', |
40
|
|
|
], |
41
|
|
|
'linkAttributes' => [ |
42
|
|
|
'class' => 'list-group-item list-group-item-action', |
43
|
|
|
], |
44
|
|
|
'text' => null, |
45
|
|
|
'children' => [], |
46
|
|
|
]) |
47
|
|
|
->setNormalizer('admin', function (Options $options, $adminName) { |
48
|
|
|
// user has to defined either an admin name and an action name, or a route name with optional |
49
|
|
|
// parameters, or an url |
50
|
|
|
if (null === $adminName |
51
|
|
|
&& null === $options->offsetGet('route') |
52
|
|
|
&& null === $options->offsetGet('uri') |
53
|
|
|
) { |
54
|
|
|
throw new InvalidOptionsException('You should either defined an admin name, or route name or an uri'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $adminName; |
58
|
|
|
}) |
59
|
|
|
// if an admin name is set, an action name can provided. This action will be the menu link |
60
|
|
|
->setNormalizer('action', function (Options $options, $action) { |
61
|
|
|
// if an action name is provided, an admin name should be defined too |
62
|
|
|
if (null !== $action && null === $options->offsetGet('admin')) { |
63
|
|
|
throw new InvalidOptionsException('You should provide an admin name for this action '.$action); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// default to list action |
67
|
|
|
if (null !== $options->offsetGet('admin') && null === $action) { |
68
|
|
|
$action = 'list'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $action; |
72
|
|
|
}) |
73
|
|
|
->setNormalizer('children', function (Options $options, $items) { |
74
|
|
|
if (!is_array($items)) { |
75
|
|
|
$items = []; |
76
|
|
|
} |
77
|
|
|
$resolver = new OptionsResolver(); |
78
|
|
|
$resolvedItems = []; |
79
|
|
|
|
80
|
|
|
foreach ($items as $name => $item) { |
81
|
|
|
$itemConfiguration = new MenuItemConfiguration($name); |
82
|
|
|
$itemConfiguration->configureOptions($resolver); |
83
|
|
|
$itemConfiguration->setParameters($resolver->resolve($item)); |
84
|
|
|
|
85
|
|
|
$resolvedItems[$name] = $itemConfiguration; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $resolvedItems; |
89
|
|
|
}) |
90
|
|
|
->setNormalizer('text', function (Options $options, $text) { |
91
|
|
|
if ($text) { |
92
|
|
|
return $text; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($options->offsetGet('admin')) { |
96
|
|
|
$text = ucfirst($options->offsetGet('admin')); |
97
|
|
|
|
98
|
|
|
if (StringUtils::endsWith($text, 'y')) { |
99
|
|
|
$text = substr($text, 0, strlen($text) - 1).'ie'; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (!StringUtils::endsWith($text, 's')) { |
103
|
|
|
$text .= 's'; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $text; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return ucfirst($this->name); |
110
|
|
|
}) |
111
|
|
|
; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function all() |
115
|
|
|
{ |
116
|
|
|
$values = parent::all(); |
117
|
|
|
|
118
|
|
|
/** @var MenuItemConfiguration $value */ |
119
|
|
|
foreach ($values['children'] as $name => $value) { |
120
|
|
|
$values['children'][$name] = $value->all(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $values; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getName(): string |
127
|
|
|
{ |
128
|
|
|
return $this->name; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|