1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use MelquesPaiva\RestResponse\HttpResponse\Error; |
|
|
|
|
4
|
|
|
use MelquesPaiva\RestResponse\Response; |
5
|
|
|
|
6
|
|
|
require __DIR__ . '/../vendor/autoload.php'; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* ErrorExample |
10
|
|
|
*/ |
11
|
|
|
class ErrorExample |
12
|
|
|
{ |
13
|
|
|
/** @var Response $response */ |
14
|
|
|
protected Response $response; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* ErrorExample Constructor |
18
|
|
|
*/ |
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
$this->response = new Response(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Generate a 400 response |
26
|
|
|
* |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
|
|
public function badRequest(): void |
30
|
|
|
{ |
31
|
|
|
$this->response->badRequest("The data passed to this request is not valid", "parameter_if_necessary"); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Generate a 401 response |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public function unauthorized(): void |
40
|
|
|
{ |
41
|
|
|
$this->response->unauthorized( |
42
|
|
|
"You don't have authorization or you aren't authenticate to access this method", |
43
|
|
|
"parameter_if_necessary" |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Generate a 403 response |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function actionForbidden(): void |
53
|
|
|
{ |
54
|
|
|
$this->response->actionForbidden( |
55
|
|
|
"The server understood the request, but you can't receive a succesfull response" |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Generate a 404 response |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function notFound(): void |
65
|
|
|
{ |
66
|
|
|
$this->response->notFound("No information founded", "parameter_if_necessary"); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Generate a 405 response |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
|
public function methodNotAllowed(): void |
75
|
|
|
{ |
76
|
|
|
$this->response->methodNotAllowed( |
77
|
|
|
"The method with you trying to access this method is not allowed", |
78
|
|
|
"parameter_if_necessary" |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Generate a 500 response |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
public function internalError(): void |
88
|
|
|
{ |
89
|
|
|
$this->response->internalError("Some errors occurred on the server side"); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Generate a 501 response |
94
|
|
|
* |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
public function methodNotImplemented(): void |
98
|
|
|
{ |
99
|
|
|
$this->response->methodNotImplemented("This method still isn't impelemented"); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Method to simulate other error responses |
104
|
|
|
* |
105
|
|
|
* @param integer $statusCode |
106
|
|
|
* @param string $message |
107
|
|
|
* @param string $type |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
|
|
public function otherReturn(int $statusCode, string $message, string $type): void |
111
|
|
|
{ |
112
|
|
|
$error = new Error(); |
113
|
|
|
$error->setStatusCode($statusCode) |
114
|
|
|
->setMessage($message) |
115
|
|
|
->setType($type); |
116
|
|
|
|
117
|
|
|
echo $this->response->errorResponse($error); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// $param = $_GET['param']; |
122
|
|
|
// switch($param) { |
123
|
|
|
// case "bad_request": |
124
|
|
|
// (new ErrorExample())->badRequest(); |
125
|
|
|
// break; |
126
|
|
|
// case "unauthorized": |
127
|
|
|
// (new ErrorExample())->unauthorized(); |
128
|
|
|
// break; |
129
|
|
|
// case "action_forbidden": |
130
|
|
|
// (new ErrorExample())->actionForbidden(); |
131
|
|
|
// break; |
132
|
|
|
// case "not_found": |
133
|
|
|
// (new ErrorExample())->notFound(); |
134
|
|
|
// break; |
135
|
|
|
// case "method_not_allowed": |
136
|
|
|
// (new ErrorExample())->methodNotAllowed(); |
137
|
|
|
// break; |
138
|
|
|
// case "internal_error": |
139
|
|
|
// (new ErrorExample())->internalError(); |
140
|
|
|
// break; |
141
|
|
|
// case "method_not_implemented": |
142
|
|
|
// (new ErrorExample())->methodNotImplemented(); |
143
|
|
|
// break; |
144
|
|
|
// default: |
145
|
|
|
// (new ErrorExample())->otherReturn(402, "message", "type"); |
146
|
|
|
// break; |
147
|
|
|
// } |
148
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: