Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 46 additions & 73 deletions src/ObjectSerializer.php
Original file line number Diff line number Diff line change
@@ -1,63 +1,36 @@
<?php

/**
* ObjectSerializer.
*
* PHP version 5
*
* @category Class
*
* @author Swagger Codegen team
*
* @see https://github.com/swagger-api/swagger-codegen
*/

/**
* Fingerprint Pro Server API.
*
* 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.
*
* OpenAPI spec version: 3
* Contact: support@fingerprint.com
* Generated by: https://github.com/swagger-api/swagger-codegen.git
* Swagger Codegen version: 3.0.34
*/
/**
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen
* Do not edit the class manually.
*/

namespace Fingerprint\ServerAPI;

use DateTime;
use DateTimeInterface;
use Exception;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use stdClass;

/**
* ObjectSerializer Class Doc Comment.
*
* @category Class
*
* @author Swagger Codegen team
*
* @see https://github.com/swagger-api/swagger-codegen
*/
class ObjectSerializer
{
/**
* Serialize data.
*
* @param mixed $data the data to serialize
* @param mixed $data the data to serialize
* @param string|null $format the format of the Swagger type of the data
*
* @return array|object|string serialized form of $data
* @return array|bool|object|string serialized form of $data
*/
public static function sanitizeForSerialization(mixed $data, ?string $format = null): array|bool|object|string
{
if (is_scalar($data) || null === $data) {
return $data;
}
if ($data instanceof \DateTime) {
return ('date' === $format) ? $data->format('Y-m-d') : $data->format(\DateTime::ATOM);
if ($data instanceof DateTime) {
return ('date' === $format) ? $data->format('Y-m-d') : $data->format(DateTimeInterface::ATOM);
}
if (is_array($data)) {
foreach ($data as $property => $value) {
Expand All @@ -70,7 +43,7 @@ public static function sanitizeForSerialization(mixed $data, ?string $format = n

return $data;
}
if ($data instanceof \stdClass) {
if ($data instanceof stdClass) {
foreach ($data as $property => $value) {
if (is_scalar($value)) {
$data->{$property} = $value;
Expand Down Expand Up @@ -98,15 +71,15 @@ public static function sanitizeForSerialization(mixed $data, ?string $format = n
&& !in_array($value, $swaggerType::getAllowableEnumValues())) {
$imploded = implode("', '", $swaggerType::getAllowableEnumValues());

throw new \InvalidArgumentException("Invalid value for enum '{$swaggerType}', must be one of: '{$imploded}'");
throw new InvalidArgumentException("Invalid value for enum '$swaggerType', must be one of: '$imploded'");
}
if (null !== $value) {
if (is_scalar($value)) {
$values[$data::attributeMap()[$property]] = $value;
} elseif ('tag' === $property && empty($value)) {
$values[$data::attributeMap()[$property]] = new \stdClass();
$values[$data::attributeMap()[$property]] = new stdClass();
} else {
$values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value, $swaggerType, $formats[$property]);
$values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value, $formats[$property]);
}
}
}
Expand Down Expand Up @@ -136,31 +109,21 @@ public static function toPathValue(string $value): string
* If it's a string, pass through unchanged. It will be url-encoded
* later.
*
* @param \DateTime|string|string[] $object an object to be serialized to a string
* @param string|null $format the format of the parameter
* @param DateTime|string|string[] $object an object to be serialized to a string
* @param string|null $format the format of the parameter
*
* @return array|string the serialized object
*/
public static function toQueryValue(array|bool|\DateTime|string $object, ?string $format = null): array|string
public static function toQueryValue(array|bool|DateTime|string $object, ?string $format = null): array|string
{
if (is_array($object)) {
switch ($format) {
case 'multi':
return $object;

case 'ssv':
return implode(' ', $object);

case 'tsv':
return implode("\t", $object);

case 'pipes':
return implode('|', $object);

case 'csv':
default:
return implode(',', $object);
}
return match ($format) {
'multi' => $object,
'ssv' => implode(' ', $object),
'tsv' => implode("\t", $object),
'pipes' => implode('|', $object),
default => implode(',', $object),
};
}

if (is_bool($object)) {
Expand All @@ -176,15 +139,15 @@ public static function toQueryValue(array|bool|\DateTime|string $object, ?string
* If it's a datetime object, format it in RFC3339
* If it's a date, format it in Y-m-d.
*
* @param \DateTime|string $value the value of the parameter
* @param string|null $format the format of the parameter
* @param DateTime|string $value the value of the parameter
* @param string|null $format the format of the parameter
*
* @return string the header string
*/
public static function toString(\DateTime|string $value, ?string $format = null): string
public static function toString(DateTime|string $value, ?string $format = null): string
{
if ($value instanceof \DateTime) {
return ('date' === $format) ? $value->format('Y-m-d') : $value->format(\DateTime::ATOM);
if ($value instanceof DateTime) {
return ('date' === $format) ? $value->format('Y-m-d') : $value->format(DateTimeInterface::ATOM);
}

return $value;
Expand All @@ -195,7 +158,7 @@ public static function toString(\DateTime|string $value, ?string $format = null)
*
* @param string $class class name is passed as a string
*
* @throws \Exception
* @throws Exception
*/
public static function deserialize(ResponseInterface $response, string $class): mixed
{
Expand All @@ -205,6 +168,11 @@ public static function deserialize(ResponseInterface $response, string $class):
return self::mapToClass($data, $class, $response);
}

/**
* @throws \DateMalformedStringException
* @throws SerializationException
* @noinspection PhpFullyQualifiedNameUsageInspection
*/
protected static function mapToClass(mixed $data, string $class, ResponseInterface $response): mixed
{
if ('string' === gettype($data)) {
Expand All @@ -227,6 +195,13 @@ protected static function mapToClass(mixed $data, string $class, ResponseInterfa
return $instance;
}

/**
* @throws \DateMalformedStringException
* @throws SerializationException
* @noinspection PhpMultipleClassDeclarationsInspection
* @noinspection PhpUndefinedMethodInspection
* @noinspection PhpFullyQualifiedNameUsageInspection
*/
protected static function castData(mixed $data, string $class, ResponseInterface $response): mixed
{
if (null === $data) {
Expand All @@ -248,14 +223,14 @@ protected static function castData(mixed $data, string $class, ResponseInterface
if (0 === strcasecmp(substr($class, -2), '[]')) {
$subClass = substr($class, 0, -2);
$values = [];
foreach ($data as $key => $value) {
foreach ($data as $value) {
$values[] = self::castData($value, $subClass, $response);
}

return $values;
}
if ('mixed' === $class) {
if ($data instanceof \stdClass) {
if ($data instanceof stdClass) {
if (empty(get_object_vars($data))) {
return null;
}
Expand All @@ -278,7 +253,7 @@ protected static function castData(mixed $data, string $class, ResponseInterface
// be interpreted as a missing field/value. Let's handle
// this graceful.
if (!empty($data)) {
return new \DateTime($data);
return new DateTime($data);
}

return null;
Expand Down Expand Up @@ -314,14 +289,12 @@ protected static function castData(mixed $data, string $class, ResponseInterface
} elseif (enum_exists($class)) {
try {
return $class::from($data);
} catch (\ValueError $e) {
} catch (\ValueError) {
$allowedValues = array_map(fn ($case) => $case->value, $class::cases());
$imploded = implode("', '", $allowedValues);

throw new \InvalidArgumentException("Invalid value '{$data}' for enum '{$class}', must be one of: '{$imploded}'");
throw new InvalidArgumentException("Invalid value '$data' for enum '$class', must be one of: '$imploded'");
}

return $data;
}

return self::mapToClass($data, $class, $response);
Expand Down
3 changes: 2 additions & 1 deletion src/SerializationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace Fingerprint\ServerAPI;

use Psr\Http\Message\ResponseInterface;
use Throwable;

final class SerializationException extends \Exception
{
protected ?ResponseInterface $response;

public function __construct(?ResponseInterface $response = null, ?\Exception $prev = null)
public function __construct(?ResponseInterface $response = null, ?Throwable $prev = null)
{
parent::__construct("Response from the server couldn't be serialized", $prev ? $prev->getCode() : 0, $prev);
$this->response = $response;
Expand Down
29 changes: 29 additions & 0 deletions test/SerializationExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Fingerprint\ServerAPI;

use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;

class SerializationExceptionTest extends TestCase
{
public function testSetResponseOverridesConstructorValue(): void
{
$initialResponse = new Response(400);
$exception = new SerializationException($initialResponse);

$this->assertSame($initialResponse, $exception->getResponse());

$newResponse = new Response(500);
$exception->setResponse($newResponse);

$this->assertSame($newResponse, $exception->getResponse());
}

public function testSetResponseOnNullInitialResponse(): void
{
$exception = new SerializationException();

$this->assertNull($exception->getResponse());
}
}
Loading