|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * FormDataProcessor |
| 5 | + * PHP version 7.4. |
| 6 | + * |
| 7 | + * @category Class |
| 8 | + * |
| 9 | + * @author OpenAPI Generator team |
| 10 | + * |
| 11 | + * @see https://openapi-generator.tech |
| 12 | + */ |
| 13 | + |
| 14 | +/** |
| 15 | + * Search API. |
| 16 | + * |
| 17 | + * The Algolia Search API lets you search, configure, and manage your indices and records. ## Client libraries Use Algolia's API clients and libraries to reliably integrate Algolia's APIs with your apps. The official API clients are covered by Algolia's [Service Level Agreement](https://www.algolia.com/policies/sla/). See: [Algolia's ecosystem](https://www.algolia.com/doc/guides/getting-started/how-algolia-works/in-depth/ecosystem/) ## Base URLs The base URLs for requests to the Search API are: - `https://{APPLICATION_ID}.algolia.net` - `https://{APPLICATION_ID}-dsn.algolia.net`. If your subscription includes a [Distributed Search Network](https://dashboard.algolia.com/infra), this ensures that requests are sent to servers closest to users. Both URLs provide high availability by distributing requests with load balancing. **All requests must use HTTPS.** ## Retry strategy To guarantee high availability, implement a retry strategy for all API requests using the URLs of your servers as fallbacks: - `https://{APPLICATION_ID}-1.algolianet.com` - `https://{APPLICATION_ID}-2.algolianet.com` - `https://{APPLICATION_ID}-3.algolianet.com` These URLs use a different DNS provider than the primary URLs. You should randomize this list to ensure an even load across the three servers. All Algolia API clients implement this retry strategy. ## Authentication To authenticate your API requests, add these headers: - `x-algolia-application-id`. Your Algolia application ID. - `x-algolia-api-key`. An API key with the necessary permissions to make the request. The required access control list (ACL) to make a request is listed in each endpoint's reference. You can find your application ID and API key in the [Algolia dashboard](https://dashboard.algolia.com/account). ## Request format Depending on the endpoint, request bodies are either JSON objects or arrays of JSON objects, ## Parameters Parameters are passed as query parameters for GET and DELETE requests, and in the request body for POST and PUT requests. Query parameters must be [URL-encoded](https://developer.mozilla.org/en-US/docs/Glossary/Percent-encoding). Non-ASCII characters must be UTF-8 encoded. Plus characters (`+`) are interpreted as spaces. Arrays as query parameters must be one of: - A comma-separated string: `attributesToRetrieve=title,description` - A URL-encoded JSON array: `attributesToRetrieve=%5B%22title%22,%22description%22%D` ## Response status and errors The Search API returns JSON responses. Since JSON doesn't guarantee any specific ordering, don't rely on the order of attributes in the API response. Successful responses return a `2xx` status. Client errors return a `4xx` status. Server errors are indicated by a `5xx` status. Error responses have a `message` property with more information. ## Version The current version of the Search API is version 1, as indicated by the `/1/` in each endpoint's URL. |
| 18 | + * |
| 19 | + * The version of the OpenAPI document: 1.0.0 |
| 20 | + * Generated by: https://openapi-generator.tech |
| 21 | + * Generator version: 7.13.0 |
| 22 | + */ |
| 23 | + |
| 24 | +/** |
| 25 | + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). |
| 26 | + * https://openapi-generator.tech |
| 27 | + * Do not edit the class manually. |
| 28 | + */ |
| 29 | + |
| 30 | +namespace Algolia\AlgoliaSearch; |
| 31 | + |
| 32 | +use Algolia\AlgoliaSearch\Model\Search\ModelInterface; |
| 33 | +use DateTime; |
| 34 | +use GuzzleHttp\Psr7\Utils; |
| 35 | +use Psr\Http\Message\StreamInterface; |
| 36 | +use SplFileObject; |
| 37 | + |
| 38 | +/** |
| 39 | + * FormDataProcessor Class Doc Comment. |
| 40 | + * |
| 41 | + * @category Class |
| 42 | + * |
| 43 | + * @author OpenAPI Generator team |
| 44 | + * |
| 45 | + * @see https://openapi-generator.tech |
| 46 | + */ |
| 47 | +class FormDataProcessor |
| 48 | +{ |
| 49 | + /** |
| 50 | + * Tags whether payload passed to ::prepare() contains one or more |
| 51 | + * SplFileObject or stream values. |
| 52 | + */ |
| 53 | + public bool $has_file = false; |
| 54 | + |
| 55 | + /** |
| 56 | + * Take value and turn it into an array suitable for inclusion in |
| 57 | + * the http body (form parameter). If it's a string, pass through unchanged |
| 58 | + * If it's a datetime object, format it in ISO8601. |
| 59 | + * |
| 60 | + * @param array<array|\ArrayAccess|bool|\DateTime|\SplFileObject|string> $values the value of the form parameter |
| 61 | + * |
| 62 | + * @return array [key => value] of formdata |
| 63 | + */ |
| 64 | + public function prepare(array $values): array |
| 65 | + { |
| 66 | + $this->has_file = false; |
| 67 | + $result = []; |
| 68 | + |
| 69 | + foreach ($values as $k => $v) { |
| 70 | + if (null === $v) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + $result[$k] = $this->makeFormSafe($v); |
| 75 | + } |
| 76 | + |
| 77 | + return $result; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Flattens a multi-level array of data and generates a single-level array |
| 82 | + * compatible with formdata - a single-level array where the keys use bracket |
| 83 | + * notation to signify nested data. |
| 84 | + * |
| 85 | + * credit: https://github.com/FranBar1966/FlatPHP |
| 86 | + */ |
| 87 | + public static function flatten(array $source, string $start = ''): array |
| 88 | + { |
| 89 | + $opt = [ |
| 90 | + 'prefix' => '[', |
| 91 | + 'suffix' => ']', |
| 92 | + 'suffix-end' => true, |
| 93 | + 'prefix-list' => '[', |
| 94 | + 'suffix-list' => ']', |
| 95 | + 'suffix-list-end' => true, |
| 96 | + ]; |
| 97 | + |
| 98 | + if ('' === $start) { |
| 99 | + $currentPrefix = ''; |
| 100 | + $currentSuffix = ''; |
| 101 | + $currentSuffixEnd = false; |
| 102 | + } elseif (array_is_list($source)) { |
| 103 | + $currentPrefix = $opt['prefix-list']; |
| 104 | + $currentSuffix = $opt['suffix-list']; |
| 105 | + $currentSuffixEnd = $opt['suffix-list-end']; |
| 106 | + } else { |
| 107 | + $currentPrefix = $opt['prefix']; |
| 108 | + $currentSuffix = $opt['suffix']; |
| 109 | + $currentSuffixEnd = $opt['suffix-end']; |
| 110 | + } |
| 111 | + |
| 112 | + $currentName = $start; |
| 113 | + $result = []; |
| 114 | + |
| 115 | + foreach ($source as $key => $val) { |
| 116 | + $currentName .= $currentPrefix.$key; |
| 117 | + |
| 118 | + if (is_array($val) && !empty($val)) { |
| 119 | + $currentName .= $currentSuffix; |
| 120 | + $result += self::flatten($val, $currentName); |
| 121 | + } else { |
| 122 | + if ($currentSuffixEnd) { |
| 123 | + $currentName .= $currentSuffix; |
| 124 | + } |
| 125 | + |
| 126 | + $result[$currentName] = ObjectSerializer::toString($val); |
| 127 | + } |
| 128 | + |
| 129 | + $currentName = $start; |
| 130 | + } |
| 131 | + |
| 132 | + return $result; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * formdata must be limited to scalars or arrays of scalar values, |
| 137 | + * or a resource for a file upload. Here we iterate through all available |
| 138 | + * data and identify how to handle each scenario. |
| 139 | + * |
| 140 | + * @param mixed $value |
| 141 | + */ |
| 142 | + protected function makeFormSafe($value) |
| 143 | + { |
| 144 | + if ($value instanceof \SplFileObject) { |
| 145 | + return $this->processFiles([$value])[0]; |
| 146 | + } |
| 147 | + |
| 148 | + if (is_resource($value)) { |
| 149 | + $this->has_file = true; |
| 150 | + |
| 151 | + return $value; |
| 152 | + } |
| 153 | + |
| 154 | + if ($value instanceof ModelInterface) { |
| 155 | + return $this->processModel($value); |
| 156 | + } |
| 157 | + |
| 158 | + if (is_array($value) || (is_object($value) && !$value instanceof \DateTimeInterface)) { |
| 159 | + $data = []; |
| 160 | + |
| 161 | + foreach ($value as $k => $v) { |
| 162 | + $data[$k] = $this->makeFormSafe($v); |
| 163 | + } |
| 164 | + |
| 165 | + return $data; |
| 166 | + } |
| 167 | + |
| 168 | + return ObjectSerializer::toString($value); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * We are able to handle nested ModelInterface. We do not simply call |
| 173 | + * json_decode(json_encode()) because any given model may have binary data |
| 174 | + * or other data that cannot be serialized to a JSON string. |
| 175 | + */ |
| 176 | + protected function processModel(ModelInterface $model): array |
| 177 | + { |
| 178 | + $result = []; |
| 179 | + |
| 180 | + foreach ($model::openAPITypes() as $name => $type) { |
| 181 | + $value = $model->offsetGet($name); |
| 182 | + |
| 183 | + if (null === $value) { |
| 184 | + continue; |
| 185 | + } |
| 186 | + |
| 187 | + if (false !== strpos($type, '\SplFileObject')) { |
| 188 | + $file = is_array($value) ? $value : [$value]; |
| 189 | + $result[$name] = $this->processFiles($file); |
| 190 | + |
| 191 | + continue; |
| 192 | + } |
| 193 | + |
| 194 | + if ($value instanceof ModelInterface) { |
| 195 | + $result[$name] = $this->processModel($value); |
| 196 | + |
| 197 | + continue; |
| 198 | + } |
| 199 | + |
| 200 | + if (is_array($value) || is_object($value)) { |
| 201 | + $result[$name] = $this->makeFormSafe($value); |
| 202 | + |
| 203 | + continue; |
| 204 | + } |
| 205 | + |
| 206 | + $result[$name] = ObjectSerializer::toString($value); |
| 207 | + } |
| 208 | + |
| 209 | + return $result; |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * Handle file data. |
| 214 | + */ |
| 215 | + protected function processFiles(array $files): array |
| 216 | + { |
| 217 | + $this->has_file = true; |
| 218 | + |
| 219 | + $result = []; |
| 220 | + |
| 221 | + foreach ($files as $i => $file) { |
| 222 | + if (is_array($file)) { |
| 223 | + $result[$i] = $this->processFiles($file); |
| 224 | + |
| 225 | + continue; |
| 226 | + } |
| 227 | + |
| 228 | + if ($file instanceof StreamInterface) { |
| 229 | + $result[$i] = $file; |
| 230 | + |
| 231 | + continue; |
| 232 | + } |
| 233 | + |
| 234 | + if ($file instanceof \SplFileObject) { |
| 235 | + $result[$i] = $this->tryFopen($file); |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + return $result; |
| 240 | + } |
| 241 | + |
| 242 | + private function tryFopen(\SplFileObject $file) |
| 243 | + { |
| 244 | + return Utils::tryFopen($file->getRealPath(), 'rb'); |
| 245 | + } |
| 246 | +} |
0 commit comments