Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
38.68% |
41 / 106 |
|
28.57% |
2 / 7 |
CRAP | |
0.00% |
0 / 1 |
ObjectSerializer | |
38.68% |
41 / 106 |
|
28.57% |
2 / 7 |
726.37 | |
0.00% |
0 / 1 |
sanitizeForSerialization | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
306 | |||
toPathValue | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
toQueryValue | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
toString | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
deserialize | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
mapToClass | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
6 | |||
castData | |
50.00% |
27 / 54 |
|
0.00% |
0 / 1 |
96.00 |
1 | <?php |
2 | /** |
3 | * ObjectSerializer. |
4 | * |
5 | * PHP version 5 |
6 | * |
7 | * @category Class |
8 | * |
9 | * @author Swagger Codegen team |
10 | * |
11 | * @see https://github.com/swagger-api/swagger-codegen |
12 | */ |
13 | |
14 | /** |
15 | * Fingerprint Pro Server API. |
16 | * |
17 | * Fingerprint Pro Server API allows you to get information about visitors and about individual events in a server environment. It can be used for data exports, decision-making, and data analysis scenarios. Server API is intended for server-side usage, it's not intended to be used from the client side, whether it's a browser or a mobile device. |
18 | * |
19 | * OpenAPI spec version: 3 |
20 | * Contact: support@fingerprint.com |
21 | * Generated by: https://github.com/swagger-api/swagger-codegen.git |
22 | * Swagger Codegen version: 3.0.34 |
23 | */ |
24 | /** |
25 | * NOTE: This class is auto generated by the swagger code generator program. |
26 | * https://github.com/swagger-api/swagger-codegen |
27 | * Do not edit the class manually. |
28 | */ |
29 | |
30 | namespace Fingerprint\ServerAPI; |
31 | |
32 | use Psr\Http\Message\ResponseInterface; |
33 | |
34 | /** |
35 | * ObjectSerializer Class Doc Comment. |
36 | * |
37 | * @category Class |
38 | * |
39 | * @author Swagger Codegen team |
40 | * |
41 | * @see https://github.com/swagger-api/swagger-codegen |
42 | */ |
43 | class ObjectSerializer |
44 | { |
45 | /** |
46 | * Serialize data. |
47 | * |
48 | * @param mixed $data the data to serialize |
49 | * @param string|null $format the format of the Swagger type of the data |
50 | * |
51 | * @return array|object|string serialized form of $data |
52 | */ |
53 | public static function sanitizeForSerialization(mixed $data, ?string $format = null): array|bool|object|string |
54 | { |
55 | if (is_scalar($data) || null === $data) { |
56 | return $data; |
57 | } |
58 | if ($data instanceof \DateTime) { |
59 | return ('date' === $format) ? $data->format('Y-m-d') : $data->format(\DateTime::ATOM); |
60 | } |
61 | if (is_array($data)) { |
62 | foreach ($data as $property => $value) { |
63 | $data[$property] = self::sanitizeForSerialization($value); |
64 | } |
65 | |
66 | return $data; |
67 | } |
68 | if ($data instanceof \stdClass) { |
69 | foreach ($data as $property => $value) { |
70 | $data->{$property} = self::sanitizeForSerialization($value); |
71 | } |
72 | |
73 | return $data; |
74 | } |
75 | |
76 | if (is_object($data)) { |
77 | $class = get_class($data); |
78 | if (enum_exists($class)) { |
79 | return $data->value; |
80 | } |
81 | $values = []; |
82 | $formats = $data::swaggerFormats(); |
83 | foreach ($data::swaggerTypes() as $property => $swaggerType) { |
84 | $getter = $data::getters()[$property]; |
85 | $value = $data->{$getter}(); |
86 | if (null !== $value |
87 | && !in_array($swaggerType, ['DateTime', 'bool', 'boolean', 'byte', 'double', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true) |
88 | && method_exists($swaggerType, 'getAllowableEnumValues') |
89 | && !in_array($value, $swaggerType::getAllowableEnumValues())) { |
90 | $imploded = implode("', '", $swaggerType::getAllowableEnumValues()); |
91 | |
92 | throw new \InvalidArgumentException("Invalid value for enum '{$swaggerType}', must be one of: '{$imploded}'"); |
93 | } |
94 | if (null !== $value) { |
95 | $values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value, $swaggerType, $formats[$property]); |
96 | } |
97 | } |
98 | |
99 | return (object) $values; |
100 | } |
101 | |
102 | return (string) $data; |
103 | } |
104 | |
105 | /** |
106 | * Take value and turn it into a string suitable for inclusion in |
107 | * the path, by url-encoding. |
108 | * |
109 | * @param string $value a string which will be part of the path |
110 | * |
111 | * @return string the serialized object |
112 | */ |
113 | public static function toPathValue(string $value): string |
114 | { |
115 | return rawurlencode(self::toString($value)); |
116 | } |
117 | |
118 | /** |
119 | * Take value and turn it into a string suitable for inclusion in |
120 | * the query, by imploding comma-separated if it's an object. |
121 | * If it's a string, pass through unchanged. It will be url-encoded |
122 | * later. |
123 | * |
124 | * @param \DateTime|string|string[] $object an object to be serialized to a string |
125 | * @param string|null $format the format of the parameter |
126 | * |
127 | * @return string the serialized object |
128 | */ |
129 | public static function toQueryValue(array|\DateTime|string $object, ?string $format = null): string |
130 | { |
131 | if (is_array($object)) { |
132 | return implode(',', $object); |
133 | } |
134 | |
135 | return self::toString($object, $format); |
136 | } |
137 | |
138 | /** |
139 | * Take value and turn it into a string suitable for inclusion in |
140 | * the parameter. If it's a string, pass through unchanged |
141 | * If it's a datetime object, format it in RFC3339 |
142 | * If it's a date, format it in Y-m-d. |
143 | * |
144 | * @param \DateTime|string $value the value of the parameter |
145 | * @param string|null $format the format of the parameter |
146 | * |
147 | * @return string the header string |
148 | */ |
149 | public static function toString(\DateTime|string $value, ?string $format = null): string |
150 | { |
151 | if ($value instanceof \DateTime) { |
152 | return ('date' === $format) ? $value->format('Y-m-d') : $value->format(\DateTime::ATOM); |
153 | } |
154 | |
155 | return $value; |
156 | } |
157 | |
158 | /** |
159 | * Deserialize a JSON string into an object. |
160 | * |
161 | * @param string $class class name is passed as a string |
162 | * |
163 | * @throws \Exception |
164 | */ |
165 | public static function deserialize(ResponseInterface $response, string $class): mixed |
166 | { |
167 | $data = $response->getBody()->getContents(); |
168 | $response->getBody()->rewind(); |
169 | |
170 | return self::mapToClass($data, $class, $response); |
171 | } |
172 | |
173 | protected static function mapToClass(mixed $data, string $class, ResponseInterface $response): mixed |
174 | { |
175 | if ('string' === gettype($data)) { |
176 | $data = json_decode($data, false); |
177 | } |
178 | $instance = new $class(); |
179 | foreach ($instance::swaggerTypes() as $property => $type) { |
180 | $propertySetter = $instance::setters()[$property]; |
181 | |
182 | if (!isset($propertySetter) || !isset($data->{$instance::attributeMap()[$property]})) { |
183 | continue; |
184 | } |
185 | |
186 | $propertyValue = $data->{$instance::attributeMap()[$property]}; |
187 | if (isset($propertyValue)) { |
188 | $instance->{$propertySetter}(self::castData($propertyValue, $type, $response)); |
189 | } |
190 | } |
191 | |
192 | return $instance; |
193 | } |
194 | |
195 | protected static function castData(mixed $data, string $class, ResponseInterface $response): mixed |
196 | { |
197 | if (null === $data) { |
198 | return null; |
199 | } |
200 | if (str_starts_with($class, 'map[')) { // for associative array e.g. map[string,int] |
201 | $inner = substr($class, 4, -1); |
202 | $deserialized = []; |
203 | if (false !== strrpos($inner, ',')) { |
204 | $subClass_array = explode(',', $inner, 2); |
205 | $subClass = $subClass_array[1]; |
206 | foreach ($data as $key => $value) { |
207 | $deserialized[$key] = self::castData($value, $subClass, $response); |
208 | } |
209 | } |
210 | |
211 | return $deserialized; |
212 | } |
213 | if (0 === strcasecmp(substr($class, -2), '[]')) { |
214 | $subClass = substr($class, 0, -2); |
215 | $values = []; |
216 | foreach ($data as $key => $value) { |
217 | $values[] = self::castData($value, $subClass, $response); |
218 | } |
219 | |
220 | return $values; |
221 | } |
222 | if ('mixed' === $class) { |
223 | if ($data instanceof \stdClass) { |
224 | if (empty(get_object_vars($data))) { |
225 | return null; |
226 | } |
227 | |
228 | return (array) $data; |
229 | } |
230 | |
231 | return $data; |
232 | } |
233 | if ('object' === $class || 'array' === $class) { |
234 | settype($data, 'array'); |
235 | |
236 | return $data; |
237 | } |
238 | if ('\DateTime' === $class) { |
239 | // Some API's return an invalid, empty string as a |
240 | // date-time property. DateTime::__construct() will return |
241 | // the current time for empty input which is probably not |
242 | // what is meant. The invalid empty string is probably to |
243 | // be interpreted as a missing field/value. Let's handle |
244 | // this graceful. |
245 | if (!empty($data)) { |
246 | return new \DateTime($data); |
247 | } |
248 | |
249 | return null; |
250 | } |
251 | if (in_array($class, ['DateTime', 'bool', 'boolean', 'byte', 'double', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) { |
252 | $originalData = $data; |
253 | $normalizedClass = strtolower($class); |
254 | |
255 | switch ($normalizedClass) { |
256 | case 'int': |
257 | $normalizedClass = 'integer'; |
258 | |
259 | break; |
260 | |
261 | case 'bool': |
262 | $normalizedClass = 'boolean'; |
263 | |
264 | break; |
265 | } |
266 | if ('float' === $normalizedClass && is_numeric($originalData)) { |
267 | return (float) $originalData; |
268 | } |
269 | if ('string' === $normalizedClass && is_object($data)) { |
270 | throw new SerializationException($response); |
271 | } |
272 | |
273 | settype($data, $class); |
274 | if (gettype($data) === $normalizedClass) { |
275 | return $data; |
276 | } |
277 | |
278 | throw new SerializationException($response); |
279 | } elseif (enum_exists($class)) { |
280 | try { |
281 | return $class::from($data); |
282 | } catch (\ValueError $e) { |
283 | $allowedValues = array_map(fn ($case) => $case->value, $class::cases()); |
284 | $imploded = implode("', '", $allowedValues); |
285 | |
286 | throw new \InvalidArgumentException("Invalid value '{$data}' for enum '{$class}', must be one of: '{$imploded}'"); |
287 | } |
288 | |
289 | return $data; |
290 | } |
291 | |
292 | return self::mapToClass($data, $class, $response); |
293 | } |
294 | } |