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
|
|
|
foreach ($config['grids'] as $gridName => $gridConfig) { |
58
|
|
|
$gridConfig = $this->resolveConfig([ |
59
|
|
|
'name' => null, |
60
|
|
|
'query' => null, |
61
|
|
|
'columns' => [], |
62
|
|
|
'filters' => [], |
63
|
|
|
'actions' => [], |
64
|
|
|
'page_size' => 50, |
65
|
|
|
], $gridConfig); |
66
|
|
|
|
67
|
|
|
$columns = []; |
68
|
|
View Code Duplication |
foreach ($gridConfig['columns'] as $columnName => $columnConfig) { |
|
|
|
|
69
|
|
|
$columnConfig = $this->resolveConfig([ |
70
|
|
|
'type' => null, |
71
|
|
|
'options' => [], |
72
|
|
|
], $columnConfig); |
73
|
|
|
|
74
|
|
|
$columns[$columnName] = new ColumnMetadata( |
75
|
|
|
$columnName, |
76
|
|
|
$columnConfig['type'], |
77
|
|
|
$columnConfig['options'] |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$filters = []; |
82
|
|
|
foreach ($gridConfig['filters'] as $filterName => $filterConfig) { |
83
|
|
|
$filterConfig = $this->resolveConfig([ |
84
|
|
|
'type' => null, |
85
|
|
|
'field' => null, |
86
|
|
|
'options' => [], |
87
|
|
|
], $filterConfig); |
88
|
|
|
|
89
|
|
|
$filters[$filterName] = new FilterMetadata( |
90
|
|
|
$filterName, |
91
|
|
|
$filterConfig['type'], |
92
|
|
|
$filterConfig['field'], |
93
|
|
|
$filterConfig['options'] |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$actions = []; |
98
|
|
View Code Duplication |
foreach ($gridConfig['actions'] as $actionName => $actionConfig) { |
|
|
|
|
99
|
|
|
$actionConfig = $this->resolveConfig([ |
100
|
|
|
'type' => null, |
101
|
|
|
'options' => [], |
102
|
|
|
], $actionConfig); |
103
|
|
|
|
104
|
|
|
$actions[$actionName] = new ActionMetadata( |
105
|
|
|
$actionName, |
106
|
|
|
$actionConfig['type'], |
107
|
|
|
$actionConfig['options'] |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$grids[$gridName] = new GridMetadata( |
|
|
|
|
112
|
|
|
$gridName, |
113
|
|
|
$columns, |
114
|
|
|
$filters, |
115
|
|
|
$actions, |
116
|
|
|
$gridConfig['page_size'], |
117
|
|
|
$gridConfig['query'] |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$classMetadata = new ClassMetadata($class->getName(), $grids, $queries); |
|
|
|
|
122
|
|
|
|
123
|
|
|
return $classMetadata; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function getAllClassNames() |
130
|
|
|
{ |
131
|
|
|
return array_keys($this->config); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
private function resolveConfig(array $defaultConfig, array $config) |
135
|
|
|
{ |
136
|
|
View Code Duplication |
if ($diff = array_diff(array_keys($config), array_keys($defaultConfig))) { |
|
|
|
|
137
|
|
|
throw new \InvalidArgumentException(sprintf( |
138
|
|
|
'Invalid configuration keys "%s" for grid, valid keys: "%s"', |
139
|
|
|
implode('", "', $diff), implode('", "', array_keys($defaultConfig)) |
140
|
|
|
)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return array_merge($defaultConfig, $config); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|