-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoop.php
More file actions
111 lines (99 loc) · 2.19 KB
/
Loop.php
File metadata and controls
111 lines (99 loc) · 2.19 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
<?php
namespace App\Task;
use App\Model\FlowModel;
use App\Model\StepModel;
use App\Model\TaskModel;
use App\Service\ExecutionContext;
class Loop extends TaskModel
{
public function __construct()
{
$this->type = 'utility';
$this->name = 'Loop';
$this->description = 'Iterate over a set of rows';
parent::__construct();
}
public function getFields(): array
{
return [
'key' => [
'type' => 'text',
'label' => 'Key / Column',
'description' => 'Leave empty for root iteration',
],
'action' => [
'label' => 'Action',
'type' => 'select',
'choices' => [
'flow' => 'Flow',
'step' => 'Step',
'tasks' => 'Tasks',
],
],
'flow' => [
'label' => 'Flow',
'type' => 'entity',
'entity' => 'flow',
'actions' => [ 'edit', 'create' ],
'conditionals' => [
'action' => 'flow',
],
],
'step' => [
'label' => 'Step',
'type' => 'entity',
'entity' => 'step',
'actions' => [ 'edit', 'create' ],
'conditionals' => [
'action' => 'step',
],
],
'tasks' => [
'label' => 'Tasks',
'type' => 'tasks',
'conditionals' => [
'action' => 'tasks',
],
],
];
}
function execute( array $config, ExecutionContext $context, $data )
{
$loop = $data;
$key = $config['key'] ?? '';
if ( $key ) {
$loop = $loop[ $config['key'] ] ?? [];
}
switch ( $config['action'] ?? '' ) {
case 'flow':
$method = 'executeFlow';
$action = FlowModel::get( $config['flow'] );
break;
case 'step':
$method = 'executeStep';
$action = StepModel::get( $config['step'] );
break;
case 'tasks':
$method = 'executeTasks';
$action = $config['tasks'];
break;
default:
$context->addError( 'Invalid action' );
return $data;
}
$service = $context->getExecuteService();
if ( $service && $action ) {
$context->descend();
foreach ( $loop as $index => $value ) {
$loop[ $index ] = $service->$method( $action, $context, $value );
}
$context->ascend();
}
if ( $key ) {
$data[ $key ] = $loop;
} else {
$data = $loop;
}
return $data;
}
}