From db9dd23f8839c35ecaedd50a0cb0f63642fb614f Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Thu, 10 Jun 2021 13:03:02 -0700 Subject: [PATCH 1/2] Conditional enum definition based on php version --- src/Enums/HTTPMethod.php | 36 +++++++-------------------------- src/Enums/HTTPMethodGTE81.php | 15 ++++++++++++++ src/Enums/HTTPMethodLTE80.php | 38 +++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 29 deletions(-) create mode 100644 src/Enums/HTTPMethodGTE81.php create mode 100644 src/Enums/HTTPMethodLTE80.php diff --git a/src/Enums/HTTPMethod.php b/src/Enums/HTTPMethod.php index 6fa438d..d8b3cbd 100644 --- a/src/Enums/HTTPMethod.php +++ b/src/Enums/HTTPMethod.php @@ -4,35 +4,13 @@ namespace Firehed\API\Enums; -use Firehed\Common\Enum; +use function class_alias; +use function version_compare; -/** - * Direct use of this class (via these static methods) is deprecated. Instead, - * implementations should rely on the Request traits. - * - * This will be converted to a native Enum in PHP 8.1. - * - * @method static HTTPMethod DELETE() - * @method static HTTPMethod GET() - * @method static HTTPMethod OPTIONS() - * @method static HTTPMethod PATCH() - * @method static HTTPMethod POST() - * @method static HTTPMethod PUT() - */ -class HTTPMethod extends Enum -{ +use const PHP_VERSION; - // Other methods exist, but these are the only relevant ones for RESTful - // APIs - const GET = 'GET'; - const PATCH = 'PATCH'; - const POST = 'POST'; - const PUT = 'PUT'; - const DELETE = 'DELETE'; - const OPTIONS = 'OPTIONS'; - - public function __toString() - { - return $this->getValue(); - } +if (version_compare(PHP_VERSION, '8.1.0') >= 0) { + class_alias(HTTPMethodGTE81::class, HTTPMethod::class); +} else { + class_alias(HTTPMethodLTE80::class, HTTPMethod::class); } diff --git a/src/Enums/HTTPMethodGTE81.php b/src/Enums/HTTPMethodGTE81.php new file mode 100644 index 0000000..aa2125c --- /dev/null +++ b/src/Enums/HTTPMethodGTE81.php @@ -0,0 +1,15 @@ +getValue(); + } +} From 3ee56b5ec64c6e9ade013647c7e0c5f8f6560799 Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Thu, 10 Jun 2021 13:07:42 -0700 Subject: [PATCH 2/2] Backed enums require explicit type --- src/Enums/HTTPMethodGTE81.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Enums/HTTPMethodGTE81.php b/src/Enums/HTTPMethodGTE81.php index aa2125c..3d78523 100644 --- a/src/Enums/HTTPMethodGTE81.php +++ b/src/Enums/HTTPMethodGTE81.php @@ -4,7 +4,7 @@ namespace Firehed\API\Enums; -enum HTTPMethodGTE81 +enum HTTPMethodGTE81: string { case GET = 'GET'; case PATCH = 'PATCH';