From af4c1ac0dc812e45555cd06f1d83ce203b931072 Mon Sep 17 00:00:00 2001 From: nejcc Date: Sun, 27 Oct 2024 18:02:16 +0100 Subject: [PATCH] update enum http status code --- Tests/HttpStatusCodeTest.php | 95 ++++++++++++ build/logs/junit.xml | 248 ++++++++++++++++++++++++++++++ examples/enums.php | 40 +++++ src/Enums/Http/HttpStatusCode.php | 184 ++++++++++++++++++++++ 4 files changed, 567 insertions(+) create mode 100644 Tests/HttpStatusCodeTest.php create mode 100644 build/logs/junit.xml create mode 100644 examples/enums.php create mode 100644 src/Enums/Http/HttpStatusCode.php diff --git a/Tests/HttpStatusCodeTest.php b/Tests/HttpStatusCodeTest.php new file mode 100644 index 0000000..2fe2645 --- /dev/null +++ b/Tests/HttpStatusCodeTest.php @@ -0,0 +1,95 @@ +assertTrue(HttpStatusCode::CONTINUE->isInformational()); + $this->assertFalse(HttpStatusCode::OK->isInformational()); + } + + public function testIsSuccess() + { + $this->assertTrue(HttpStatusCode::OK->isSuccess()); + $this->assertFalse(HttpStatusCode::BAD_REQUEST->isSuccess()); + } + + public function testIsRedirection() + { + $this->assertTrue(HttpStatusCode::MOVED_PERMANENTLY->isRedirection()); + $this->assertFalse(HttpStatusCode::NOT_FOUND->isRedirection()); + } + + public function testIsClientError() + { + $this->assertTrue(HttpStatusCode::NOT_FOUND->isClientError()); + $this->assertFalse(HttpStatusCode::OK->isClientError()); + } + + public function testIsServerError() + { + $this->assertTrue(HttpStatusCode::INTERNAL_SERVER_ERROR->isServerError()); + $this->assertFalse(HttpStatusCode::BAD_REQUEST->isServerError()); + } + + public function testGetMessage() + { + $this->assertEquals('OK', HttpStatusCode::OK->getMessage()); + $this->assertEquals('Not Found', HttpStatusCode::NOT_FOUND->getMessage()); + $this->assertEquals('Internal Server Error', HttpStatusCode::INTERNAL_SERVER_ERROR->getMessage()); + } + + public function testGetSuggestion() + { + $this->assertEquals( + 'Check the request parameters and try again.', + HttpStatusCode::BAD_REQUEST->getSuggestion() + ); + $this->assertEquals( + 'The server is currently down for maintenance. Try again later.', + HttpStatusCode::SERVICE_UNAVAILABLE->getSuggestion() + ); + $this->assertEquals( + 'No specific suggestion.', + HttpStatusCode::OK->getSuggestion() + ); + } + + public function testBuildResponse() + { + $response = HttpStatusCode::OK->buildResponse( + data: ['message' => 'Success'], + headers: ['Content-Type' => 'application/json'] + ); + + $expectedResponse = [ + 'status' => 200, + 'message' => 'OK', + 'data' => ['message' => 'Success'], + 'headers' => ['Content-Type' => 'application/json'], + ]; + + $this->assertEquals($expectedResponse, $response); + } + + public function testGetSuccessCodes() + { + $successCodes = HttpStatusCode::getSuccessCodes(); + $this->assertContains(HttpStatusCode::OK, $successCodes); + $this->assertNotContains(HttpStatusCode::BAD_REQUEST, $successCodes); + } + + public function testGetClientErrorCodes() + { + $clientErrorCodes = HttpStatusCode::getClientErrorCodes(); + $this->assertContains(HttpStatusCode::NOT_FOUND, $clientErrorCodes); + $this->assertNotContains(HttpStatusCode::INTERNAL_SERVER_ERROR, $clientErrorCodes); + } +} diff --git a/build/logs/junit.xml b/build/logs/junit.xml new file mode 100644 index 0000000..225927a --- /dev/null +++ b/build/logs/junit.xml @@ -0,0 +1,248 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/enums.php b/examples/enums.php new file mode 100644 index 0000000..1eb0e25 --- /dev/null +++ b/examples/enums.php @@ -0,0 +1,40 @@ +buildResponse( + data: ['user' => 'Lord Nejc'], + headers: ['Content-Type' => 'application/json'] +); +echo "
";
+print_r($response);
+echo "
"; +/* Output: +Array +( + [status] => 200 + [message] => OK + [data] => Array + ( + [user] => Lord Nejc + ) + [headers] => Array + ( + [Content-Type] => application/json + ) +) +*/ + +echo "
";
+$successCodes = HttpStatusCode::getSuccessCodes();
+foreach ($successCodes as $code) {
+    echo "{$code->value} - {$code->getMessage()}\n";
+}
+echo "
"; + + + +$status = HttpStatusCode::NOT_FOUND; +echo $status->getSuggestion(); // Output: "Verify the URL or resource identifier." \ No newline at end of file diff --git a/src/Enums/Http/HttpStatusCode.php b/src/Enums/Http/HttpStatusCode.php new file mode 100644 index 0000000..e18dc1e --- /dev/null +++ b/src/Enums/Http/HttpStatusCode.php @@ -0,0 +1,184 @@ +value >= 100 && $this->value < 200; + } + + /** + * Check if the status code indicates success. + */ + public function isSuccess(): bool + { + return $this->value >= 200 && $this->value < 300; + } + + /** + * Check if the status code is a redirection. + */ + public function isRedirection(): bool + { + return $this->value >= 300 && $this->value < 400; + } + + /** + * Check if the status code is a client error. + */ + public function isClientError(): bool + { + return $this->value >= 400 && $this->value < 500; + } + + /** + * Check if the status code is a server error. + */ + public function isServerError(): bool + { + return $this->value >= 500 && $this->value < 600; + } + + /** + * Get a message description for the HTTP status code. + */ + public function getMessage(): string + { + return match ($this) { + self::CONTINUE => 'Continue', + self::SWITCHING_PROTOCOLS => 'Switching Protocols', + self::PROCESSING => 'Processing', + self::EARLY_HINTS => 'Early Hints', + self::OK => 'OK', + self::CREATED => 'Created', + self::ACCEPTED => 'Accepted', + self::NON_AUTHORITATIVE_INFORMATION => 'Non-Authoritative Information', + self::NO_CONTENT => 'No Content', + self::RESET_CONTENT => 'Reset Content', + self::PARTIAL_CONTENT => 'Partial Content', + self::MULTIPLE_CHOICES => 'Multiple Choices', + self::MOVED_PERMANENTLY => 'Moved Permanently', + self::FOUND => 'Found', + self::SEE_OTHER => 'See Other', + self::NOT_MODIFIED => 'Not Modified', + self::TEMPORARY_REDIRECT => 'Temporary Redirect', + self::PERMANENT_REDIRECT => 'Permanent Redirect', + self::BAD_REQUEST => 'Bad Request', + self::UNAUTHORIZED => 'Unauthorized', + self::PAYMENT_REQUIRED => 'Payment Required', + self::FORBIDDEN => 'Forbidden', + self::NOT_FOUND => 'Not Found', + self::METHOD_NOT_ALLOWED => 'Method Not Allowed', + self::NOT_ACCEPTABLE => 'Not Acceptable', + self::REQUEST_TIMEOUT => 'Request Timeout', + self::CONFLICT => 'Conflict', + self::GONE => 'Gone', + self::UNPROCESSABLE_ENTITY => 'Unprocessable Entity', + self::TOO_MANY_REQUESTS => 'Too Many Requests', + self::INTERNAL_SERVER_ERROR => 'Internal Server Error', + self::NOT_IMPLEMENTED => 'Not Implemented', + self::BAD_GATEWAY => 'Bad Gateway', + self::SERVICE_UNAVAILABLE => 'Service Unavailable', + self::GATEWAY_TIMEOUT => 'Gateway Timeout', + self::HTTP_VERSION_NOT_SUPPORTED => 'HTTP Version Not Supported', + }; + } + + /** + * Get an appropriate suggestion based on the status code. + */ + public function getSuggestion(): string + { + return match ($this) { + self::BAD_REQUEST => 'Check the request parameters and try again.', + self::UNAUTHORIZED => 'Make sure you are authenticated.', + self::FORBIDDEN => 'You do not have permission to access this resource.', + self::NOT_FOUND => 'Verify the URL or resource identifier.', + self::INTERNAL_SERVER_ERROR => 'Try again later or contact support if the issue persists.', + self::SERVICE_UNAVAILABLE => 'The server is currently down for maintenance. Try again later.', + default => 'No specific suggestion.', + }; + } + + /** + * Create a standard HTTP response array. + */ + public function buildResponse(array $data = [], array $headers = []): array + { + return [ + 'status' => $this->value, + 'message' => $this->getMessage(), + 'data' => $data, + 'headers' => $headers, + ]; + } + + /** + * Static method to retrieve all successful status codes. + */ + public static function getSuccessCodes(): array + { + return array_filter(self::cases(), fn($case) => $case->isSuccess()); + } + + /** + * Static method to retrieve all client error status codes. + */ + public static function getClientErrorCodes(): array + { + return array_filter(self::cases(), fn($case) => $case->isClientError()); + } +}