-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectMetadata.php
More file actions
356 lines (287 loc) · 8.33 KB
/
Copy pathObjectMetadata.php
File metadata and controls
356 lines (287 loc) · 8.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
/*
* This file is part of the Klipper package.
*
* (c) François Pluchino <francois.pluchino@klipper.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Klipper\Component\Metadata;
use Klipper\Component\Metadata\Exception\ActionMetadataNotFoundException;
use Klipper\Component\Metadata\Exception\AssociationMetadataNotFoundException;
use Klipper\Component\Metadata\Exception\FieldMetadataNotFoundException;
use Symfony\Component\Config\Resource\ResourceInterface;
/**
* @author François Pluchino <francois.pluchino@klipper.dev>
*/
class ObjectMetadata extends BaseMetadata implements ObjectMetadataInterface
{
public const SORTABLE_DIRECTION = ['asc', 'desc'];
protected string $pluralName;
protected string $pluralLabel;
protected string $class;
/**
* @var FieldMetadataInterface[]
*/
protected array $fields = [];
/**
* @var string[]
*/
protected array $fieldNames = [];
/**
* @var AssociationMetadataInterface[]
*/
protected array $associations = [];
/**
* @var string[]
*/
protected array $associationNames = [];
protected bool $sortable = false;
protected bool $multiSortable;
protected array $defaultSortable;
protected bool $filterable = false;
protected bool $searchable = false;
protected bool $translatable = false;
/**
* @var string[]
*/
protected array $availableContexts;
protected string $fieldIdentifier;
protected string $fieldLabel;
/**
* @var string[]
*/
protected array $deepSearchPaths = [];
/**
* @var ActionMetadataInterface[]
*/
protected array $actions = [];
/**
* @var ResourceInterface[]
*/
protected array $resources = [];
public function __construct(
string $class,
string $name,
string $pluralName,
string $label,
string $pluralLabel,
?string $description,
?string $translationDomain,
bool $public,
bool $multiSortable,
array $defaultSortable,
array $availableContexts,
string $fieldIdentifier,
string $fieldLabel,
?string $formType,
array $formOptions,
array $deepSearchPaths,
array $groups,
array $resources,
array $fieldBuilders,
array $associationBuilders,
array $actionBuilders
) {
parent::__construct(
$name,
'object',
$label,
$description,
$translationDomain,
$public
);
$this->class = $class;
$this->pluralName = $pluralName;
$this->pluralLabel = $pluralLabel;
$this->multiSortable = $multiSortable;
$this->defaultSortable = $defaultSortable;
$this->availableContexts = $availableContexts;
$this->fieldIdentifier = $fieldIdentifier;
$this->fieldLabel = $fieldLabel;
$this->formType = $formType;
$this->formOptions = $formOptions;
$this->deepSearchPaths = $deepSearchPaths;
$this->groups = $groups;
$this->resources = $resources;
$this->searchable = !empty($deepSearchPaths);
foreach ($fieldBuilders as $fieldBuilder) {
$this->addField($fieldBuilder);
}
foreach ($associationBuilders as $associationBuilder) {
$this->addAssociation($associationBuilder);
}
foreach ($actionBuilders as $actionBuilder) {
$this->addAction($actionBuilder);
}
}
public function getPluralName(): string
{
return $this->pluralName;
}
public function getPluralLabel(): string
{
return $this->pluralLabel;
}
public function getClass(): string
{
return $this->class;
}
public function getFields(): array
{
return $this->fields;
}
public function hasField(string $name): bool
{
return isset($this->fields[$name]);
}
public function hasFieldByName(string $name): bool
{
return isset($this->fieldNames[$name]);
}
public function getField(string $name): FieldMetadataInterface
{
if (!$this->hasField($name)) {
throw new FieldMetadataNotFoundException($name);
}
return $this->fields[$name];
}
public function getFieldByName(string $name): FieldMetadataInterface
{
$name = $this->fieldNames[$name] ?? $name;
return $this->getField($name);
}
public function getAssociations(): array
{
return $this->associations;
}
public function hasAssociation(string $name): bool
{
return isset($this->associations[$name]);
}
public function hasAssociationByName(string $name): bool
{
return isset($this->associationNames[$name]);
}
public function getAssociation(string $name): AssociationMetadataInterface
{
if (!$this->hasAssociation($name)) {
throw new AssociationMetadataNotFoundException($name);
}
return $this->associations[$name];
}
public function getAssociationByName(string $name): AssociationMetadataInterface
{
$name = $this->associationNames[$name] ?? $name;
return $this->getAssociation($name);
}
public function isSortable(): bool
{
return $this->sortable;
}
public function isMultiSortable(): bool
{
return $this->multiSortable;
}
public function getDefaultSortable(): array
{
return $this->defaultSortable;
}
public function isFilterable(): bool
{
return $this->filterable;
}
public function isSearchable(): bool
{
return $this->searchable;
}
public function getAvailableContexts(): array
{
return $this->availableContexts;
}
public function getFieldIdentifier(): string
{
return $this->fieldIdentifier;
}
public function getFieldLabel(): string
{
return $this->fieldLabel;
}
public function getDeepSearchPaths(): array
{
return $this->deepSearchPaths;
}
public function getActions(): array
{
return $this->actions;
}
public function hasAction(string $action): bool
{
return isset($this->actions[$action]);
}
public function getAction(string $action): ActionMetadataInterface
{
if (!$this->hasAction($action)) {
throw new ActionMetadataNotFoundException($action);
}
return $this->actions[$action];
}
public function getResources(): array
{
return $this->resources;
}
/**
* Add the field metadata builder.
*
* @param FieldMetadataBuilderInterface $builder The field metadata builder
*
* @return static
*/
private function addField(FieldMetadataBuilderInterface $builder): self
{
$metadata = $builder->build($this);
$this->fields[$metadata->getField()] = $metadata;
$this->fieldNames[$metadata->getName()] = $metadata->getField();
if ($metadata->isSortable()) {
$this->sortable = true;
}
if ($metadata->isFilterable()) {
$this->filterable = true;
}
if ($metadata->isSearchable()) {
$this->searchable = true;
}
if ($metadata->isTranslatable()) {
$this->translatable = true;
}
return $this;
}
/**
* Add the association metadata builder.
*
* @param AssociationMetadataBuilderInterface $builder The association metadata builder
*
* @return static
*/
private function addAssociation(AssociationMetadataBuilderInterface $builder): self
{
$metadata = $builder->build($this);
$this->associations[$metadata->getAssociation()] = $metadata;
$this->associationNames[$metadata->getName()] = $metadata->getAssociation();
return $this;
}
/**
* Add the action metadata builder.
*
* @param ActionMetadataBuilderInterface $builder The action metadata builder
*
* @return static
*/
private function addAction(ActionMetadataBuilderInterface $builder): self
{
$action = $builder->build($this);
$this->actions[$action->getName()] = $action;
return $this;
}
}