|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * FormDataProcessor |
| 4 | + * PHP version 8.1 |
| 5 | + * |
| 6 | + * @category Class |
| 7 | + * @package OpenAPI\Client |
| 8 | + * @author OpenAPI Generator team |
| 9 | + * @link https://openapi-generator.tech |
| 10 | + */ |
| 11 | + |
| 12 | +/** |
| 13 | + * Echo Server API |
| 14 | + * |
| 15 | + * Echo Server API |
| 16 | + * |
| 17 | + * The version of the OpenAPI document: 0.1.0 |
| 18 | + * Contact: team@openapitools.org |
| 19 | + * @generated Generated by: https://openapi-generator.tech |
| 20 | + * Generator version: 7.13.0-SNAPSHOT |
| 21 | + */ |
| 22 | + |
| 23 | +/** |
| 24 | + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). |
| 25 | + * https://openapi-generator.tech |
| 26 | + * Do not edit the class manually. |
| 27 | + */ |
| 28 | + |
| 29 | +namespace OpenAPI\Client; |
| 30 | + |
| 31 | +use ArrayAccess; |
| 32 | +use DateTime; |
| 33 | +use GuzzleHttp\Psr7\Utils; |
| 34 | +use Psr\Http\Message\StreamInterface; |
| 35 | +use SplFileObject; |
| 36 | +use OpenAPI\Client\Model\ModelInterface; |
| 37 | + |
| 38 | +class FormDataProcessor |
| 39 | +{ |
| 40 | + /** |
| 41 | + * Tags whether payload passed to ::prepare() contains one or more |
| 42 | + * SplFileObject or stream values. |
| 43 | + */ |
| 44 | + public bool $has_file = false; |
| 45 | + |
| 46 | + /** |
| 47 | + * Take value and turn it into an array suitable for inclusion in |
| 48 | + * the http body (form parameter). If it's a string, pass through unchanged |
| 49 | + * If it's a datetime object, format it in ISO8601 |
| 50 | + * |
| 51 | + * @param array<string|bool|array|DateTime|ArrayAccess|SplFileObject> $values the value of the form parameter |
| 52 | + * |
| 53 | + * @return array [key => value] of formdata |
| 54 | + */ |
| 55 | + public function prepare(array $values): array |
| 56 | + { |
| 57 | + $this->has_file = false; |
| 58 | + $result = []; |
| 59 | + |
| 60 | + foreach ($values as $k => $v) { |
| 61 | + if ($v === null) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + $result[$k] = $this->makeFormSafe($v); |
| 66 | + } |
| 67 | + |
| 68 | + return $result; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Flattens a multi-level array of data and generates a single-level array |
| 73 | + * compatible with formdata - a single-level array where the keys use bracket |
| 74 | + * notation to signify nested data. |
| 75 | + * |
| 76 | + * credit: https://github.com/FranBar1966/FlatPHP |
| 77 | + */ |
| 78 | + public static function flatten(array $source, string $start = ''): array |
| 79 | + { |
| 80 | + $opt = [ |
| 81 | + 'prefix' => '[', |
| 82 | + 'suffix' => ']', |
| 83 | + 'suffix-end' => true, |
| 84 | + 'prefix-list' => '[', |
| 85 | + 'suffix-list' => ']', |
| 86 | + 'suffix-list-end' => true, |
| 87 | + ]; |
| 88 | + |
| 89 | + if ($start === '') { |
| 90 | + $currentPrefix = ''; |
| 91 | + $currentSuffix = ''; |
| 92 | + $currentSuffixEnd = false; |
| 93 | + } elseif (array_is_list($source)) { |
| 94 | + $currentPrefix = $opt['prefix-list']; |
| 95 | + $currentSuffix = $opt['suffix-list']; |
| 96 | + $currentSuffixEnd = $opt['suffix-list-end']; |
| 97 | + } else { |
| 98 | + $currentPrefix = $opt['prefix']; |
| 99 | + $currentSuffix = $opt['suffix']; |
| 100 | + $currentSuffixEnd = $opt['suffix-end']; |
| 101 | + } |
| 102 | + |
| 103 | + $currentName = $start; |
| 104 | + $result = []; |
| 105 | + |
| 106 | + foreach ($source as $key => $val) { |
| 107 | + $currentName .= $currentPrefix . $key; |
| 108 | + |
| 109 | + if (is_array($val) && !empty($val)) { |
| 110 | + $currentName .= $currentSuffix; |
| 111 | + $result += self::flatten($val, $currentName); |
| 112 | + } else { |
| 113 | + if ($currentSuffixEnd) { |
| 114 | + $currentName .= $currentSuffix; |
| 115 | + } |
| 116 | + |
| 117 | + $result[$currentName] = ObjectSerializer::toString($val); |
| 118 | + } |
| 119 | + |
| 120 | + $currentName = $start; |
| 121 | + } |
| 122 | + |
| 123 | + return $result; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * formdata must be limited to scalars or arrays of scalar values, |
| 128 | + * or a resource for a file upload. Here we iterate through all available |
| 129 | + * data and identify how to handle each scenario |
| 130 | + * |
| 131 | + * @param string|bool|array|DateTime|ArrayAccess|SplFileObject $value |
| 132 | + */ |
| 133 | + protected function makeFormSafe(mixed $value) |
| 134 | + { |
| 135 | + if ($value instanceof SplFileObject) { |
| 136 | + return $this->processFiles([$value])[0]; |
| 137 | + } |
| 138 | + |
| 139 | + if (is_resource($value)) { |
| 140 | + $this->has_file = true; |
| 141 | + |
| 142 | + return $value; |
| 143 | + } |
| 144 | + |
| 145 | + if ($value instanceof ModelInterface) { |
| 146 | + return $this->processModel($value); |
| 147 | + } |
| 148 | + |
| 149 | + if (is_array($value) || is_object($value)) { |
| 150 | + $data = []; |
| 151 | + |
| 152 | + foreach ($value as $k => $v) { |
| 153 | + $data[$k] = $this->makeFormSafe($v); |
| 154 | + } |
| 155 | + |
| 156 | + return $data; |
| 157 | + } |
| 158 | + |
| 159 | + return ObjectSerializer::toString($value); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * We are able to handle nested ModelInterface. We do not simply call |
| 164 | + * json_decode(json_encode()) because any given model may have binary data |
| 165 | + * or other data that cannot be serialized to a JSON string |
| 166 | + */ |
| 167 | + protected function processModel(ModelInterface $model): array |
| 168 | + { |
| 169 | + $result = []; |
| 170 | + |
| 171 | + foreach ($model::openAPITypes() as $name => $type) { |
| 172 | + $value = $model->offsetGet($name); |
| 173 | + |
| 174 | + if ($value === null) { |
| 175 | + continue; |
| 176 | + } |
| 177 | + |
| 178 | + if (str_contains($type, '\SplFileObject')) { |
| 179 | + $file = is_array($value) ? $value : [$value]; |
| 180 | + $result[$name] = $this->processFiles($file); |
| 181 | + |
| 182 | + continue; |
| 183 | + } |
| 184 | + |
| 185 | + if ($value instanceof ModelInterface) { |
| 186 | + $result[$name] = $this->processModel($value); |
| 187 | + |
| 188 | + continue; |
| 189 | + } |
| 190 | + |
| 191 | + if (is_array($value) || is_object($value)) { |
| 192 | + $result[$name] = $this->makeFormSafe($value); |
| 193 | + |
| 194 | + continue; |
| 195 | + } |
| 196 | + |
| 197 | + $result[$name] = ObjectSerializer::toString($value); |
| 198 | + } |
| 199 | + |
| 200 | + return $result; |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Handle file data |
| 205 | + */ |
| 206 | + protected function processFiles(array $files): array |
| 207 | + { |
| 208 | + $this->has_file = true; |
| 209 | + |
| 210 | + $result = []; |
| 211 | + |
| 212 | + foreach ($files as $i => $file) { |
| 213 | + if (is_array($file)) { |
| 214 | + $result[$i] = $this->processFiles($file); |
| 215 | + |
| 216 | + continue; |
| 217 | + } |
| 218 | + |
| 219 | + if ($file instanceof StreamInterface) { |
| 220 | + $result[$i] = $file; |
| 221 | + |
| 222 | + continue; |
| 223 | + } |
| 224 | + |
| 225 | + if ($file instanceof SplFileObject) { |
| 226 | + $result[$i] = $this->tryFopen($file); |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + return $result; |
| 231 | + } |
| 232 | + |
| 233 | + private function tryFopen(SplFileObject $file) |
| 234 | + { |
| 235 | + return Utils::tryFopen($file->getRealPath(), 'rb'); |
| 236 | + } |
| 237 | +} |
0 commit comments