|
| 1 | +<?php |
| 2 | + |
| 3 | +{{>partial_header}} |
| 4 | + |
| 5 | +namespace Model; |
| 6 | + |
| 7 | + |
| 8 | +class ObjectSerializer |
| 9 | +{ |
| 10 | + /** @var string */ |
| 11 | + private static $dateTimeFormat = \DateTime::ATOM; |
| 12 | +
|
| 13 | + /** |
| 14 | + * Change the date format |
| 15 | + * |
| 16 | + * @param string $format the new date format to use |
| 17 | + */ |
| 18 | + public static function setDateTimeFormat($format) |
| 19 | + { |
| 20 | + self::$dateTimeFormat = $format; |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Serialize data |
| 25 | + * |
| 26 | + * @param mixed $data the data to serialize |
| 27 | + * @param string $type the OpenAPIToolsType of the data |
| 28 | + * @param string $format the format of the OpenAPITools type of the data |
| 29 | + * |
| 30 | + * @return scalar|object|array|null serialized form of $data |
| 31 | + */ |
| 32 | + public static function sanitizeForSerialization($data, $type = null, $format = null) |
| 33 | + { |
| 34 | + if (is_scalar($data) || null === $data) { |
| 35 | + return $data; |
| 36 | + } |
| 37 | + |
| 38 | + if ($data instanceof \DateTime) { |
| 39 | + return ($format === 'date') ? $data->format('Y-m-d') : $data->format(self::$dateTimeFormat); |
| 40 | + } |
| 41 | + |
| 42 | + if (is_array($data)) { |
| 43 | + foreach ($data as $property => $value) { |
| 44 | + $data[$property] = self::sanitizeForSerialization($value); |
| 45 | + } |
| 46 | + return $data; |
| 47 | + } |
| 48 | + |
| 49 | + if (is_object($data)) { |
| 50 | + $values = []; |
| 51 | + if ($data instanceof ModelInterface) { |
| 52 | + $formats = $data::openAPIFormats(); |
| 53 | + foreach ($data::openAPITypes() as $property => $openAPIType) { |
| 54 | + $getter = $data::getters()[$property]; |
| 55 | + $value = $data->$getter(); |
| 56 | + if ($value !== null && !in_array($openAPIType, [{{&primitives}}], true)) { |
| 57 | + $callable = [$openAPIType, 'getAllowableEnumValues']; |
| 58 | + if (is_callable($callable)) { |
| 59 | + /** array $callable */ |
| 60 | + $allowedEnumTypes = $callable(); |
| 61 | + if (!in_array($value, $allowedEnumTypes, true)) { |
| 62 | + $imploded = implode("', '", $allowedEnumTypes); |
| 63 | + throw new \InvalidArgumentException("Invalid value for enum '$openAPIType', must be one of: '$imploded'"); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + if (($data::isNullable($property) && $data->isNullableSetToNull($property)) || $value !== null) { |
| 68 | + $values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value, $openAPIType, $formats[$property]); |
| 69 | + } |
| 70 | + } |
| 71 | + } else { |
| 72 | + foreach($data as $property => $value) { |
| 73 | + $values[$property] = self::sanitizeForSerialization($value); |
| 74 | + } |
| 75 | + } |
| 76 | + return (object)$values; |
| 77 | + } else { |
| 78 | + return (string)$data; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Sanitize filename by removing path. |
| 84 | + * e.g. ../../sun.gif becomes sun.gif |
| 85 | + * |
| 86 | + * @param string $filename filename to be sanitized |
| 87 | + * |
| 88 | + * @return string the sanitized filename |
| 89 | + */ |
| 90 | + public static function sanitizeFilename($filename) |
| 91 | + { |
| 92 | + if (preg_match("/.*[\/\\\\](.*)$/", $filename, $match)) { |
| 93 | + return $match[1]; |
| 94 | + } else { |
| 95 | + return $filename; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Shorter timestamp microseconds to 6 digits length. |
| 101 | + * |
| 102 | + * @param string $timestamp Original timestamp |
| 103 | + * |
| 104 | + * @return string the shorten timestamp |
| 105 | + */ |
| 106 | + public static function sanitizeTimestamp($timestamp) |
| 107 | + { |
| 108 | + if (!is_string($timestamp)) return $timestamp; |
| 109 | +
|
| 110 | + return preg_replace('/(:\d{2}.\d{6})\d*/', '$1', $timestamp); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Serialize an array to a string. |
| 115 | + * |
| 116 | + * @param array $collection collection to serialize to a string |
| 117 | + * @param string $style the format use for serialization (csv, |
| 118 | + * ssv, tsv, pipes, multi) |
| 119 | + * @param bool $allowCollectionFormatMulti allow collection format to be a multidimensional array |
| 120 | + * |
| 121 | + * @return string |
| 122 | + */ |
| 123 | + public static function serializeCollection(array $collection, $style, $allowCollectionFormatMulti = false) |
| 124 | + { |
| 125 | + if ($allowCollectionFormatMulti && ('multi' === $style)) { |
| 126 | + // http_build_query() almost does the job for us. We just |
| 127 | + // need to fix the result of multidimensional arrays. |
| 128 | + return preg_replace('/%5B[0-9]+%5D=/', '=', http_build_query($collection, '', '&')); |
| 129 | + } |
| 130 | + switch ($style) { |
| 131 | + case 'pipeDelimited': |
| 132 | + case 'pipes': |
| 133 | + return implode('|', $collection); |
| 134 | +
|
| 135 | + case 'tsv': |
| 136 | + return implode("\t", $collection); |
| 137 | +
|
| 138 | + case 'spaceDelimited': |
| 139 | + case 'ssv': |
| 140 | + return implode(' ', $collection); |
| 141 | +
|
| 142 | + case 'simple': |
| 143 | + case 'csv': |
| 144 | + // Deliberate fall through. CSV is default format. |
| 145 | + default: |
| 146 | + return implode(',', $collection); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Deserialize a JSON string into an object |
| 152 | + * |
| 153 | + * @param mixed $data object or primitive to be deserialized |
| 154 | + * @param string $class class name is passed as a string |
| 155 | + * @param string[] $httpHeaders HTTP headers |
| 156 | + * @param string $discriminator discriminator if polymorphism is used |
| 157 | + * |
| 158 | + * @return object|array|null a single or an array of $class instances |
| 159 | + */ |
| 160 | + public static function deserialize($data, $class, $httpHeaders = null) |
| 161 | + { |
| 162 | + if (null === $data) { |
| 163 | + return null; |
| 164 | + } |
| 165 | + |
| 166 | + if (strcasecmp(substr($class, -2), '[]') === 0) { |
| 167 | + $data = is_string($data) ? json_decode($data) : $data; |
| 168 | +
|
| 169 | + if (!is_array($data)) { |
| 170 | + throw new \InvalidArgumentException("Invalid array '$class'"); |
| 171 | + } |
| 172 | + |
| 173 | + $subClass = substr($class, 0, -2); |
| 174 | + $values = []; |
| 175 | + foreach ($data as $key => $value) { |
| 176 | + $values[] = self::deserialize($value, $subClass, null); |
| 177 | + } |
| 178 | + return $values; |
| 179 | + } |
| 180 | + |
| 181 | + if (preg_match('/^(array<|map\[)/', $class)) { // for associative array e.g. array<string,int> |
| 182 | + $data = is_string($data) ? json_decode($data) : $data; |
| 183 | + settype($data, 'array'); |
| 184 | + $inner = substr($class, 4, -1); |
| 185 | + $deserialized = []; |
| 186 | + if (strrpos($inner, ",") !== false) { |
| 187 | + $subClass_array = explode(',', $inner, 2); |
| 188 | + $subClass = $subClass_array[1]; |
| 189 | + foreach ($data as $key => $value) { |
| 190 | + $deserialized[$key] = self::deserialize($value, $subClass, null); |
| 191 | + } |
| 192 | + } |
| 193 | + return $deserialized; |
| 194 | + } |
| 195 | + |
| 196 | + if ($class === 'object') { |
| 197 | + settype($data, 'array'); |
| 198 | + return $data; |
| 199 | + } elseif ($class === 'mixed') { |
| 200 | + settype($data, gettype($data)); |
| 201 | + return $data; |
| 202 | + } |
| 203 | + |
| 204 | + if ($class === '\DateTime') { |
| 205 | + // Some APIs return an invalid, empty string as a |
| 206 | + // date-time property. DateTime::__construct() will return |
| 207 | + // the current time for empty input which is probably not |
| 208 | + // what is meant. The invalid empty string is probably to |
| 209 | + // be interpreted as a missing field/value. Let's handle |
| 210 | + // this graceful. |
| 211 | + if (!empty($data)) { |
| 212 | + try { |
| 213 | + return new \DateTime($data); |
| 214 | + } catch (\Exception $exception) { |
| 215 | + // Some APIs return a date-time with too high nanosecond |
| 216 | + // precision for php's DateTime to handle. |
| 217 | + // With provided regexp 6 digits of microseconds saved |
| 218 | + return new \DateTime(self::sanitizeTimestamp($data)); |
| 219 | + } |
| 220 | + } else { |
| 221 | + return null; |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + if ($class === '\SplFileObject') { |
| 226 | + $data = Utils::streamFor($data); |
| 227 | +
|
| 228 | + /** @var \Psr\Http\Message\StreamInterface $data */ |
| 229 | +
|
| 230 | + // determine file name |
| 231 | + if ( |
| 232 | + is_array($httpHeaders) |
| 233 | + && array_key_exists('Content-Disposition', $httpHeaders) |
| 234 | + && preg_match('/inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeaders['Content-Disposition'], $match) |
| 235 | + ) { |
| 236 | + $filename = Configuration::getDefaultConfiguration()->getTempFolderPath() . DIRECTORY_SEPARATOR . self::sanitizeFilename($match[1]); |
| 237 | + } else { |
| 238 | + $filename = tempnam(Configuration::getDefaultConfiguration()->getTempFolderPath(), ''); |
| 239 | + } |
| 240 | + |
| 241 | + $file = fopen($filename, 'w'); |
| 242 | + while ($chunk = $data->read(200)) { |
| 243 | + fwrite($file, $chunk); |
| 244 | + } |
| 245 | + fclose($file); |
| 246 | + |
| 247 | + return new \SplFileObject($filename, 'r'); |
| 248 | + } |
| 249 | + |
| 250 | + /** @psalm-suppress ParadoxicalCondition */ |
| 251 | + if (in_array($class, [{{&primitives}}], true)) { |
| 252 | + settype($data, $class); |
| 253 | + return $data; |
| 254 | + } |
| 255 | + |
| 256 | + |
| 257 | + if (method_exists($class, 'getAllowableEnumValues')) { |
| 258 | + if (!in_array($data, $class::getAllowableEnumValues(), true)) { |
| 259 | + $imploded = implode("', '", $class::getAllowableEnumValues()); |
| 260 | + throw new \InvalidArgumentException("Invalid value for enum '$class', must be one of: '$imploded'"); |
| 261 | + } |
| 262 | + return $data; |
| 263 | + } else { |
| 264 | + $data = is_string($data) ? json_decode($data) : $data; |
| 265 | +
|
| 266 | + if (is_array($data)) { |
| 267 | + $data = (object)$data; |
| 268 | + } |
| 269 | + |
| 270 | + // If a discriminator is defined and points to a valid subclass, use it. |
| 271 | + $discriminator = $class::DISCRIMINATOR; |
| 272 | + if (!empty($discriminator) && isset($data->{$discriminator}) && is_string($data->{$discriminator})) { |
| 273 | + $subclass = '\{{invokerPackage}}\Model\\' . $data->{$discriminator}; |
| 274 | + if (is_subclass_of($subclass, $class)) { |
| 275 | + $class = $subclass; |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + /** @var ModelInterface $instance */ |
| 280 | + $instance = new $class(); |
| 281 | + foreach ($instance::openAPITypes() as $property => $type) { |
| 282 | + $propertySetter = $instance::setters()[$property]; |
| 283 | +
|
| 284 | + if (!isset($propertySetter)) { |
| 285 | + continue; |
| 286 | + } |
| 287 | + |
| 288 | + if (!isset($data->{$instance::attributeMap()[$property]})) { |
| 289 | + if ($instance::isNullable($property)) { |
| 290 | + $instance->$propertySetter(null); |
| 291 | + } |
| 292 | + |
| 293 | + continue; |
| 294 | + } |
| 295 | + |
| 296 | + if (isset($data->{$instance::attributeMap()[$property]})) { |
| 297 | + $propertyValue = $data->{$instance::attributeMap()[$property]}; |
| 298 | + $instance->$propertySetter(self::deserialize($propertyValue, $type, null)); |
| 299 | + } |
| 300 | + } |
| 301 | + return $instance; |
| 302 | + } |
| 303 | + } |
| 304 | +} |
0 commit comments