-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplit.php
More file actions
259 lines (223 loc) · 7.11 KB
/
Split.php
File metadata and controls
259 lines (223 loc) · 7.11 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
<?php
namespace SyncEngine\Task;
use SyncEngine\Model\ColumnModel;
use SyncEngine\Model\TaskModel;
use SyncEngine\Service\ConditionsValidator;
use SyncEngine\Service\ExecuteContext;
use SyncEngine\Service\ExecuteData;
use SyncEngine\Structure\Data\ConfigData;
use SyncEngine\Task\Type\StructureTaskType;
class Split extends TaskModel
{
public function __construct()
{
parent::__construct();
$this->type = StructureTaskType::TYPE;
$this->icon = 'task-split';
$this->name = $this->trans( 'Split' );
$this->description = $this->trans( 'Split value and/or split column key into multiple' );
}
public function getFields(): array
{
return [
'key' => [
'label' => $this->trans( 'Key / Column name' ),
'help' => [
$this->trans( 'The data column name to split' ),
$this->trans( 'Nested keys are supported: {example}', [ 'example' => 'key.nested_key', ] ),
$this->trans( 'Leave empty for root iteration' ),
],
'type' => 'text', // @todo Column/Key selection field type.
'taggable' => true,
],
'action' => [
'label' => $this->trans( 'Action' ),
'type' => 'select',
'default' => 'value',
'choices' => [
'value' => $this->trans( 'Split value' ),
'key' => $this->trans( 'Split into column keys' ),
'both' => $this->trans( 'Split value and split into column keys' ),
],
],
'key_method' => [
'label' => $this->trans( 'Column key split method' ),
'type' => 'select',
'choices' => [
'columns' => $this->trans( 'Split into column keys by providing custom names' ),
'indexed' => $this->trans( 'Split into column keys using an indexed name' ),
],
'conditions' => [
'action' => [ 'key', 'both' ],
],
],
'columns' => [
'label' => $this->trans( 'Column key names' ),
'type' => 'grid',
'columns' => [
'index' => $this->trans( 'Current value index/key (optional)' ),
'key' => $this->trans( 'New column key name' ),
],
'taggable' => true,
'conditions' => [
'action' => [ 'key', 'both' ],
'key_method' => 'columns',
],
],
'index_key' => [
'label' => $this->trans( 'Indexed key' ),
'type' => 'text',
'help' => $this->trans( 'The template for the new indexed keys.' ),
'desc' => $this->trans( 'Wildcards: {wildcards}', [ 'wildcards' => '{*key*} {*index*}' ] ),
// @todo Convert this to Tags (Needs big refactor in Execute service.
'default' => '{*key*}_{*index*}',
'taggable' => true,
'conditions' => [
'action' => [ 'key', 'both' ],
'key_method' => 'indexed',
],
],
'index_start' => [
'label' => $this->trans( 'Index starts with' ),
'type' => 'number',
'placeholder' => '0',
'conditions' => [
'action' => [ 'key', 'both' ],
'key_method' => 'indexed',
],
],
'remove' => [
'label' => $this->trans( 'Remove original key(s)?' ),
'type' => 'switch',
'conditions' => [
'action' => [ 'key', 'both' ],
],
],
'separator' => [
'label' => $this->trans( 'Separator' ),
'type' => 'select',
'choices' => [
',' => $this->trans( 'Comma' ) . ' (,)',
';' => $this->trans( 'Semicolon' ) . ' (;)',
' ' => $this->trans( 'Space' ),
'{*tab*}' => $this->trans( 'Tab' ),
'{*nl*}' => $this->trans( 'New line' ) . ' (\n)',
'{*regex*}' => $this->trans( 'Regular expression (Regex)' ),
],
'customizable' => true,
'conditions' => [
'action' => [ 'value', 'both' ],
],
'fields' => [
'regex_split' => [
'label' => $this->trans( 'Regular expression' ),
'description' => $this->trans( 'Regex must be made for splitting values, not matching.' ) . ' `preg_split()`',
'type' => 'text',
'conditions' => [
'action' => [ 'value', 'both' ],
'separator' => '{*regex*}',
],
],
],
],
'column' => [
'label' => $this->trans( 'Split value column definition' ),
'type' => 'column',
'conditions' => [
'action' => [ 'value', 'both' ],
],
],
];
}
public function execute( ConfigData $config, ExecuteContext $context, ExecuteData $data ): ExecuteData
{
if ( empty( $config['key'] ) ) {
$context->addError( $this->trans( 'No key configured' ) );
return $data;
}
if ( empty( $config['action'] ) ) {
$context->addError( $this->trans( 'No action configured' ) );
return $data;
}
$key = $config['key'];
$value = $data->get( $key );
if ( ConditionsValidator::isEmptyValue( $value ) ) {
return $data->set( [], $key );
}
$action = $config['action'];
if ( is_array( $value ) && str_contains( $key, '[]' ) ) {
/*foreach ( $value as $index => $val ) {
// @todo Support loop structure.
}*/
$context->addError( $this->trans( 'Loop key not supported' ) );
return $data;
}
if ( 'value' === $action || 'both' === $action ) {
if ( ! is_string( $value ) ) {
$context->addError( $this->trans( 'Value is not splittable into list' ) );
return $data;
}
$separator = match ( $config['separator'] ?? '' ) {
'{*nl*}' => "\n",
'{*tab*}' => " ",
default => $config['separator'] ?? '',
};
if ( '{*regex*}' === $separator ) {
if ( empty( $config['regex_split'] ) ) {
$context->addError( 'Empty regular expression' );
return $data;
}
$value = preg_split( $config['regex_split'], $value );
} else {
$value = explode( $separator, $value );
}
if ( ! empty( $config['column']['_class'] ) ) {
$column = ColumnModel::get( $config['column']['_class'] );
foreach ( $value as $index => $val ) {
$value[ $index ] = $column->format( $val, $config['column'] );
}
}
}
if ( 'key' === $action || 'both' === $action ) {
if ( ! is_array( $value ) ) {
$context->addError( $this->trans( 'Value is not splittable into keys' ) );
return $data;
}
if ( ! empty( $config['remove'] ) ) {
$data->unset( $key );
}
switch ( $config['key_method'] ?? '' ) {
case 'indexed':
$indexed = $config['index_key'] ?? '{*key*}_{*index*}';
$indexed = str_replace( '{*key*}', $key, $indexed );
$start = (int) ( $config['index_start'] ?? 0 );
for ( $i = $start, $num = $start + count( $value ); $i < $num; $i ++ ) {
$index_key = str_replace( '{*index*}', $i, $indexed );
$data->set( $value[ $i - $start ], $index_key );
}
break;
case 'columns':
if ( empty( $config['columns'] ) ) {
$context->addError( $this->trans( 'No columns configured' ) );
return $data;
}
$columns = $config['columns'];
for ( $i = 0, $count = count( $columns ); $i < $count; $i ++ ) {
$column = $columns[ $i ];
if ( ! isset( $column['key'] ) ) {
continue;
}
$name = $column['key'];
$index = $column['index'] ?? $i;
if ( isset( $value[ $index ] ) ) {
$data->set( $value[ $index ], $name );
}
}
break;
}
} else {
$data->set( $value, $key );
}
return $data;
}
}