-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormDataProcessor.php
More file actions
240 lines (195 loc) · 6.3 KB
/
FormDataProcessor.php
File metadata and controls
240 lines (195 loc) · 6.3 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
* FormDataProcessor
* PHP version 8.1
*
* @category Class
* @package Aternos\SpigotApi
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/
/**
* SpigotMC Xenforo Resource Manager API
*
* Exposes resource/author information via a simple JSON REST API
*
* The version of the OpenAPI document: 0.2
* @generated Generated by: https://openapi-generator.tech
* Generator version: 7.20.0
*/
/**
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
namespace Aternos\SpigotApi;
use ArrayAccess;
use DateTime;
use GuzzleHttp\Psr7\Utils;
use Psr\Http\Message\StreamInterface;
use SplFileObject;
use Aternos\SpigotApi\Model\ModelInterface;
class FormDataProcessor
{
/**
* Tags whether payload passed to ::prepare() contains one or more
* SplFileObject or stream values.
*/
public bool $has_file = false;
/**
* Take value and turn it into an array suitable for inclusion in
* the http body (form parameter). If it's a string, pass through unchanged
* If it's a datetime object, format it in ISO8601
*
* @param array<string|bool|array|DateTime|ArrayAccess|SplFileObject> $values the value of the form parameter
*
* @return array [key => value] of formdata
*/
public function prepare(array $values): array
{
$this->has_file = false;
$result = [];
foreach ($values as $k => $v) {
if ($v === null) {
continue;
}
$result[$k] = $this->makeFormSafe($v);
}
return $result;
}
/**
* Flattens a multi-level array of data and generates a single-level array
* compatible with formdata - a single-level array where the keys use bracket
* notation to signify nested data.
*
* credit: https://github.com/FranBar1966/FlatPHP
*/
public static function flatten(array $source, string $start = ''): array
{
$opt = [
'prefix' => '[',
'suffix' => ']',
'suffix-end' => true,
'prefix-list' => '[',
'suffix-list' => ']',
'suffix-list-end' => true,
];
if ($start === '') {
$currentPrefix = '';
$currentSuffix = '';
$currentSuffixEnd = false;
} elseif (array_is_list($source)) {
$currentPrefix = $opt['prefix-list'];
$currentSuffix = $opt['suffix-list'];
$currentSuffixEnd = $opt['suffix-list-end'];
} else {
$currentPrefix = $opt['prefix'];
$currentSuffix = $opt['suffix'];
$currentSuffixEnd = $opt['suffix-end'];
}
$currentName = $start;
$result = [];
foreach ($source as $key => $val) {
$currentName .= $currentPrefix . $key;
if (is_array($val) && !empty($val)) {
$currentName .= $currentSuffix;
$result += self::flatten($val, $currentName);
} else {
if ($currentSuffixEnd) {
$currentName .= $currentSuffix;
}
if (is_resource($val)) {
$result[$currentName] = $val;
} else {
$result[$currentName] = ObjectSerializer::toString($val);
}
}
$currentName = $start;
}
return $result;
}
/**
* formdata must be limited to scalars or arrays of scalar values,
* or a resource for a file upload. Here we iterate through all available
* data and identify how to handle each scenario
*
* @param string|bool|array|DateTime|ArrayAccess|SplFileObject $value
*/
protected function makeFormSafe(mixed $value)
{
if ($value instanceof SplFileObject) {
return $this->processFiles([$value])[0];
}
if (is_resource($value)) {
$this->has_file = true;
return $value;
}
if ($value instanceof ModelInterface) {
return $this->processModel($value);
}
if (is_array($value) || is_object($value)) {
$data = [];
foreach ($value as $k => $v) {
$data[$k] = $this->makeFormSafe($v);
}
return $data;
}
return ObjectSerializer::toString($value);
}
/**
* We are able to handle nested ModelInterface. We do not simply call
* json_decode(json_encode()) because any given model may have binary data
* or other data that cannot be serialized to a JSON string
*/
protected function processModel(ModelInterface $model): array
{
$result = [];
foreach ($model::openAPITypes() as $name => $type) {
$value = $model->offsetGet($name);
if ($value === null) {
continue;
}
if (str_contains($type, '\SplFileObject')) {
$file = is_array($value) ? $value : [$value];
$result[$name] = $this->processFiles($file);
continue;
}
if ($value instanceof ModelInterface) {
$result[$name] = $this->processModel($value);
continue;
}
if (is_array($value) || is_object($value)) {
$result[$name] = $this->makeFormSafe($value);
continue;
}
$result[$name] = ObjectSerializer::toString($value);
}
return $result;
}
/**
* Handle file data
*/
protected function processFiles(array $files): array
{
$this->has_file = true;
$result = [];
foreach ($files as $i => $file) {
if (is_array($file)) {
$result[$i] = $this->processFiles($file);
continue;
}
if ($file instanceof StreamInterface) {
$result[$i] = $file;
continue;
}
if ($file instanceof SplFileObject) {
$result[$i] = $this->tryFopen($file);
}
}
return $result;
}
private function tryFopen(SplFileObject $file)
{
return Utils::tryFopen($file->getRealPath(), 'rb');
}
}