PluginProbe
MONEI Payments for WooCommerce / 4.0.0
MONEI Payments for WooCommerce v4.0.0
7.3.3 7.3.2 7.3.1 7.3.0 7.2.4 7.2.3 7.2.2 7.2.0 7.2.1 7.1.3 2.1.0 3.0.0 3.1.0 3.1.1 4.0.0 4.1.0 4.1.1 4.2.0 4.2.1 5.0 5.1.0 5.1.1 5.1.2 5.2.2 5.2.3 All 87 releases
monei / includes / lib / ObjectSerializer.php

ObjectSerializer.php in MONEI Payments for WooCommerce 4.0.0, at includes/lib/ObjectSerializer.php

353 lines 12.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ObjectSerializer
4 *
5 * PHP version 5
6 *
7 * @category Class
8 * @package OpenAPI\Client
9 * @author OpenAPI Generator team
10 * @link https://openapi-generator.tech
11 */
12
13 /**
14 * MONEI API v1
15 *
16 * The MONEI API is organized around [REST](https://en.wikipedia.org/wiki/Representational_State_Transfer). Our API has predictable resource-oriented URLs, accepts JSON-encoded request bodies, returns JSON-encoded responses, and uses standard HTTP response codes, authentication, and verbs.
17 *
18 * The version of the OpenAPI document: 1.0.0
19 *
20 * Generated by: https://openapi-generator.tech
21 * OpenAPI Generator version: 4.3.1
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 OpenAPI\Client;
31
32 use OpenAPI\Client\Model\ModelInterface;
33
34 /**
35 * ObjectSerializer Class Doc Comment
36 *
37 * @category Class
38 * @package OpenAPI\Client
39 * @author OpenAPI Generator team
40 * @link https://openapi-generator.tech
41 */
42 class ObjectSerializer
43 {
44 /** @var string */
45 private static $dateTimeFormat = \DateTime::ATOM;
46
47 /**
48 * Change the date format
49 *
50 * @param string $format the new date format to use
51 */
52 public static function setDateTimeFormat($format)
53 {
54 self::$dateTimeFormat = $format;
55 }
56
57 /**
58 * Serialize data
59 *
60 * @param mixed $data the data to serialize
61 * @param string $type the OpenAPIToolsType of the data
62 * @param string $format the format of the OpenAPITools type of the data
63 *
64 * @return string|object serialized form of $data
65 */
66 public static function sanitizeForSerialization($data, $type = null, $format = null)
67 {
68 if (is_scalar($data) || null === $data) {
69 return $data;
70 } elseif ($data instanceof \DateTime) {
71 return ($format === 'date') ? $data->format('Y-m-d') : $data->format(self::$dateTimeFormat);
72 } elseif (is_array($data)) {
73 foreach ($data as $property => $value) {
74 $data[$property] = self::sanitizeForSerialization($value);
75 }
76 return $data;
77 } elseif (is_object($data)) {
78 $values = [];
79 if ($data instanceof ModelInterface) {
80 $formats = $data::openAPIFormats();
81 foreach ($data::openAPITypes() as $property => $openAPIType) {
82 $getter = $data::getters()[$property];
83 $value = $data->$getter();
84 if ($value !== null
85 && !in_array($openAPIType, ['DateTime', 'bool', 'boolean', 'byte', 'double', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)
86 && method_exists($openAPIType, 'getAllowableEnumValues')
87 && !in_array($value, $openAPIType::getAllowableEnumValues(), true)) {
88 $imploded = implode("', '", $openAPIType::getAllowableEnumValues());
89 throw new \InvalidArgumentException("Invalid value for enum '$openAPIType', must be one of: '$imploded'");
90 }
91 if ($value !== null) {
92 $values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value, $openAPIType, $formats[$property]);
93 }
94 }
95 } else {
96 foreach($data as $property => $value) {
97 $values[$property] = self::sanitizeForSerialization($value);
98 }
99 }
100 return (object)$values;
101 } else {
102 return (string)$data;
103 }
104 }
105
106 /**
107 * Sanitize filename by removing path.
108 * e.g. ../../sun.gif becomes sun.gif
109 *
110 * @param string $filename filename to be sanitized
111 *
112 * @return string the sanitized filename
113 */
114 public static function sanitizeFilename($filename)
115 {
116 if (preg_match("/.*[\/\\\\](.*)$/", $filename, $match)) {
117 return $match[1];
118 } else {
119 return $filename;
120 }
121 }
122
123 /**
124 * Take value and turn it into a string suitable for inclusion in
125 * the path, by url-encoding.
126 *
127 * @param string $value a string which will be part of the path
128 *
129 * @return string the serialized object
130 */
131 public static function toPathValue($value)
132 {
133 return rawurlencode(self::toString($value));
134 }
135
136 /**
137 * Take value and turn it into a string suitable for inclusion in
138 * the query, by imploding comma-separated if it's an object.
139 * If it's a string, pass through unchanged. It will be url-encoded
140 * later.
141 *
142 * @param string[]|string|\DateTime $object an object to be serialized to a string
143 *
144 * @return string the serialized object
145 */
146 public static function toQueryValue($object)
147 {
148 if (is_array($object)) {
149 return implode(',', $object);
150 } else {
151 return self::toString($object);
152 }
153 }
154
155 /**
156 * Take value and turn it into a string suitable for inclusion in
157 * the header. If it's a string, pass through unchanged
158 * If it's a datetime object, format it in ISO8601
159 *
160 * @param string $value a string which will be part of the header
161 *
162 * @return string the header string
163 */
164 public static function toHeaderValue($value)
165 {
166 if (method_exists($value, 'toHeaderValue')) {
167 return $value->toHeaderValue();
168 }
169
170 return self::toString($value);
171 }
172
173 /**
174 * Take value and turn it into a string suitable for inclusion in
175 * the http body (form parameter). If it's a string, pass through unchanged
176 * If it's a datetime object, format it in ISO8601
177 *
178 * @param string|\SplFileObject $value the value of the form parameter
179 *
180 * @return string the form string
181 */
182 public static function toFormValue($value)
183 {
184 if ($value instanceof \SplFileObject) {
185 return $value->getRealPath();
186 } else {
187 return self::toString($value);
188 }
189 }
190
191 /**
192 * Take value and turn it into a string suitable for inclusion in
193 * the parameter. If it's a string, pass through unchanged
194 * If it's a datetime object, format it in ISO8601
195 * If it's a boolean, convert it to "true" or "false".
196 *
197 * @param string|bool|\DateTime $value the value of the parameter
198 *
199 * @return string the header string
200 */
201 public static function toString($value)
202 {
203 if ($value instanceof \DateTime) { // datetime in ISO8601 format
204 return $value->format(self::$dateTimeFormat);
205 } else if (is_bool($value)) {
206 return $value ? 'true' : 'false';
207 } else {
208 return $value;
209 }
210 }
211
212 /**
213 * Serialize an array to a string.
214 *
215 * @param array $collection collection to serialize to a string
216 * @param string $style the format use for serialization (csv,
217 * ssv, tsv, pipes, multi)
218 * @param bool $allowCollectionFormatMulti allow collection format to be a multidimensional array
219 *
220 * @return string
221 */
222 public static function serializeCollection(array $collection, $style, $allowCollectionFormatMulti = false)
223 {
224 if ($allowCollectionFormatMulti && ('multi' === $style)) {
225 // http_build_query() almost does the job for us. We just
226 // need to fix the result of multidimensional arrays.
227 return preg_replace('/%5B[0-9]+%5D=/', '=', http_build_query($collection, '', '&'));
228 }
229 switch ($style) {
230 case 'pipeDelimited':
231 case 'pipes':
232 return implode('|', $collection);
233
234 case 'tsv':
235 return implode("\t", $collection);
236
237 case 'spaceDelimited':
238 case 'ssv':
239 return implode(' ', $collection);
240
241 case 'simple':
242 case 'csv':
243 // Deliberate fall through. CSV is default format.
244 default:
245 return implode(',', $collection);
246 }
247 }
248
249 /**
250 * Deserialize a JSON string into an object
251 *
252 * @param mixed $data object or primitive to be deserialized
253 * @param string $class class name is passed as a string
254 * @param string[] $httpHeaders HTTP headers
255 * @param string $discriminator discriminator if polymorphism is used
256 *
257 * @return object|array|null a single or an array of $class instances
258 */
259 public static function deserialize($data, $class, $httpHeaders = null)
260 {
261 if (null === $data) {
262 return null;
263 } elseif (substr($class, 0, 4) === 'map[') { // for associative array e.g. map[string,int]
264 $data = is_string($data) ? json_decode($data) : $data;
265 settype($data, 'array');
266 $inner = substr($class, 4, -1);
267 $deserialized = [];
268 if (strrpos($inner, ",") !== false) {
269 $subClass_array = explode(',', $inner, 2);
270 $subClass = $subClass_array[1];
271 foreach ($data as $key => $value) {
272 $deserialized[$key] = self::deserialize($value, $subClass, null);
273 }
274 }
275 return $deserialized;
276 } elseif (strcasecmp(substr($class, -2), '[]') === 0) {
277 $data = is_string($data) ? json_decode($data) : $data;
278 $subClass = substr($class, 0, -2);
279 $values = [];
280 foreach ($data as $key => $value) {
281 $values[] = self::deserialize($value, $subClass, null);
282 }
283 return $values;
284 } elseif ($class === 'object') {
285 settype($data, 'array');
286 return $data;
287 } elseif ($class === '\DateTime') {
288 // Some API's return an invalid, empty string as a
289 // date-time property. DateTime::__construct() will return
290 // the current time for empty input which is probably not
291 // what is meant. The invalid empty string is probably to
292 // be interpreted as a missing field/value. Let's handle
293 // this graceful.
294 if (!empty($data)) {
295 return new \DateTime($data);
296 } else {
297 return null;
298 }
299 } elseif (in_array($class, ['DateTime', 'bool', 'boolean', 'byte', 'double', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) {
300 settype($data, $class);
301 return $data;
302 } elseif ($class === '\SplFileObject') {
303 /** @var \Psr\Http\Message\StreamInterface $data */
304
305 // determine file name
306 if (array_key_exists('Content-Disposition', $httpHeaders) &&
307 preg_match('/inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeaders['Content-Disposition'], $match)) {
308 $filename = Configuration::getDefaultConfiguration()->getTempFolderPath() . DIRECTORY_SEPARATOR . self::sanitizeFilename($match[1]);
309 } else {
310 $filename = tempnam(Configuration::getDefaultConfiguration()->getTempFolderPath(), '');
311 }
312
313 $file = fopen($filename, 'w');
314 while ($chunk = $data->read(200)) {
315 fwrite($file, $chunk);
316 }
317 fclose($file);
318
319 return new \SplFileObject($filename, 'r');
320 } elseif (method_exists($class, 'getAllowableEnumValues')) {
321 if (!in_array($data, $class::getAllowableEnumValues(), true)) {
322 $imploded = implode("', '", $class::getAllowableEnumValues());
323 throw new \InvalidArgumentException("Invalid value for enum '$class', must be one of: '$imploded'");
324 }
325 return $data;
326 } else {
327 $data = is_string($data) ? json_decode($data) : $data;
328 // If a discriminator is defined and points to a valid subclass, use it.
329 $discriminator = $class::DISCRIMINATOR;
330 if (!empty($discriminator) && isset($data->{$discriminator}) && is_string($data->{$discriminator})) {
331 $subclass = '\OpenAPI\Client\Model\\' . $data->{$discriminator};
332 if (is_subclass_of($subclass, $class)) {
333 $class = $subclass;
334 }
335 }
336 $instance = new $class();
337 foreach ($instance::openAPITypes() as $property => $type) {
338 $propertySetter = $instance::setters()[$property];
339
340 if (!isset($propertySetter) || !isset($data->{$instance::attributeMap()[$property]})) {
341 continue;
342 }
343
344 $propertyValue = $data->{$instance::attributeMap()[$property]};
345 if (isset($propertyValue)) {
346 $instance->$propertySetter(self::deserialize($propertyValue, $type, null));
347 }
348 }
349 return $instance;
350 }
351 }
352 }
353