|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Sylius\Component\Grid\Definition; |
|
15
|
|
|
|
|
16
|
|
|
use Sylius\Component\Grid\Event\GridDefinitionConverterEvent; |
|
17
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
final class ArrayToDefinitionConverter implements ArrayToDefinitionConverterInterface |
|
23
|
|
|
{ |
|
24
|
|
|
public const EVENT_NAME = 'sylius.grid.%s'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var EventDispatcherInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $eventDispatcher; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function convert(string $code, array $configuration): Grid |
|
43
|
|
|
{ |
|
44
|
|
|
$grid = Grid::fromCodeAndDriverConfiguration( |
|
45
|
|
|
$code, |
|
46
|
|
|
$configuration['driver']['name'], |
|
47
|
|
|
$configuration['driver']['options'] |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
if (array_key_exists('sorting', $configuration)) { |
|
51
|
|
|
$grid->setSorting($configuration['sorting']); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if (array_key_exists('limits', $configuration)) { |
|
55
|
|
|
$grid->setLimits($configuration['limits']); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
foreach ($configuration['fields'] as $name => $fieldConfiguration) { |
|
59
|
|
|
$grid->addField($this->convertField($name, $fieldConfiguration)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
foreach ($configuration['filters'] as $name => $filterConfiguration) { |
|
63
|
|
|
$grid->addFilter($this->convertFilter($name, $filterConfiguration)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
foreach ($configuration['actions'] as $name => $actionGroupConfiguration) { |
|
67
|
|
|
$grid->addActionGroup($this->convertActionGroup($name, $actionGroupConfiguration)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->eventDispatcher->dispatch($this->getEventName($code), new GridDefinitionConverterEvent($grid)); |
|
71
|
|
|
|
|
72
|
|
|
return $grid; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $name |
|
77
|
|
|
* @param array $configuration |
|
78
|
|
|
* |
|
79
|
|
|
* @return Field |
|
80
|
|
|
*/ |
|
81
|
|
|
private function convertField(string $name, array $configuration): Field |
|
82
|
|
|
{ |
|
83
|
|
|
$field = Field::fromNameAndType($name, $configuration['type']); |
|
84
|
|
|
|
|
85
|
|
|
if (array_key_exists('path', $configuration)) { |
|
86
|
|
|
$field->setPath($configuration['path']); |
|
87
|
|
|
} |
|
88
|
|
|
if (array_key_exists('label', $configuration)) { |
|
89
|
|
|
$field->setLabel($configuration['label']); |
|
90
|
|
|
} |
|
91
|
|
|
if (array_key_exists('enabled', $configuration)) { |
|
92
|
|
|
$field->setEnabled($configuration['enabled']); |
|
93
|
|
|
} |
|
94
|
|
|
if (array_key_exists('sortable', $configuration)) { |
|
95
|
|
|
$sortable = $configuration['sortable']; |
|
96
|
|
|
|
|
97
|
|
|
if ($sortable === true || $sortable === null) { |
|
98
|
|
|
$sortable = $name; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if ($sortable === false) { |
|
102
|
|
|
$sortable = null; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$field->setSortable($sortable); |
|
106
|
|
|
} |
|
107
|
|
|
if (array_key_exists('position', $configuration)) { |
|
108
|
|
|
$field->setPosition($configuration['position']); |
|
109
|
|
|
} |
|
110
|
|
|
if (array_key_exists('options', $configuration)) { |
|
111
|
|
|
$field->setOptions($configuration['options']); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $field; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param string $name |
|
119
|
|
|
* @param array $configuration |
|
120
|
|
|
* |
|
121
|
|
|
* @return Filter |
|
122
|
|
|
*/ |
|
123
|
|
|
private function convertFilter(string $name, array $configuration): Filter |
|
124
|
|
|
{ |
|
125
|
|
|
$filter = Filter::fromNameAndType($name, $configuration['type']); |
|
126
|
|
|
|
|
127
|
|
|
if (array_key_exists('label', $configuration)) { |
|
128
|
|
|
$filter->setLabel($configuration['label']); |
|
129
|
|
|
} |
|
130
|
|
|
if (array_key_exists('template', $configuration)) { |
|
131
|
|
|
$filter->setTemplate($configuration['template']); |
|
132
|
|
|
} |
|
133
|
|
|
if (array_key_exists('enabled', $configuration)) { |
|
134
|
|
|
$filter->setEnabled($configuration['enabled']); |
|
135
|
|
|
} |
|
136
|
|
|
if (array_key_exists('position', $configuration)) { |
|
137
|
|
|
$filter->setPosition($configuration['position']); |
|
138
|
|
|
} |
|
139
|
|
|
if (array_key_exists('options', $configuration)) { |
|
140
|
|
|
$filter->setOptions($configuration['options']); |
|
141
|
|
|
} |
|
142
|
|
|
if (array_key_exists('form_options', $configuration)) { |
|
143
|
|
|
$filter->setFormOptions($configuration['form_options']); |
|
144
|
|
|
} |
|
145
|
|
|
if (array_key_exists('default_value', $configuration)) { |
|
146
|
|
|
$filter->setCriteria($configuration['default_value']); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $filter; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param string $name |
|
154
|
|
|
* @param array $configuration |
|
155
|
|
|
* |
|
156
|
|
|
* @return ActionGroup |
|
157
|
|
|
*/ |
|
158
|
|
|
private function convertActionGroup(string $name, array $configuration): ActionGroup |
|
159
|
|
|
{ |
|
160
|
|
|
$actionGroup = ActionGroup::named($name); |
|
161
|
|
|
|
|
162
|
|
|
foreach ($configuration as $actionName => $actionConfiguration) { |
|
163
|
|
|
$actionGroup->addAction($this->convertAction($actionName, $actionConfiguration)); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return $actionGroup; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param string $name |
|
171
|
|
|
* @param array $configuration |
|
172
|
|
|
* |
|
173
|
|
|
* @return Action |
|
174
|
|
|
*/ |
|
175
|
|
|
private function convertAction(string $name, array $configuration): Action |
|
176
|
|
|
{ |
|
177
|
|
|
$action = Action::fromNameAndType($name, $configuration['type']); |
|
178
|
|
|
|
|
179
|
|
|
if (array_key_exists('label', $configuration)) { |
|
180
|
|
|
$action->setLabel($configuration['label']); |
|
181
|
|
|
} |
|
182
|
|
|
if (array_key_exists('icon', $configuration)) { |
|
183
|
|
|
$action->setIcon($configuration['icon']); |
|
184
|
|
|
} |
|
185
|
|
|
if (array_key_exists('enabled', $configuration)) { |
|
186
|
|
|
$action->setEnabled($configuration['enabled']); |
|
187
|
|
|
} |
|
188
|
|
|
if (array_key_exists('position', $configuration)) { |
|
189
|
|
|
$action->setPosition($configuration['position']); |
|
190
|
|
|
} |
|
191
|
|
|
if (array_key_exists('options', $configuration)) { |
|
192
|
|
|
$action->setOptions($configuration['options']); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
return $action; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param string $code |
|
200
|
|
|
* |
|
201
|
|
|
* @return string |
|
202
|
|
|
*/ |
|
203
|
|
|
private function getEventName(string $code): string |
|
204
|
|
|
{ |
|
205
|
|
|
return sprintf(self::EVENT_NAME, str_replace('sylius_', '', $code)); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|