-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractRequest.php
More file actions
102 lines (93 loc) · 2.69 KB
/
AbstractRequest.php
File metadata and controls
102 lines (93 loc) · 2.69 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
<?php
namespace SyncEngine\Task\Abstract;
use SyncEngine\Model\TaskModel;
use SyncEngine\Service\ExecuteData;
use SyncEngine\Service\Tag\TagParser;
use SyncEngine\Webservice\Helper\Result;
abstract class AbstractRequest extends TaskModel
{
public function getResponseFields(): array
{
return [
'param' => [
'label' => $this->trans( 'Response param name' ),
'help' => [
$this->trans( 'The param name where the results are located' ),
$this->trans( 'Nested keys are supported: key.nested_key' ),
],
'type' => 'text',
'placeholder' => 'eg. products',
],
'key' => [
'label' => $this->trans( 'Key / Column name override to store results' ),
'help' => [
$this->trans( 'Nested keys are supported: key.nested_key' ),
$this->trans( 'Start with dot (.) for root' ),
],
'type' => 'text', // @todo Column/Key selection field type?
],
'action' => [
'label' => $this->trans( 'Action' ),
'type' => 'select',
'default' => 'replace',
'required' => true,
'choices' => [
'replace' => $this->trans( 'Replace current data with results' ),
'merge' => $this->trans( 'Merge results with current data' ),
'insert' => $this->trans( 'Insert results into current data without replacing existing values' ),
],
],
'action_recursive' => [
'label' => $this->trans( 'Combine recursively?' ),
'type' => 'checkbox',
'conditions' => [ 'action' => [ 'merge', 'insert' ] ],
],
];
}
public function handleResult( ?Result $result, array $config, ExecuteData $data ): ExecuteData
{
$key = $config['key'] ?? null;
$return = [];
if ( $key ) {
$key = ltrim( $key, $data->separator );
if ( empty( $key ) ) {
$key = null;
}
}
if ( $result instanceof Result && $result->isSuccessful() ) {
$return = $result->getData();
$param = $config['param'] ?? null;
// @todo Use resourcedata instead of tags?
if ( ! empty( $param ) || is_numeric( $param ) ) {
$parser = new TagParser( (array) $return );
$return = $parser->parseTag( $param );
}
}
switch ( $config['action'] ?? '' ) {
case 'merge':
if ( empty( $return ) ) {
// @todo Error?
return $data;
}
if ( $key ) {
$return = [ $key => $return ];
}
$data->merge( (array) $return, ! empty( $config['action_recursive'] ) );
break;
case 'insert':
if ( empty( $return ) ) {
// @todo Error?
return $data;
}
if ( $key ) {
$return = [ $key => $return ];
}
$data->insert( (array) $return, ! empty( $config['action_recursive'] ) );
break;
default:
$data->set( $return, $key );
break;
}
return $data;
}
}