1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Psi\Component\Grid\Metadata\Driver; |
6
|
|
|
|
7
|
|
|
use Metadata\Driver\AdvancedDriverInterface; |
8
|
|
|
use Psi\Component\Grid\Metadata\ActionMetadata; |
9
|
|
|
use Psi\Component\Grid\Metadata\ClassMetadata; |
10
|
|
|
use Psi\Component\Grid\Metadata\ColumnMetadata; |
11
|
|
|
use Psi\Component\Grid\Metadata\FilterMetadata; |
12
|
|
|
use Psi\Component\Grid\Metadata\GridMetadata; |
13
|
|
|
use Psi\Component\Grid\Metadata\QueryMetadata; |
14
|
|
|
|
15
|
|
|
class ArrayDriver implements AdvancedDriverInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $config; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param array $config |
24
|
|
|
*/ |
25
|
|
|
public function __construct(array $config) |
26
|
|
|
{ |
27
|
|
|
$this->config = $config; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function loadMetadataForClass(\ReflectionClass $class) |
34
|
|
|
{ |
35
|
|
|
if (!isset($this->config[$class->getName()])) { |
|
|
|
|
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$config = $this->resolveConfig([ |
40
|
|
|
'grids' => [], |
41
|
|
|
'queries' => [], |
42
|
|
|
], $this->config[$class->getName()]); |
|
|
|
|
43
|
|
|
|
44
|
|
|
$queries = []; |
45
|
|
|
foreach ($config['queries'] as $queryName => $queryConfig) { |
46
|
|
|
$queryConfig = $this->resolveConfig([ |
47
|
|
|
'name' => null, |
48
|
|
|
'selects' => [], |
49
|
|
|
'joins' => [], |
50
|
|
|
'criteria' => [], |
51
|
|
|
], $queryConfig); |
52
|
|
|
$queries[$queryName] = new QueryMetadata( |
53
|
|
|
$queryName, $queryConfig['selects'], $queryConfig['joins'], $queryConfig['criteria'] |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$grids = []; |
58
|
|
|
foreach ($config['grids'] as $gridName => $gridConfig) { |
59
|
|
|
$gridConfig = $this->resolveConfig([ |
60
|
|
|
'name' => null, |
61
|
|
|
'query' => null, |
62
|
|
|
'columns' => [], |
63
|
|
|
'filters' => [], |
64
|
|
|
'actions' => [], |
65
|
|
|
'page_size' => 50, |
66
|
|
|
], $gridConfig); |
67
|
|
|
|
68
|
|
|
$columns = []; |
69
|
|
View Code Duplication |
foreach ($gridConfig['columns'] as $columnName => $columnConfig) { |
|
|
|
|
70
|
|
|
$columnConfig = $this->resolveConfig([ |
71
|
|
|
'type' => null, |
72
|
|
|
'tags' => [], |
73
|
|
|
'groups' => [], |
74
|
|
|
'options' => [], |
75
|
|
|
], $columnConfig); |
76
|
|
|
|
77
|
|
|
$columns[$columnName] = new ColumnMetadata( |
78
|
|
|
$columnName, |
79
|
|
|
$columnConfig['type'], |
80
|
|
|
$columnConfig['groups'], |
81
|
|
|
$columnConfig['options'], |
82
|
|
|
$columnConfig['tags'] |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$filters = []; |
87
|
|
View Code Duplication |
foreach ($gridConfig['filters'] as $filterName => $filterConfig) { |
|
|
|
|
88
|
|
|
$filterConfig = $this->resolveConfig([ |
89
|
|
|
'type' => null, |
90
|
|
|
'field' => null, |
91
|
|
|
'options' => [], |
92
|
|
|
'tags' => [], |
93
|
|
|
], $filterConfig); |
94
|
|
|
|
95
|
|
|
$filters[$filterName] = new FilterMetadata( |
96
|
|
|
$filterName, |
97
|
|
|
$filterConfig['type'], |
98
|
|
|
$filterConfig['field'], |
99
|
|
|
$filterConfig['options'], |
100
|
|
|
$filterConfig['tags'] |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$actions = []; |
105
|
|
|
foreach ($gridConfig['actions'] as $actionName => $actionConfig) { |
106
|
|
|
$actionConfig = $this->resolveConfig([ |
107
|
|
|
'type' => null, |
108
|
|
|
'options' => [], |
109
|
|
|
'tags' => [], |
110
|
|
|
], $actionConfig); |
111
|
|
|
|
112
|
|
|
$actions[$actionName] = new ActionMetadata( |
113
|
|
|
$actionName, |
114
|
|
|
$actionConfig['type'], |
115
|
|
|
$actionConfig['options'], |
116
|
|
|
$actionConfig['tags'] |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$grids[$gridName] = new GridMetadata( |
121
|
|
|
$gridName, |
122
|
|
|
$columns, |
123
|
|
|
$filters, |
124
|
|
|
$actions, |
125
|
|
|
$gridConfig['page_size'], |
126
|
|
|
$gridConfig['query'] |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$classMetadata = new ClassMetadata($class->getName(), $grids, $queries); |
|
|
|
|
131
|
|
|
|
132
|
|
|
return $classMetadata; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
public function getAllClassNames() |
139
|
|
|
{ |
140
|
|
|
return array_keys($this->config); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
private function resolveConfig(array $defaultConfig, array $config) |
144
|
|
|
{ |
145
|
|
View Code Duplication |
if ($diff = array_diff(array_keys($config), array_keys($defaultConfig))) { |
|
|
|
|
146
|
|
|
throw new \InvalidArgumentException(sprintf( |
147
|
|
|
'Invalid configuration keys "%s" for grid, valid keys: "%s"', |
148
|
|
|
implode('", "', $diff), implode('", "', array_keys($defaultConfig)) |
149
|
|
|
)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return array_merge($defaultConfig, $config); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|