-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorageModel.php
More file actions
473 lines (418 loc) · 10.8 KB
/
StorageModel.php
File metadata and controls
473 lines (418 loc) · 10.8 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
<?php
namespace SyncEngine\Model;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use SyncEngine\Entity\Storage;
use SyncEngine\Model\Abstract\EngineModel;
use SyncEngine\Model\Interface\Supervisable;
use SyncEngine\Model\Interface\Taggable;
use SyncEngine\Model\Trait\Data;
use SyncEngine\Model\Trait\Supervisor;
use SyncEngine\Model\Trait\Tags;
use SyncEngine\Service\DataFormatter;
use SyncEngine\Service\SchemaConverter;
use SyncEngine\Structure\Data\MapData;
use SyncEngine\Structure\Data\SchemaData;
/**
* @extends EngineModel<Storage>
*
* @method int getId()
* @method setId( int $id )
* @method string getName()
* @method setName( string $name )
* @method string getDescription()
* @method setDescription( string $description )
* @method string getType()
*/
class StorageModel extends EngineModel implements Taggable, Supervisable
{
use Data {
getData as getDataDefault;
}
use Tags;
use Supervisor;
protected static array $_TYPES = [
'' => 'Generic / Other',
'entities' => 'Entities',
'schema' => 'Schema',
'mapper' => 'Mapper',
'raw' => 'Raw',
];
public function __construct( ?Storage $storage = null )
{
parent::__construct( $storage );
}
public function handleRequest( Request $request ): Response
{
return new Response();
}
public function parseConfig(): void
{
$this->setType( $this->getConfig( 'type', '' ) );
}
public static function getTypes(): array
{
return self::$_TYPES;
}
public static function addType( $type, $label ): void
{
self::$_TYPES[ $type ] = $label;
}
public function isRaw(): bool
{
return 'raw' === $this->getType();
}
public function setType( $type ): void
{
if ( ! array_key_exists( $type, self::$_TYPES ) ) {
return;
}
$this->setConfig( $type, 'type' );
$this->entity->setType( $type );
}
/**
* Get plain data.
*
* @param $key
* @param $default
*
* @return mixed
*/
public function getData( $key = null, $default = null ): mixed
{
if ( $this->isRaw() ) {
return $this->getDataDefault( 'value', $default );
}
return $this->getDataDefault( $key, $default );
}
/**
* Return the storage data as a mapper format.
* The source key is the array key and the target key is the array value.
*
* @param string $sourceKey
* @param string $targetKey
*
* @return MapData
*/
public function getDataMap( string $sourceKey = '', string $targetKey = '' ): MapData
{
// Enforces data existence.
$strictSourceKey = false;
$strictTargetKey = false;
$data = new MapData();
if ( $this->isRaw() ) {
return $data;
}
// Find column names.
if ( ! $sourceKey || ! $targetKey ) {
switch ( $this->getType() ) {
case 'mapper':
$sourceKey = $sourceKey ?: 'source';
$targetKey = $targetKey ?: 'target';
// The mapper type is strict on formatting.
$strictSourceKey = true;
$strictTargetKey = true;
break;
case 'schema':
$config = $this->getConfig( 'schema', [] );
$sourceKey = $sourceKey ?: $config['name_key'] ?? null;
$targetKey = $targetKey ?: $config['label_key'] ?? null;
if ( $sourceKey ) {
$strictSourceKey = true;
}
if ( $targetKey ) {
$strictTargetKey = true;
}
$sourceKey = $sourceKey ?? 'name';
$targetKey = $targetKey ?? 'label';
break;
default:
$columns = $this->getColumns();
if ( ! $sourceKey && ! empty( $columns[0]['key'] ) ) {
$sourceKey = $columns[0]['key'];
}
if ( ! $sourceKey && ! empty( $columns[1]['key'] ) ) {
$targetKey = $columns[1]['key'];
}
break;
}
}
foreach ( $this->getData() as $index => $value ) {
if ( ! is_array( $value ) ) {
if ( $strictSourceKey || $strictTargetKey ) {
continue;
}
$left = $index;
$right = $value;
} else {
$left = $value[ $sourceKey ] ?? null;
$right = $value[ $targetKey ] ?? null;
if ( null === $left ) {
if ( $strictSourceKey ) {
continue;
}
$left = $index;
}
if ( null === $right && $strictTargetKey ) {
continue;
}
}
$data->add( $left, $right );
}
if ( 'entities' === $this->getType() ) {
$schema = $this->getDataSchema();
}
return $data;
}
public function getDataSchema(): ?SchemaData
{
switch ( $this->getType() ) {
case 'schema':
$definitions = $this->getConfig( 'schema.columns' ) ?? [];
return SchemaData::fromDefinitions( $definitions );
case 'mapper':
// A mapper storage will return the output (target) schema.
$target = $this->getConfig( 'mapper.schema.target' );
return static::get( $target )?->getDataSchema() ?? new SchemaData( [] );
}
return null;
}
public function getDataSchemaConverter(): ?SchemaConverter
{
switch ( $this->getType() ) {
case 'schema':
$definitions = $this->getConfig( 'schema.columns' ) ?? [];
return new SchemaConverter( SchemaData::fromDefinitions( $definitions ) );
case 'mapper':
$source = $this->getConfig( 'mapper.schema.source' );
$target = $this->getConfig( 'mapper.schema.target' );
return new SchemaConverter(
static::get( $target )?->getDataSchema() ?? new SchemaData( [] ),
$source ? static::get( $source )?->getDataSchema() : null
);
}
return null;
}
/**
* Return storage data as associative array.
* It will use the data keys as the index for each value.
*
* @param $key
*
* @return array
*/
public function getDataAssociative( $key = '' ): array
{
if ( $this->isRaw() ) {
return [ $key => $this->getData() ];
}
if ( ! $key ) {
switch ( $this->getType() ) {
case 'mapper':
$key = 'source';
break;
case 'schema':
$config = $this->getConfig( 'schema' );
$key = ( $config['name_key'] ?? '' ) ?: 'name';
break;
default:
$key = 'key';
break;
}
}
$data = [];
foreach ( $this->getData() as $index => $value ) {
$data[ $value[ $key ] ?? $index ] = $value;
}
return $data;
}
/**
* Get all keys from the storage data.
* In case of an entity or columns it will return all column keys.
* In case of a mapper it will return all source keys.
* In case of a fields list it will return all field name keys.
*
* @return array
*/
public function getDataKeys(): array
{
if ( $this->isRaw() ) {
return [];
}
switch ( $this->getType() ) {
case 'mapper':
case 'schema':
return $this->getDataMap()->getSources();
case 'entities':
default:
return $this->getColumns( 'key' ) ?: array_keys( $this->getData() ?? [] );
}
}
/**
* Return only the root of all data keys.
* Any traversal data will be removed.
*
* Example: root.depth1.depth2 => root.
*
* @return array
*/
public function getDataRootKeys(): array
{
$keys = $this->getDataKeys();
foreach ( $keys as &$key ) {
$key = explode( '.', (string) $key )[0];
}
return array_unique( $keys );
}
/**
* Return all columns for this storage data.
* Can be filtered by key.
*
* @param $key
*
* @return array[]
*/
public function getColumns( $key = '' ): array
{
$columns = $this->getConfig( 'columns', [] );
if ( 'mapper' === $this->getType() ) {
$columns = [
[
'key' => 'source',
'name' => 'From',
],
[
'key' => 'target',
'name' => 'To',
],
];
}
if ( $columns && $key ) {
foreach ( $columns as &$column ) {
$column = $column[ $key ] ?? '';
}
}
return $columns;
}
public function normalize( $dependencies = false, $dependents = false ): array
{
$normalized = parent::normalize( $dependencies, $dependents );
if ( 'schema' === $this->getType() ) {
$normalized['_schema'] = $this->getDataSchema()?->normalize();
}
return $normalized;
}
public function getFields(): array
{
$choices = [];
foreach ( self::getTypes() as $type => $label ) {
$choices[ $type ] = $this->trans( $label );
}
return [
'type' => [
'label' => $this->trans( 'Data type' ),
'type' => 'select',
'default' => '',
'choices' => $choices,
'fields' => [
'entities' => [
'conditions' => [ 'type' => [ '', 'entities' ] ],
'fields' => [
'schema' => [
'label' => $this->trans( 'Schema' ),
'type' => 'entity',
'entity' => 'storage',
'query' => [ 'where' => [ 'type' => 'schema' ] ],
'actions' => [ 'edit', 'create' ],
],
'columns' => [
'label' => $this->trans( 'Columns' ),
'type' => 'grid',
'name' => 'columns',
'columns' => [
'key' => $this->trans( 'Key' ),
'name' => $this->trans( 'Name' ),
],
],
],
],
'schema' => [
'conditions' => [ 'type' => 'schema' ],
'nested' => [
'name_key' => [
'label' => $this->trans( 'Field name key' ),
'help' => $this->trans(
'By default it will fetch the index key unless the value is an array containing field information.'
),
'type' => 'text',
'placeholder' => 'name',
],
'label_key' => [
'label' => $this->trans( 'Field label key' ),
'help' => $this->trans(
'Used in case the value is an array containing field information.'
),
'type' => 'text',
'placeholder' => 'label',
],
'columns' => [
'label' => $this->trans( 'Column definitions' ),
'description' => $this->trans(
'Configure column types for each field.'
),
'type' => 'schema',
],
],
],
'mapper' => [
'conditions' => [ 'type' => 'mapper' ],
'label' => $this->trans( 'Schema storages' ),
'nested' => [
'schema' => [
'nested' => [
'' => [
'inline' => 'fixed',
'fields' => [
'source' => [
'label' => $this->trans( 'From source schema' ),
'type' => 'entity',
'entity' => 'storage',
'query' => [ 'where' => [ 'type' => 'schema' ] ],
'actions' => [ 'edit', 'create' ],
],
'target' => [
'label' => $this->trans( 'To target schema' ),
'type' => 'entity',
'entity' => 'storage',
'query' => [ 'where' => [ 'type' => 'schema' ] ],
'actions' => [ 'edit', 'create' ],
],
],
],
],
],
],
],
'format' => [
'conditions' => [ 'type' => 'format' ],
'label' => $this->trans( 'Format options' ),
'fields' => [
'format' => ( new DataFormatter() )->getFormatDecodeField(),
],
],
],
],
];
}
public function getTags(): array
{
return [
'config' => [],
'data' => [],
];
}
public static function getEntityClass(): string
{
return Storage::class;
}
}