Skip to content

Commit 3421562

Browse files
committed
ajout du trait DeterminesStatusCode
1 parent 6778b4e commit 3421562

2 files changed

Lines changed: 146 additions & 1 deletion

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* the LICENSE file that was distributed with this source code.
1010
*/
1111

12-
namespace BlitzPHP\Traits;
12+
namespace BlitzPHP\Traits\Http;
1313

1414
use BlitzPHP\Contracts\Http\StatusCode;
1515

Http/DeterminesStatusCode.php

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Blitz PHP framework.
5+
*
6+
* (c) 2022 Dimitri Sitchet Tomkeu <devcode.dst@gmail.com>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace BlitzPHP\Traits\Http;
13+
14+
use BlitzPHP\Contracts\Http\StatusCode;
15+
16+
/**
17+
* @credit <a href="http://laravel.com/">Laravel</a>
18+
*/
19+
trait DeterminesStatusCode
20+
{
21+
/**
22+
* Obtenez le code d'état de la réponse.
23+
*/
24+
abstract protected function status(): int;
25+
26+
/**
27+
* Déterminez si le code de réponse était la réponse 200 "OK".
28+
*/
29+
public function ok(): bool
30+
{
31+
return $this->status() === StatusCode::OK;
32+
}
33+
34+
/**
35+
* Déterminez si le code de réponse était la réponse 201 "Created".
36+
*/
37+
public function created(): bool
38+
{
39+
return $this->status() === StatusCode::CREATED;
40+
}
41+
42+
/**
43+
* Déterminez si le code de réponse était la réponse 202 "Accepted".
44+
*/
45+
public function accepted(): bool
46+
{
47+
return $this->status() === StatusCode::ACCEPTED;
48+
}
49+
50+
/**
51+
* Déterminez si le code de réponse était le code d'état donné et si le corps n'a pas de contenu.
52+
*/
53+
public function noContent(int $status = StatusCode::NO_CONTENT): bool
54+
{
55+
return $this->status() === $status && $this->body() === '';
56+
}
57+
58+
/**
59+
* Déterminez si le code de réponse était un 301 "Moved Permanently".
60+
*/
61+
public function movedPermanently(): bool
62+
{
63+
return $this->status() === StatusCode::MOVED_PERMANENTLY;
64+
}
65+
66+
/**
67+
* Déterminez si le code de réponse était une réponse 302 "Found".
68+
*/
69+
public function found(): bool
70+
{
71+
return $this->status() === StatusCode::FOUND;
72+
}
73+
74+
/**
75+
* Déterminez si la réponse était une réponse 400 "Bad Request".
76+
*/
77+
public function badRequest(): bool
78+
{
79+
return $this->status() === StatusCode::BAD_REQUEST;
80+
}
81+
82+
/**
83+
* Déterminez si la réponse était une réponse 401 "Unauthorized".
84+
*/
85+
public function unauthorized(): bool
86+
{
87+
return $this->status() === StatusCode::UNAUTHORIZED;
88+
}
89+
90+
/**
91+
* Déterminez si la réponse était une réponse 402 "Payment Required".
92+
*/
93+
public function paymentRequired(): bool
94+
{
95+
return $this->status() === StatusCode::PAYMENT_REQUIRED;
96+
}
97+
98+
/**
99+
* Déterminez si la réponse était une réponse 403 "Forbidden".
100+
*/
101+
public function forbidden(): bool
102+
{
103+
return $this->status() === StatusCode::FORBIDDEN;
104+
}
105+
106+
/**
107+
* Déterminez si la réponse était une réponse 404 "Not Found".
108+
*/
109+
public function notFound(): bool
110+
{
111+
return $this->status() === StatusCode::NOT_FOUND;
112+
}
113+
114+
/**
115+
* Déterminez si la réponse était une réponse 408 "Request Timeout".
116+
*/
117+
public function requestTimeout(): bool
118+
{
119+
return $this->status() === StatusCode::REQUEST_TIMEOUT;
120+
}
121+
122+
/**
123+
* Déterminez si la réponse était une réponse 409 "Conflict".
124+
*/
125+
public function conflict(): bool
126+
{
127+
return $this->status() === StatusCode::CONFLICT;
128+
}
129+
130+
/**
131+
* Déterminez si la réponse était une réponse 422 "Unprocessable Entity".
132+
*/
133+
public function unprocessableEntity(): bool
134+
{
135+
return $this->status() === StatusCode::UNPROCESSABLE_ENTITY;
136+
}
137+
138+
/**
139+
* Déterminez si la réponse était une réponse 429 "Too Many Requests".
140+
*/
141+
public function tooManyRequests(): bool
142+
{
143+
return $this->status() === StatusCode::TOO_MANY_REQUESTS;
144+
}
145+
}

0 commit comments

Comments
 (0)