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
|
|
|
namespace Sylius\Component\Grid\Definition; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
final class ArrayToDefinitionConverter implements ArrayToDefinitionConverterInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
|
|
public function convert($code, array $configuration) |
23
|
|
|
{ |
24
|
|
|
$grid = Grid::fromCodeAndDriverConfiguration($code, $configuration['driver']['name'], $configuration['driver']['options']); |
25
|
|
|
|
26
|
|
|
if (array_key_exists('sorting', $configuration)) { |
27
|
|
|
$grid->setSorting($configuration['sorting']); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
foreach ($configuration['fields'] as $name => $fieldConfiguration) { |
31
|
|
|
$grid->addField($this->convertField($name, $fieldConfiguration)); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
foreach ($configuration['filters'] as $name => $filterConfiguration) { |
35
|
|
|
$grid->addFilter($this->convertFilter($name, $filterConfiguration)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
foreach ($configuration['actions'] as $name => $actionGroupConfiguration) { |
39
|
|
|
$grid->addActionGroup($this->convertActionGroup($name, $actionGroupConfiguration)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $grid; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $name |
47
|
|
|
* @param array $configuration |
48
|
|
|
* |
49
|
|
|
* @return Field |
50
|
|
|
*/ |
51
|
|
|
private function convertField($name, array $configuration) |
52
|
|
|
{ |
53
|
|
|
$field = Field::fromNameAndType($name, $configuration['type']); |
54
|
|
|
|
55
|
|
|
if (array_key_exists('path', $configuration)) { |
56
|
|
|
$field->setPath($configuration['path']); |
57
|
|
|
} |
58
|
|
|
if (array_key_exists('label', $configuration)) { |
59
|
|
|
$field->setLabel($configuration['label']); |
60
|
|
|
} |
61
|
|
|
if (array_key_exists('enabled', $configuration)) { |
62
|
|
|
$field->setEnabled($configuration['enabled']); |
63
|
|
|
} |
64
|
|
|
if (array_key_exists('sortable', $configuration)) { |
65
|
|
|
$field->setSortable($configuration['sortable']); |
66
|
|
|
} |
67
|
|
|
if (array_key_exists('position', $configuration)) { |
68
|
|
|
$field->setPosition($configuration['position']); |
69
|
|
|
} |
70
|
|
|
if (array_key_exists('options', $configuration)) { |
71
|
|
|
$field->setOptions($configuration['options']); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $field; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $name |
79
|
|
|
* @param array $configuration |
80
|
|
|
* |
81
|
|
|
* @return Filter |
82
|
|
|
*/ |
83
|
|
|
private function convertFilter($name, array $configuration) |
84
|
|
|
{ |
85
|
|
|
$filter = Filter::fromNameAndType($name, $configuration['type']); |
86
|
|
|
|
87
|
|
|
if (array_key_exists('label', $configuration)) { |
88
|
|
|
$filter->setLabel($configuration['label']); |
89
|
|
|
} |
90
|
|
|
if (array_key_exists('template', $configuration)) { |
91
|
|
|
$filter->setTemplate($configuration['template']); |
92
|
|
|
} |
93
|
|
|
if (array_key_exists('enabled', $configuration)) { |
94
|
|
|
$filter->setEnabled($configuration['enabled']); |
95
|
|
|
} |
96
|
|
|
if (array_key_exists('position', $configuration)) { |
97
|
|
|
$filter->setPosition($configuration['position']); |
98
|
|
|
} |
99
|
|
|
if (array_key_exists('options', $configuration)) { |
100
|
|
|
$filter->setOptions($configuration['options']); |
101
|
|
|
} |
102
|
|
|
if (array_key_exists('form_options', $configuration)) { |
103
|
|
|
$filter->setFormOptions($configuration['form_options']); |
104
|
|
|
} |
105
|
|
|
if (array_key_exists('default_value', $configuration)) { |
106
|
|
|
$filter->setCriteria($configuration['default_value']); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $filter; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $name |
114
|
|
|
* @param array $configuration |
115
|
|
|
* |
116
|
|
|
* @return ActionGroup |
117
|
|
|
*/ |
118
|
|
|
private function convertActionGroup($name, array $configuration) |
119
|
|
|
{ |
120
|
|
|
$actionGroup = ActionGroup::named($name); |
121
|
|
|
|
122
|
|
|
foreach ($configuration as $actionName => $actionConfiguration) { |
123
|
|
|
$actionGroup->addAction($this->convertAction($actionName, $actionConfiguration)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $actionGroup; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $name |
131
|
|
|
* @param array $configuration |
132
|
|
|
* |
133
|
|
|
* @return Action |
134
|
|
|
*/ |
135
|
|
|
private function convertAction($name, array $configuration) |
136
|
|
|
{ |
137
|
|
|
$action = Action::fromNameAndType($name, $configuration['type']); |
138
|
|
|
|
139
|
|
|
if (array_key_exists('label', $configuration)) { |
140
|
|
|
$action->setLabel($configuration['label']); |
141
|
|
|
} |
142
|
|
|
if (array_key_exists('icon', $configuration)) { |
143
|
|
|
$action->setIcon($configuration['icon']); |
144
|
|
|
} |
145
|
|
|
if (array_key_exists('enabled', $configuration)) { |
146
|
|
|
$action->setEnabled($configuration['enabled']); |
147
|
|
|
} |
148
|
|
|
if (array_key_exists('position', $configuration)) { |
149
|
|
|
$action->setPosition($configuration['position']); |
150
|
|
|
} |
151
|
|
|
if (array_key_exists('options', $configuration)) { |
152
|
|
|
$action->setOptions($configuration['options']); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $action; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|