-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge.php
More file actions
86 lines (78 loc) · 1.88 KB
/
Merge.php
File metadata and controls
86 lines (78 loc) · 1.88 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
<?php
namespace App\Task;
use App\Model\TaskModel;
use App\Service\ExecutionContext;
class Merge extends TaskModel
{
public function __construct()
{
$this->type = 'structure';
$this->name = 'Merge';
$this->description = 'Merge columns into single column';
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' => 'Merge key value',
'indexed' => 'Merge columns with key as prefix ending with an index (key#)',
],
],
'separator' => [
'label' => 'Separator',
'type' => 'separator',
],
'postfix' => [
'label' => 'Postfix',
'type' => 'text',
'default' => '__',
'conditionals' => [
'action' => 'indexed',
],
],
'remove' => [
'label' => 'Remove merged items?',
'type' => 'checkbox',
'conditionals' => [
'action' => 'indexed',
],
],
];
}
function execute( array $config, ExecutionContext $context, $data )
{
$key = $config['key'];
$values = [];
switch ( $config['action'] ?? '' ) {
case 'indexed':
$search = $key . ( $config['postfix'] ?? '' );
$i = 0;
while ( isset( $data[ $search . $i ] ) ) {
$values[] = $data[ $search . $i ];
if ( ! empty( $config['remove'] ) ) {
unset( $data[ $search . $i ] );
}
}
$data[ $key ] = implode( $config['separator'] ?? '', $values );
break;
case 'value':
if ( isset( $data[ $key ] ) && is_array( $data[ $key ] ) ) {
$data[ $key ] = implode( $config['separator'] ?? '', $data[ $key ] );
}
break;
default:
$context->addError( 'Invalid action' );
break;
}
return $data;
}
}