-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoutineModel.php
More file actions
243 lines (216 loc) · 5.9 KB
/
RoutineModel.php
File metadata and controls
243 lines (216 loc) · 5.9 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
<?php
namespace SyncEngine\Model;
use SyncEngine\Entity\Routine;
use SyncEngine\Model\Abstract\EngineModel;
use SyncEngine\Model\Interface\Supervisable;
use SyncEngine\Model\Interface\Taggable;
use SyncEngine\Model\Trait\Supervisor;
use SyncEngine\Model\Trait\Tags;
use SyncEngine\Structure\Data\SchemaData;
/**
* @extends EngineModel<Routine>
*
* @method int getId()
* @method setId( int $id )
* @method string getName()
* @method setName( string $name )
* @method string getDescription()
* @method setDescription( string $description )
*/
class RoutineModel extends EngineModel implements Taggable, Supervisable
{
use Tags;
use Supervisor;
public function __construct( ?Routine $routine = null )
{
parent::__construct( $routine );
}
/**
* @return TaskModel[]
*/
public function getTasks(): array
{
$tasks = [];
foreach ( $this->getConfig( 'tasks' ) ?? [] as $task ) {
$tasks[] = TaskModel::create( $task['_class'] ?? '' );
}
return $tasks;
}
public function getTask( $ref ): TaskModel|null
{
foreach ( $this->getConfig( 'tasks' ) ?? [] as $task ) {
if ( $task['_ref'] === $ref ) {
return TaskModel::create( $task['_class'] ?? '' );
}
}
return null;
}
public function getVariableSchema(): SchemaData
{
return SchemaData::fromDefinitions( $this->getConfig( 'schema.variables', [] ) );
}
public function getInputSchema(): SchemaData
{
$storage = $this->getConfig( 'schema.input.storage' );
$manual = $this->getConfig( 'schema.input.manual' );
$schema = $storage ? SchemaData::fromStorage( $storage ) : new SchemaData();
if ( $manual ) {
$schema->merge( SchemaData::fromDefinitions( $manual ) );
}
return $schema;
}
public function getOutputSchema(): SchemaData
{
$storage = $this->getConfig( 'schema.output.storage' );
$manual = $this->getConfig( 'schema.output.manual' );
$schema = $storage ? SchemaData::fromStorage( $storage ) : new SchemaData();
if ( $manual ) {
$schema->merge( SchemaData::fromDefinitions( $manual ) );
}
return $schema;
}
public function normalize( $dependencies = false, $dependents = false ): array
{
$data = parent::normalize( $dependencies, $dependents );
$variablesSchema = $this->getVariableSchema();
$inputSchema = $this->getInputSchema();
$outputSchema = $this->getOutputSchema();
$data['_schema'] = [
'variables' => $variablesSchema->getSchema(),
'input' => $inputSchema->getSchema(),
'output' => $outputSchema->getSchema(),
];
$inputFields = $inputSchema->getFields();
$variablesFields = $variablesSchema->getFields();
$data['_step'] = [
'fields' => [],
'tags' => $outputSchema->getTags(),
];
if ( count( $inputFields ) ) {
$data['_step']['fields']['input'] = [
'label' => [
'icon' => 'input',
'text' => $this->trans( 'Input data' ),
],
'nested' => $inputFields->generateLabels()->bulkEdit( [ 'taggable' => true ] ),
];
}
if ( count( $variablesFields ) ) {
$data['_step']['fields']['variables'] = [
'label' => [
'icon' => 'variable',
'text' => $this->trans( 'Input variables' ),
],
'nested' => $variablesFields->generateLabels()->bulkEdit( [ 'taggable' => true ] ),
];
}
if ( 1 < count( $data['_step']['fields'] ) ) {
$data['_step']['fields'] = [
'_' => [
'tabs' => $data['_step']['fields'],
],
];
}
return $data;
}
public function getFields(): array
{
// @todo Implement fields.
return [
'_' => [
'tabs' => [
'tasks' => [
'required' => true,
'label' => [
'icon' => 'task',
'text' => $this->trans( 'Tasks' ),
],
'type' => 'tasks',
],
'conditions' => [
'indicator' => true,
'label' => [
'icon' => 'condition',
'text' => $this->trans( 'Conditions' ),
],
'type' => 'conditions',
'taggable' => true,
'source' => [
'customizable' => true,
'choices' => [
'' => 'Data',
'{{ cache }}' => 'Cache',
'{{ variables }}' => 'Variables',
],
],
],
'schema' => [
'indicator' => true,
'label' => [
'icon' => 'schema',
'text' => $this->trans( 'Schema' ),
],
'nested' => [
'variables' => [
'label' => [
'icon' => 'variable',
'text' => $this->trans( 'Input variables' ),
],
'type' => 'schema',
],
'input' => [
'label' => [
'icon' => 'input',
'text' => $this->trans( 'Input schema' ),
],
'collapsed' => true,
'nested' => [
'storage' => [
'label' => $this->trans( 'Predefined schema storage' ),
'type' => 'entity',
'entity' => 'storage',
'query' => [ 'where' => [ 'type' => 'schema' ] ],
'actions' => [ 'edit', 'create' ],
],
'manual' => [
'label' => $this->trans( 'Manual definitions' ),
'description' => $this->trans(
'Configure column types for each field.'
),
'type' => 'schema',
],
],
],
'output' => [
'label' => [
'icon' => 'output',
'text' => $this->trans( 'Output schema' ),
],
'nested' => [
'storage' => [
'label' => $this->trans( 'Predefined schema storage' ),
'type' => 'entity',
'entity' => 'storage',
'query' => [ 'where' => [ 'type' => 'schema' ] ],
'actions' => [ 'edit', 'create' ],
],
'manual' => [
'label' => $this->trans( 'Manual definitions' ),
'description' => $this->trans(
'Configure column types for each field.'
),
'type' => 'schema',
],
],
],
],
],
],
],
];
}
public static function getEntityClass(): string
{
return Routine::class;
}
}