From 8e050f32de84cf22f7dcaef6e50f98047421a80c Mon Sep 17 00:00:00 2001 From: Ilya Taratukhin Date: Wed, 30 Oct 2024 22:03:21 +0100 Subject: [PATCH] feat: use enums from PHP 8 instead of custom classes --- src/Model/BotdBotResult.php | 24 +++-------------- src/Model/ErrorCode.php | 51 +++++++++--------------------------- src/Model/VPNConfidence.php | 24 +++-------------- src/ObjectSerializer.php | 14 ++++++---- template/model_enum.mustache | 5 ++++ 5 files changed, 35 insertions(+), 83 deletions(-) create mode 100644 template/model_enum.mustache diff --git a/src/Model/BotdBotResult.php b/src/Model/BotdBotResult.php index 1ce8345..3f973e1 100644 --- a/src/Model/BotdBotResult.php +++ b/src/Model/BotdBotResult.php @@ -38,25 +38,9 @@ * * @see https://github.com/swagger-api/swagger-codegen */ -class BotdBotResult +enum BotdBotResult: string { - /** - * Possible values of this enum. - */ - public const NOT_DETECTED = 'notDetected'; - public const GOOD = 'good'; - public const BAD = 'bad'; - - /** - * Gets allowable values of the enum. - * - * @return string[] - */ - public static function getAllowableEnumValues() - { - return [ - self::NOT_DETECTED, - self::GOOD, - self::BAD, ]; - } + case NOT_DETECTED = 'notDetected'; + case GOOD = 'good'; + case BAD = 'bad'; } diff --git a/src/Model/ErrorCode.php b/src/Model/ErrorCode.php index 64157f6..5056269 100644 --- a/src/Model/ErrorCode.php +++ b/src/Model/ErrorCode.php @@ -38,43 +38,18 @@ * * @see https://github.com/swagger-api/swagger-codegen */ -class ErrorCode +enum ErrorCode: string { - /** - * Possible values of this enum. - */ - public const REQUEST_CANNOT_BE_PARSED = 'RequestCannotBeParsed'; - public const TOKEN_REQUIRED = 'TokenRequired'; - public const TOKEN_NOT_FOUND = 'TokenNotFound'; - public const SUBSCRIPTION_NOT_ACTIVE = 'SubscriptionNotActive'; - public const WRONG_REGION = 'WrongRegion'; - public const FEATURE_NOT_ENABLED = 'FeatureNotEnabled'; - public const REQUEST_NOT_FOUND = 'RequestNotFound'; - public const VISITOR_NOT_FOUND = 'VisitorNotFound'; - public const TOO_MANY_REQUESTS = 'TooManyRequests'; - public const _429_TOO_MANY_REQUESTS = '429 Too Many Requests'; - public const STATE_NOT_READY = 'StateNotReady'; - public const FAILED = 'Failed'; - - /** - * Gets allowable values of the enum. - * - * @return string[] - */ - public static function getAllowableEnumValues() - { - return [ - self::REQUEST_CANNOT_BE_PARSED, - self::TOKEN_REQUIRED, - self::TOKEN_NOT_FOUND, - self::SUBSCRIPTION_NOT_ACTIVE, - self::WRONG_REGION, - self::FEATURE_NOT_ENABLED, - self::REQUEST_NOT_FOUND, - self::VISITOR_NOT_FOUND, - self::TOO_MANY_REQUESTS, - self::_429_TOO_MANY_REQUESTS, - self::STATE_NOT_READY, - self::FAILED, ]; - } + case REQUEST_CANNOT_BE_PARSED = 'RequestCannotBeParsed'; + case TOKEN_REQUIRED = 'TokenRequired'; + case TOKEN_NOT_FOUND = 'TokenNotFound'; + case SUBSCRIPTION_NOT_ACTIVE = 'SubscriptionNotActive'; + case WRONG_REGION = 'WrongRegion'; + case FEATURE_NOT_ENABLED = 'FeatureNotEnabled'; + case REQUEST_NOT_FOUND = 'RequestNotFound'; + case VISITOR_NOT_FOUND = 'VisitorNotFound'; + case TOO_MANY_REQUESTS = 'TooManyRequests'; + case _429_TOO_MANY_REQUESTS = '429 Too Many Requests'; + case STATE_NOT_READY = 'StateNotReady'; + case FAILED = 'Failed'; } diff --git a/src/Model/VPNConfidence.php b/src/Model/VPNConfidence.php index cc2d2c1..2ca6711 100644 --- a/src/Model/VPNConfidence.php +++ b/src/Model/VPNConfidence.php @@ -38,25 +38,9 @@ * * @see https://github.com/swagger-api/swagger-codegen */ -class VPNConfidence +enum VPNConfidence: string { - /** - * Possible values of this enum. - */ - public const LOW = 'low'; - public const MEDIUM = 'medium'; - public const HIGH = 'high'; - - /** - * Gets allowable values of the enum. - * - * @return string[] - */ - public static function getAllowableEnumValues() - { - return [ - self::LOW, - self::MEDIUM, - self::HIGH, ]; - } + case LOW = 'low'; + case MEDIUM = 'medium'; + case HIGH = 'high'; } diff --git a/src/ObjectSerializer.php b/src/ObjectSerializer.php index 7ef0e85..d98c14c 100644 --- a/src/ObjectSerializer.php +++ b/src/ObjectSerializer.php @@ -260,11 +260,15 @@ protected static function castData(mixed $data, string $class, ResponseInterface } throw new SerializationException($response); - } elseif (method_exists($class, 'getAllowableEnumValues')) { - if (!in_array($data, $class::getAllowableEnumValues())) { - $imploded = implode("', '", $class::getAllowableEnumValues()); - - throw new \InvalidArgumentException("Invalid value for enum '{$class}', must be one of: '{$imploded}'"); + } elseif (enum_exists($class)) { + try { + $enumInstance = $class::from($data); + return $enumInstance; + } catch (\ValueError $e) { + $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}'"); } return $data; diff --git a/template/model_enum.mustache b/template/model_enum.mustache new file mode 100644 index 0000000..b778215 --- /dev/null +++ b/template/model_enum.mustache @@ -0,0 +1,5 @@ +enum {{classname}}: string +{ + {{#allowableValues}}{{#enumVars}}case {{{name}}} = {{{value}}}; + {{/enumVars}}{{/allowableValues}} +}