Skip to content

Commit 713bd01

Browse files
committed
refactor: improve ObjectSerializer
1 parent 7c7d079 commit 713bd01

File tree

3 files changed

+77
-74
lines changed

3 files changed

+77
-74
lines changed

src/ObjectSerializer.php

Lines changed: 46 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,36 @@
11
<?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-
152
/**
163
* Fingerprint Pro Server API.
174
*
185
* 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.
296
*/
307

318
namespace Fingerprint\ServerAPI;
329

10+
use DateTime;
11+
use DateTimeInterface;
12+
use Exception;
13+
use InvalidArgumentException;
3314
use Psr\Http\Message\ResponseInterface;
15+
use stdClass;
3416

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-
*/
4417
class ObjectSerializer
4518
{
4619
/**
4720
* Serialize data.
4821
*
49-
* @param mixed $data the data to serialize
22+
* @param mixed $data the data to serialize
5023
* @param string|null $format the format of the Swagger type of the data
5124
*
52-
* @return array|object|string serialized form of $data
25+
* @return array|bool|object|string serialized form of $data
5326
*/
5427
public static function sanitizeForSerialization(mixed $data, ?string $format = null): array|bool|object|string
5528
{
5629
if (is_scalar($data) || null === $data) {
5730
return $data;
5831
}
59-
if ($data instanceof \DateTime) {
60-
return ('date' === $format) ? $data->format('Y-m-d') : $data->format(\DateTime::ATOM);
32+
if ($data instanceof DateTime) {
33+
return ('date' === $format) ? $data->format('Y-m-d') : $data->format(DateTimeInterface::ATOM);
6134
}
6235
if (is_array($data)) {
6336
foreach ($data as $property => $value) {
@@ -70,7 +43,7 @@ public static function sanitizeForSerialization(mixed $data, ?string $format = n
7043

7144
return $data;
7245
}
73-
if ($data instanceof \stdClass) {
46+
if ($data instanceof stdClass) {
7447
foreach ($data as $property => $value) {
7548
if (is_scalar($value)) {
7649
$data->{$property} = $value;
@@ -98,15 +71,15 @@ public static function sanitizeForSerialization(mixed $data, ?string $format = n
9871
&& !in_array($value, $swaggerType::getAllowableEnumValues())) {
9972
$imploded = implode("', '", $swaggerType::getAllowableEnumValues());
10073

101-
throw new \InvalidArgumentException("Invalid value for enum '{$swaggerType}', must be one of: '{$imploded}'");
74+
throw new InvalidArgumentException("Invalid value for enum '$swaggerType', must be one of: '$imploded'");
10275
}
10376
if (null !== $value) {
10477
if (is_scalar($value)) {
10578
$values[$data::attributeMap()[$property]] = $value;
10679
} elseif ('tag' === $property && empty($value)) {
107-
$values[$data::attributeMap()[$property]] = new \stdClass();
80+
$values[$data::attributeMap()[$property]] = new stdClass();
10881
} else {
109-
$values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value, $swaggerType, $formats[$property]);
82+
$values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value, $formats[$property]);
11083
}
11184
}
11285
}
@@ -136,31 +109,21 @@ public static function toPathValue(string $value): string
136109
* If it's a string, pass through unchanged. It will be url-encoded
137110
* later.
138111
*
139-
* @param \DateTime|string|string[] $object an object to be serialized to a string
140-
* @param string|null $format the format of the parameter
112+
* @param DateTime|string|string[] $object an object to be serialized to a string
113+
* @param string|null $format the format of the parameter
141114
*
142115
* @return array|string the serialized object
143116
*/
144-
public static function toQueryValue(array|bool|\DateTime|string $object, ?string $format = null): array|string
117+
public static function toQueryValue(array|bool|DateTime|string $object, ?string $format = null): array|string
145118
{
146119
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-
}
120+
return match ($format) {
121+
'multi' => $object,
122+
'ssv' => implode(' ', $object),
123+
'tsv' => implode("\t", $object),
124+
'pipes' => implode('|', $object),
125+
default => implode(',', $object),
126+
};
164127
}
165128

166129
if (is_bool($object)) {
@@ -176,15 +139,15 @@ public static function toQueryValue(array|bool|\DateTime|string $object, ?string
176139
* If it's a datetime object, format it in RFC3339
177140
* If it's a date, format it in Y-m-d.
178141
*
179-
* @param \DateTime|string $value the value of the parameter
180-
* @param string|null $format the format of the parameter
142+
* @param DateTime|string $value the value of the parameter
143+
* @param string|null $format the format of the parameter
181144
*
182145
* @return string the header string
183146
*/
184-
public static function toString(\DateTime|string $value, ?string $format = null): string
147+
public static function toString(DateTime|string $value, ?string $format = null): string
185148
{
186-
if ($value instanceof \DateTime) {
187-
return ('date' === $format) ? $value->format('Y-m-d') : $value->format(\DateTime::ATOM);
149+
if ($value instanceof DateTime) {
150+
return ('date' === $format) ? $value->format('Y-m-d') : $value->format(DateTimeInterface::ATOM);
188151
}
189152

190153
return $value;
@@ -195,7 +158,7 @@ public static function toString(\DateTime|string $value, ?string $format = null)
195158
*
196159
* @param string $class class name is passed as a string
197160
*
198-
* @throws \Exception
161+
* @throws Exception
199162
*/
200163
public static function deserialize(ResponseInterface $response, string $class): mixed
201164
{
@@ -205,6 +168,11 @@ public static function deserialize(ResponseInterface $response, string $class):
205168
return self::mapToClass($data, $class, $response);
206169
}
207170

171+
/**
172+
* @throws \DateMalformedStringException
173+
* @throws SerializationException
174+
* @noinspection PhpFullyQualifiedNameUsageInspection
175+
*/
208176
protected static function mapToClass(mixed $data, string $class, ResponseInterface $response): mixed
209177
{
210178
if ('string' === gettype($data)) {
@@ -227,6 +195,13 @@ protected static function mapToClass(mixed $data, string $class, ResponseInterfa
227195
return $instance;
228196
}
229197

198+
/**
199+
* @throws \DateMalformedStringException
200+
* @throws SerializationException
201+
* @noinspection PhpMultipleClassDeclarationsInspection
202+
* @noinspection PhpUndefinedMethodInspection
203+
* @noinspection PhpFullyQualifiedNameUsageInspection
204+
*/
230205
protected static function castData(mixed $data, string $class, ResponseInterface $response): mixed
231206
{
232207
if (null === $data) {
@@ -248,14 +223,14 @@ protected static function castData(mixed $data, string $class, ResponseInterface
248223
if (0 === strcasecmp(substr($class, -2), '[]')) {
249224
$subClass = substr($class, 0, -2);
250225
$values = [];
251-
foreach ($data as $key => $value) {
226+
foreach ($data as $value) {
252227
$values[] = self::castData($value, $subClass, $response);
253228
}
254229

255230
return $values;
256231
}
257232
if ('mixed' === $class) {
258-
if ($data instanceof \stdClass) {
233+
if ($data instanceof stdClass) {
259234
if (empty(get_object_vars($data))) {
260235
return null;
261236
}
@@ -278,7 +253,7 @@ protected static function castData(mixed $data, string $class, ResponseInterface
278253
// be interpreted as a missing field/value. Let's handle
279254
// this graceful.
280255
if (!empty($data)) {
281-
return new \DateTime($data);
256+
return new DateTime($data);
282257
}
283258

284259
return null;
@@ -314,14 +289,12 @@ protected static function castData(mixed $data, string $class, ResponseInterface
314289
} elseif (enum_exists($class)) {
315290
try {
316291
return $class::from($data);
317-
} catch (\ValueError $e) {
292+
} catch (\ValueError) {
318293
$allowedValues = array_map(fn ($case) => $case->value, $class::cases());
319294
$imploded = implode("', '", $allowedValues);
320295

321-
throw new \InvalidArgumentException("Invalid value '{$data}' for enum '{$class}', must be one of: '{$imploded}'");
296+
throw new InvalidArgumentException("Invalid value '$data' for enum '$class', must be one of: '$imploded'");
322297
}
323-
324-
return $data;
325298
}
326299

327300
return self::mapToClass($data, $class, $response);

src/SerializationException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
namespace Fingerprint\ServerAPI;
44

55
use Psr\Http\Message\ResponseInterface;
6+
use Throwable;
67

78
final class SerializationException extends \Exception
89
{
910
protected ?ResponseInterface $response;
1011

11-
public function __construct(?ResponseInterface $response = null, ?\Exception $prev = null)
12+
public function __construct(?ResponseInterface $response = null, ?Throwable $prev = null)
1213
{
1314
parent::__construct("Response from the server couldn't be serialized", $prev ? $prev->getCode() : 0, $prev);
1415
$this->response = $response;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Fingerprint\ServerAPI;
4+
5+
use GuzzleHttp\Psr7\Response;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class SerializationExceptionTest extends TestCase
9+
{
10+
public function testSetResponseOverridesConstructorValue(): void
11+
{
12+
$initialResponse = new Response(400);
13+
$exception = new SerializationException($initialResponse);
14+
15+
$this->assertSame($initialResponse, $exception->getResponse());
16+
17+
$newResponse = new Response(500);
18+
$exception->setResponse($newResponse);
19+
20+
$this->assertSame($newResponse, $exception->getResponse());
21+
}
22+
23+
public function testSetResponseOnNullInitialResponse(): void
24+
{
25+
$exception = new SerializationException();
26+
27+
$this->assertNull($exception->getResponse());
28+
}
29+
}

0 commit comments

Comments
 (0)