-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConcreteHttpStatus.php
110 lines (90 loc) · 3.27 KB
/
ConcreteHttpStatus.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
declare(strict_types=1);
/**
* Copyright (c) 2024-2025 guanguans<ityaozm@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/guanguans/laravel-api-response
*/
namespace Guanguans\LaravelApiResponse\Concerns;
use Illuminate\Http\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
/**
* @see \Illuminate\Http\Client\Concerns\DeterminesStatusCode
*
* @mixin \Guanguans\LaravelApiResponse\ApiResponse
*/
trait ConcreteHttpStatus
{
public function ok(string $message = '', int $code = Response::HTTP_OK): JsonResponse
{
return $this->success(null, $message, $code);
}
public function created(mixed $data = null, string $message = '', ?string $location = null): JsonResponse
{
return $this->localize($data, $message, Response::HTTP_CREATED, $location);
}
public function accepted(mixed $data = null, string $message = '', ?string $location = null): JsonResponse
{
return $this->localize($data, $message, Response::HTTP_ACCEPTED, $location);
}
public function localize(mixed $data = null, string $message = '', int $code = Response::HTTP_OK, ?string $location = null): JsonResponse
{
return tap(
$this->success($data, $message, $code),
static function (JsonResponse $jsonResponse) use ($location): void {
$location and $jsonResponse->header('Location', $location);
}
);
}
public function noContent(string $message = ''): JsonResponse
{
return $this->success(null, $message, Response::HTTP_NO_CONTENT);
}
public function badRequest(string $message = ''): JsonResponse
{
return $this->error($message);
}
public function unauthorized(string $message = ''): JsonResponse
{
return $this->error($message, Response::HTTP_UNAUTHORIZED);
}
public function paymentRequired(string $message = ''): JsonResponse
{
return $this->error($message, Response::HTTP_PAYMENT_REQUIRED);
}
public function forbidden(string $message = ''): JsonResponse
{
return $this->error($message, Response::HTTP_FORBIDDEN);
}
public function notFound(string $message = ''): JsonResponse
{
return $this->error($message, Response::HTTP_NOT_FOUND);
}
public function methodNotAllowed(string $message = ''): JsonResponse
{
return $this->error($message, Response::HTTP_METHOD_NOT_ALLOWED);
}
public function requestTimeout(string $message = ''): JsonResponse
{
return $this->error($message, Response::HTTP_REQUEST_TIMEOUT);
}
public function conflict(string $message = ''): JsonResponse
{
return $this->error($message, Response::HTTP_CONFLICT);
}
public function teapot(string $message = ''): JsonResponse
{
return $this->error($message, Response::HTTP_I_AM_A_TEAPOT);
}
public function unprocessableEntity(string $message = ''): JsonResponse
{
return $this->error($message, Response::HTTP_UNPROCESSABLE_ENTITY);
}
public function tooManyRequests(string $message = ''): JsonResponse
{
return $this->error($message, Response::HTTP_TOO_MANY_REQUESTS);
}
}