-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRfcReadTable.php
199 lines (180 loc) · 3.96 KB
/
RfcReadTable.php
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
<?php
namespace SapPhp\Functions\Table;
use Closure;
use sapnwrfc;
use Carbon\Carbon;
use SapPhp\FunctionModule;
class RfcReadTable extends FunctionModule
{
/**
* sapnwrfc_function Paramters
*
* @var array
*/
public $parameters = [
'DELIMITER' => '§',
'QUERY_TABLE' => '',
'FIELDS' => [],
'OPTIONS' => [],
];
/**
* QueryBuilders
*
* @var QueryBuilder
*/
public $query;
/**
* Create a new instance of RfcReadTable.
*
* @param sapnwrfc $handle
*
* @return void
*/
public function __construct(sapnwrfc $handle)
{
$this->query = new QueryBuilder($this);
$this->parser = function($result) {
return $this->parse($result);
};
parent::__construct($handle, 'RFC_READ_TABLE');
}
/**
* Delimiter used by SAP to concatenate table rows
*
* @param string $value
*
* @return $this
*/
public function delimiter($value)
{
return $this->param('delimiter', $value);
}
/**
* Return query fields array.
*
* @param array $fields
*
* @return array
*/
public function fields($fields) {
foreach ($fields as $key => $field) {
$fields[] = ['FIELDNAME' => strtoupper($field)];
unset($fields[$key]);
}
return $fields;
}
/**
* Set fields for retrieval and execute function. Keep in mind this value is limited to
* 512 bytes per row.
*
* @param array $fields
*
* @return Collection
*/
public function get($fields = [])
{
$this->param('fields', $this->fields($fields));
$this->param('options', $this->query->options());
return $this->invoke();
}
/**
* Limit table rows to provided number.
*
* @param int $number
*
* @return $this
*/
public function limit($number)
{
return $this->param('rowcount', (int)$number);
}
/**
* Skip provided number of rows from the result.
*
* @param int $number
*
* @return $this
*/
public function offset($number)
{
return $this->param('rowskips', (int)$number);
}
/**
* Set table to be queried.
*
* @param string $name
*
* @return $this
*/
public function table($name)
{
return $this->param('query_table', strtoupper($name));
}
/**
* Parse output from SAP and transform to Collection
*
* @param array $result
*
* @return Collection
*/
public function parse($result)
{
// Clear all that spaces.
$result = array_trim($result);
// Get DATA and FIELDS SAP tables.
$data = collect($result['DATA']);
$fields = collect($result['FIELDS']);
// Get columns.
$columns = $fields->pluck('FIELDNAME')->toArray();
// If no raw rows early exit.
if ($data->count() === 0) {
return collect();
}
// Explode raw data rows and combine with columns.
$table = $data->pluck('WA')->transform(function($item) use ($columns)
{
$values = array_trim(explode($this->parameters['DELIMITER'], $item));
if (count($values) != count($columns)) {
eval(\Psy\sh());
}
return array_combine($columns, $values);
});
// Apply transformations in corelation with fields type.
$fields->each(function ($field) use ($table) {
// Transform dates.
if ($field['TYPE'] === 'D') {
$table->transform(function ($row) use($field) {
if ($row[$field['FIELDNAME']] == '00000000') {
$row[$field['FIELDNAME']] = null;
} else {
try {
$row[$field['FIELDNAME']] = Carbon::createFromFormat('Ymd', $row[$field['FIELDNAME']]);
} catch (\InvalidArgumentException $e) {
// Do nothing. Invalid format.
}
}
return $row;
});
}
});
return $table;
}
/**
* Dynamically handle calls to object methods.
*
* @param string $method
* @param array $arguments
*
* @return mixed
*/
public function __call($method, $arguments)
{
if (method_exists($this, $method)) {
return $this->{$method}(...$arguments);
} elseif (method_exists($this->query, $method)) {
return $this->query->{$method}(...$arguments);
} else {
trigger_error("Call to undefined method ". get_class($this) ."::$method()", E_USER_ERROR);
}
}
}