-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplit.php
More file actions
95 lines (82 loc) · 1.93 KB
/
Split.php
File metadata and controls
95 lines (82 loc) · 1.93 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
<?php
namespace App\Task;
use App\Model\TaskModel;
use App\Service\ExecutionContext;
class Split extends TaskModel
{
public function __construct()
{
$this->type = 'structure';
$this->name = 'Split';
$this->description = 'Split column into multiple columns';
parent::__construct();
}
public function getFields(): array
{
return [
'key' => [
'label' => 'Key',
'type' => 'text', // @todo Column/Key selection field type.
],
'action' => [
'label' => 'Action',
'type' => 'select',
'default' => 'value',
'choices' => [
'value' => 'Split key value',
'indexed' => 'Split value into several columns using the key and an index postfix (key#)',
],
],
'separator' => [
'label' => 'Separator',
'type' => 'separator',
],
'postfix' => [
'label' => 'Postfix',
'type' => 'text',
'default' => '__',
'conditionals' => [
'action' => 'prefixed',
],
],
'remove' => [
'label' => 'Remove merged items?',
'type' => 'checkbox',
'conditionals' => [
'action' => 'prefixed',
],
],
];
}
function execute( array $config, ExecutionContext $context, $data )
{
$key = $config['key'];
$field = $data[ $key ] ?? null;
if ( empty( $field ) ) {
return $data;
}
if ( empty( $config['separator'] ) ) {
// @todo error?
return $data;
}
switch ( $config['action'] ?? '' ) {
case 'indexed':
$prefix = $key . ( $config['postfix'] ?? '' );
$split = explode( $config['separator'], $field );
for ( $i = 0; $i < count( $split ); $i ++ ) {
$data[ $prefix . $i ] = $split[ $i ];
}
if ( ! empty( $config['remove'] ) ) {
unset( $data[ $key ] );
}
break;
case 'value':
$data[ $key ] = explode( $config['separator'], $field );
break;
default:
$context->addError( 'Invalid action' );
break;
}
return $data;
}
}