-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtract.php
More file actions
105 lines (90 loc) · 2.94 KB
/
Extract.php
File metadata and controls
105 lines (90 loc) · 2.94 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
<?php
namespace SyncEngine\Task;
use SyncEngine\Model\TaskModel;
use SyncEngine\Service\ConditionsValidator;
use SyncEngine\Service\ExecuteContext;
use SyncEngine\Service\ExecuteData;
use SyncEngine\Structure\Data\ConfigData;
use SyncEngine\Structure\Data\ResourceData;
use SyncEngine\Task\Type\StructureTaskType;
class Extract extends TaskModel
{
public function __construct()
{
parent::__construct();
$this->type = StructureTaskType::TYPE;
$this->icon = 'task-extract';
$this->name = $this->trans( 'Extract' );
$this->description = $this->trans( 'Extract a column from each item in your data' );
}
public function getFields(): array
{
return [
'key' => [
'label' => $this->trans( 'Key / Column name' ),
'type' => 'text', // @todo Column/Key selection field type.
'help' => [
$this->trans( 'The data column name to extract items from' ),
$this->trans( 'Nested keys are supported: key.nested_key' ),
$this->trans( 'Leave empty for root' ),
],
'taggable' => true,
],
'column_key' => [
'label' => $this->trans( 'The column to extract' ),
'type' => 'text',
'help' => [
$this->trans( 'Nested keys are supported: key.nested_key' ),
$this->trans( 'Leave empty for root' ),
],
'default' => '',
'taggable' => true,
],
'target_key' => [
'label' => $this->trans( 'The target column to put results' ),
'description' => $this->trans( 'Leave empty to use the Key / Column name set above' ),
'type' => 'text',
'help' => [
$this->trans( 'The data column name to extract items from' ),
$this->trans( 'Nested keys are supported: key.nested_key' ),
$this->trans( 'Start with dot (.) for root' ),
],
'default' => '',
'taggable' => true,
],
];
}
public function execute( ConfigData $config, ExecuteContext $context, ExecuteData $data ): ExecuteData
{
$column_key = $config['column_key'] ?? null;
if ( empty( $config['column_key'] ) ) {
$context->addError( $this->trans( 'No column key configured' ) );
return $data;
}
$key = $config['key'] ?? null;
$target = $config['target_key'] ?? null;
if ( ConditionsValidator::isEmptyValue( $target ) ) {
$target = $key;
}
if ( is_string( $target ) ) {
$target = ltrim( $target, '.' );
}
$items = $data->get( $key );
if ( ! is_iterable( $items ) ) {
$context->addLog( $this->trans( 'Data not iterable' ) );
return $data;
}
$extracted = [];
foreach ( $items as $index => $value ) {
if ( ! is_iterable( $value ) ) {
$context->addLog( $this->trans( 'Could not extract' ), [ 'config' => $config, 'data' => $value ] );
// @todo define default value or other method to handle invalid items?
$extracted[ $index ] = null;
} else {
$extracted[ $index ] = ResourceData::create( $value )->get( $column_key );
}
}
$data->set( $extracted, $target );
return $data;
}
}