Skip to content

Commit b5c2dfe

Browse files
author
jiannei
committed
feat: http status code enum
1 parent d948ce5 commit b5c2dfe

File tree

3 files changed

+79
-55
lines changed

3 files changed

+79
-55
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
4+
namespace Jiannei\Response\Laravel\Repositories\Enums;
5+
6+
7+
use Jiannei\Enum\Laravel\Contracts\LocalizedEnumContract;
8+
use Jiannei\Enum\Laravel\Enum;
9+
10+
class HttpStatusCodeEnum extends Enum implements LocalizedEnumContract
11+
{
12+
const HTTP_CONTINUE = 100;
13+
const HTTP_SWITCHING_PROTOCOLS = 101;
14+
const HTTP_PROCESSING = 102; // RFC2518
15+
const HTTP_EARLY_HINTS = 103; // RFC8297
16+
const HTTP_OK = 200;
17+
const HTTP_CREATED = 201;
18+
const HTTP_ACCEPTED = 202;
19+
const HTTP_NON_AUTHORITATIVE_INFORMATION = 203;
20+
const HTTP_NO_CONTENT = 204;
21+
const HTTP_RESET_CONTENT = 205;
22+
const HTTP_PARTIAL_CONTENT = 206;
23+
const HTTP_MULTI_STATUS = 207; // RFC4918
24+
const HTTP_ALREADY_REPORTED = 208; // RFC5842
25+
const HTTP_IM_USED = 226; // RFC3229
26+
const HTTP_MULTIPLE_CHOICES = 300;
27+
const HTTP_MOVED_PERMANENTLY = 301;
28+
const HTTP_FOUND = 302;
29+
const HTTP_SEE_OTHER = 303;
30+
const HTTP_NOT_MODIFIED = 304;
31+
const HTTP_USE_PROXY = 305;
32+
const HTTP_RESERVED = 306;
33+
const HTTP_TEMPORARY_REDIRECT = 307;
34+
const HTTP_PERMANENTLY_REDIRECT = 308; // RFC7238
35+
const HTTP_BAD_REQUEST = 400;
36+
const HTTP_UNAUTHORIZED = 401;
37+
const HTTP_PAYMENT_REQUIRED = 402;
38+
const HTTP_FORBIDDEN = 403;
39+
const HTTP_NOT_FOUND = 404;
40+
const HTTP_METHOD_NOT_ALLOWED = 405;
41+
const HTTP_NOT_ACCEPTABLE = 406;
42+
const HTTP_PROXY_AUTHENTICATION_REQUIRED = 407;
43+
const HTTP_REQUEST_TIMEOUT = 408;
44+
const HTTP_CONFLICT = 409;
45+
const HTTP_GONE = 410;
46+
const HTTP_LENGTH_REQUIRED = 411;
47+
const HTTP_PRECONDITION_FAILED = 412;
48+
const HTTP_REQUEST_ENTITY_TOO_LARGE = 413;
49+
const HTTP_REQUEST_URI_TOO_LONG = 414;
50+
const HTTP_UNSUPPORTED_MEDIA_TYPE = 415;
51+
const HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
52+
const HTTP_EXPECTATION_FAILED = 417;
53+
const HTTP_I_AM_A_TEAPOT = 418; // RFC2324
54+
const HTTP_MISDIRECTED_REQUEST = 421; // RFC7540
55+
const HTTP_UNPROCESSABLE_ENTITY = 422; // RFC4918
56+
const HTTP_LOCKED = 423; // RFC4918
57+
const HTTP_FAILED_DEPENDENCY = 424; // RFC4918
58+
const HTTP_TOO_EARLY = 425; // RFC-ietf-httpbis-replay-04
59+
const HTTP_UPGRADE_REQUIRED = 426; // RFC2817
60+
const HTTP_PRECONDITION_REQUIRED = 428; // RFC6585
61+
const HTTP_TOO_MANY_REQUESTS = 429; // RFC6585
62+
const HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431; // RFC6585
63+
const HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451;
64+
const HTTP_INTERNAL_SERVER_ERROR = 500;
65+
const HTTP_NOT_IMPLEMENTED = 501;
66+
const HTTP_BAD_GATEWAY = 502;
67+
const HTTP_SERVICE_UNAVAILABLE = 503;
68+
const HTTP_GATEWAY_TIMEOUT = 504;
69+
const HTTP_VERSION_NOT_SUPPORTED = 505;
70+
const HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL = 506; // RFC2295
71+
const HTTP_INSUFFICIENT_STORAGE = 507; // RFC4918
72+
const HTTP_LOOP_DETECTED = 508; // RFC5842
73+
const HTTP_NOT_EXTENDED = 510; // RFC2774
74+
const HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511; // RFC6585
75+
}

src/Repositories/Enums/ResponseCodeEnum.php

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,59 +11,7 @@
1111

1212
namespace Jiannei\Response\Laravel\Repositories\Enums;
1313

14-
use Illuminate\Http\Response as HttpResponse;
15-
use Illuminate\Support\Facades\Lang;
16-
use Jiannei\Enum\Laravel\Contracts\LocalizedEnumContract;
17-
use Jiannei\Enum\Laravel\Enum;
18-
use ReflectionClass;
19-
use ReflectionException;
20-
21-
class ResponseCodeEnum extends Enum implements LocalizedEnumContract
14+
abstract class ResponseCodeEnum extends HttpStatusCodeEnum
2215
{
23-
/**
24-
* Get localized description.
25-
*
26-
* @param $value
27-
* @return string|null
28-
*/
29-
protected static function getLocalizedDescription($value): ?string
30-
{
31-
if (static::isLocalizable()) {
32-
$localizedStringKey = static::getLocalizationKey().'.'.$value;
33-
if (Lang::has($localizedStringKey)) {
34-
return Lang::get($localizedStringKey);
35-
}
36-
}
37-
38-
return HttpResponse::$statusTexts[$value] ?? null;
39-
}
40-
41-
/**
42-
* Get all of the constants defined on the class.
43-
*
44-
* @return array
45-
* @throws ReflectionException
46-
*/
47-
protected static function getConstants(): array
48-
{
49-
$calledClass = static::class;
50-
if (! array_key_exists($calledClass, static::$cache)) {
51-
$reflect = new ReflectionClass($calledClass);
52-
static::$cache[$calledClass] = array_merge(self::getHttpConstants(), $reflect->getConstants());
53-
}
54-
55-
return static::$cache[$calledClass];
56-
}
57-
58-
/**
59-
* Get HTTP standard status code.
60-
*
61-
* @return array
62-
*/
63-
protected static function getHttpConstants(): array
64-
{
65-
$reflect = new ReflectionClass(HttpResponse::class);
66-
67-
return $reflect->getConstants();
68-
}
16+
const CLIENT_VALIDATION_ERROR = 422001;// 表单验证错误
6917
}

src/ResponseTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use ErrorException;
1515
use Illuminate\Http\Exceptions\HttpResponseException;
1616
use Illuminate\Http\Request;
17+
use Jiannei\Response\Laravel\Repositories\Enums\ResponseCodeEnum;
1718
use Throwable;
1819

1920
/**
@@ -78,6 +79,6 @@ protected function buildFailedValidationResponse(Request $request, array $errors
7879
return (static::$responseBuilder)($request, $errors);
7980
}
8081

81-
return $this->response->fail('Validation error', 422, $errors);
82+
return $this->response->fail('', ResponseCodeEnum::CLIENT_VALIDATION_ERROR, $errors);
8283
}
8384
}

0 commit comments

Comments
 (0)