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