1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Rest Response |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/MelquesPaiva/rest-response for the canonical source repository |
7
|
|
|
* @copyright Copyright (c) MelquesPaiva |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace MelquesPaiva\RestResponse\HttpResponse; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Error |
14
|
|
|
* @package MelquesPaiva\RestResponse\Rest |
15
|
|
|
*/ |
16
|
|
|
class Error extends HttpResponse |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The request could not be understood by the server |
20
|
|
|
* |
21
|
|
|
* @param string $message |
22
|
|
|
* @param string $parameter |
23
|
|
|
* @return $this |
24
|
|
|
*/ |
25
|
|
|
public function badRequest(string $message, string $parameter): Error |
26
|
|
|
{ |
27
|
|
|
$this->message = $message; |
28
|
|
|
$this->statusCode = 400; |
29
|
|
|
$this->type = "bad_request"; |
30
|
|
|
$this->parameter = $parameter; |
31
|
|
|
|
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Request requires user authentication |
37
|
|
|
* |
38
|
|
|
* @param string $parameter |
39
|
|
|
* @param string $message |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
|
|
public function unauthorized(string $message, string $parameter): Error |
43
|
|
|
{ |
44
|
|
|
$this->message = $message; |
45
|
|
|
$this->statusCode = 401; |
46
|
|
|
$this->type = "unauthorized"; |
47
|
|
|
$this->parameter = $parameter; |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The server refuse to fulfill the request |
54
|
|
|
* |
55
|
|
|
* @param string $message |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
|
|
public function actionForbidden(string $message): Error |
59
|
|
|
{ |
60
|
|
|
$this->message = $message; |
61
|
|
|
$this->statusCode = 403; |
62
|
|
|
$this->type = "action_forbidden"; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The server has not found anything matching the Request-URI |
69
|
|
|
* |
70
|
|
|
* @param string $message |
71
|
|
|
* @param string $parameter |
72
|
|
|
* @return Error |
73
|
|
|
*/ |
74
|
|
|
public function notFound(string $message, string $parameter): Error |
75
|
|
|
{ |
76
|
|
|
$this->message = $message; |
77
|
|
|
$this->statusCode = 404; |
78
|
|
|
$this->type = "not_found"; |
79
|
|
|
$this->parameter = $parameter; |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* The method specified in the Request is not allowed for the resource |
86
|
|
|
* |
87
|
|
|
* @param string $message |
88
|
|
|
* @param string $parameter |
89
|
|
|
* @return Error |
90
|
|
|
*/ |
91
|
|
|
public function methodNotAllowed(string $message, string $parameter): Error |
92
|
|
|
{ |
93
|
|
|
$this->message = $message; |
94
|
|
|
$this->statusCode = 405; |
95
|
|
|
$this->type = "method_not_allowed"; |
96
|
|
|
$this->parameter = $parameter; |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* The server encountered an unexpected condition |
103
|
|
|
* |
104
|
|
|
* @param string $message |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
|
|
public function internalError(string $message): Error |
108
|
|
|
{ |
109
|
|
|
$this->message = $message; |
110
|
|
|
$this->statusCode = 500; |
111
|
|
|
$this->type = "internal_error"; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* The server does not support the functionality required to fulfill the request |
118
|
|
|
* |
119
|
|
|
* @param string $message |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
|
|
public function notImplemented(string $message): Error |
123
|
|
|
{ |
124
|
|
|
$this->message = $message; |
125
|
|
|
$this->statusCode = 501; |
126
|
|
|
$this->type = "not_implemented"; |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
public function toArray(): array |
135
|
|
|
{ |
136
|
|
|
return [ |
137
|
|
|
'message' => $this->message, |
138
|
|
|
'type' => $this->type, |
139
|
|
|
'statusCode' => $this->statusCode, |
140
|
|
|
'parameter' => $this->parameter, |
141
|
|
|
'data' => $this->data |
142
|
|
|
]; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|