Skip to content

Commit

Permalink
feat: use enums from PHP 8 instead of custom classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ilfa committed Oct 30, 2024
1 parent 5645bf0 commit 8e050f3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 83 deletions.
24 changes: 4 additions & 20 deletions src/Model/BotdBotResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
51 changes: 13 additions & 38 deletions src/Model/ErrorCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
24 changes: 4 additions & 20 deletions src/Model/VPNConfidence.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
14 changes: 9 additions & 5 deletions src/ObjectSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions template/model_enum.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
enum {{classname}}: string
{
{{#allowableValues}}{{#enumVars}}case {{{name}}} = {{{value}}};
{{/enumVars}}{{/allowableValues}}
}

0 comments on commit 8e050f3

Please sign in to comment.