1 | <?php |
||
19 | class ApiProblem |
||
20 | { |
||
21 | const RFC2616 = 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html'; |
||
22 | |||
23 | /** |
||
24 | * Title of the error. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $title; |
||
29 | /** |
||
30 | * URL describing the problem type; defaults to HTTP status codes. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $type = self::RFC2616; |
||
35 | |||
36 | /** |
||
37 | * Description of the specific problem. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $detail = ''; |
||
42 | |||
43 | /** |
||
44 | * HTTP status for the error. |
||
45 | * |
||
46 | * @var int |
||
47 | */ |
||
48 | protected $status; |
||
49 | |||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $additionalDetails = []; |
||
54 | |||
55 | /** |
||
56 | * Status titles for common problems. |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | protected static $problemStatus = [ |
||
61 | // CLIENT ERROR |
||
62 | 400 => 'Bad Request', |
||
63 | 401 => 'Unauthorized', |
||
64 | 402 => 'Payment Required', |
||
65 | 403 => 'Forbidden', |
||
66 | 404 => 'Not Found', |
||
67 | 405 => 'Method Not Allowed', |
||
68 | 406 => 'Not Acceptable', |
||
69 | 407 => 'Proxy Authentication Required', |
||
70 | 408 => 'Request Time-out', |
||
71 | 409 => 'Conflict', |
||
72 | 410 => 'Gone', |
||
73 | 411 => 'Length Required', |
||
74 | 412 => 'Precondition Failed', |
||
75 | 413 => 'Request Entity Too Large', |
||
76 | 414 => 'Request-URI Too Large', |
||
77 | 415 => 'Unsupported Media Type', |
||
78 | 416 => 'Requested range not satisfiable', |
||
79 | 417 => 'Expectation Failed', |
||
80 | 418 => 'I\'m a teapot', |
||
81 | 422 => 'Unprocessable Entity', |
||
82 | 423 => 'Locked', |
||
83 | 424 => 'Failed Dependency', |
||
84 | 425 => 'Unordered Collection', |
||
85 | 426 => 'Upgrade Required', |
||
86 | 428 => 'Precondition Required', |
||
87 | 429 => 'Too Many Requests', |
||
88 | 431 => 'Request Header Fields Too Large', |
||
89 | // SERVER ERROR |
||
90 | 500 => 'Internal Server Error', |
||
91 | 501 => 'Not Implemented', |
||
92 | 502 => 'Bad Gateway', |
||
93 | 503 => 'Service Unavailable', |
||
94 | 504 => 'Gateway Time-out', |
||
95 | 505 => 'HTTP Version not supported', |
||
96 | 506 => 'Variant Also Negotiates', |
||
97 | 507 => 'Insufficient Storage', |
||
98 | 508 => 'Loop Detected', |
||
99 | 511 => 'Network Authentication Required', |
||
100 | ]; |
||
101 | |||
102 | /** |
||
103 | * ApiProblem constructor. |
||
104 | * |
||
105 | * @param $status |
||
106 | * @param $detail |
||
107 | * @param string $title |
||
108 | * @param string $type |
||
109 | * @param array $additionalDetails |
||
110 | */ |
||
111 | public function __construct($status, $detail, $title = '', $type = '', array $additionalDetails = []) |
||
119 | |||
120 | /** |
||
121 | * @param Exception $exception |
||
122 | * @param string $title |
||
123 | * @param string $type |
||
124 | * @param array $additionalDetails |
||
125 | * |
||
126 | * @return ApiProblem |
||
127 | */ |
||
128 | public static function fromException(Exception $exception, $title = '', $type = '', array $additionalDetails = []) |
||
146 | |||
147 | /** |
||
148 | * Returns value for `additionalDetails`. |
||
149 | * |
||
150 | * @return array |
||
151 | */ |
||
152 | public function additionalDetails() |
||
156 | |||
157 | /** |
||
158 | * Sets the value for `additionalDetails` property. |
||
159 | * |
||
160 | * @param array $additionalDetails |
||
161 | */ |
||
162 | protected function setAdditionalDetails(array &$additionalDetails) |
||
170 | |||
171 | /** |
||
172 | * Returns value for `title`. |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | public function title() |
||
180 | |||
181 | /** |
||
182 | * Sets the value for `title` property. |
||
183 | * |
||
184 | * @param string $title |
||
185 | */ |
||
186 | protected function setTitle($title) |
||
190 | |||
191 | /** |
||
192 | * Returns value for `type`. |
||
193 | * |
||
194 | * @return string |
||
195 | */ |
||
196 | public function type() |
||
200 | |||
201 | /** |
||
202 | * Sets the value for `type` property. |
||
203 | * |
||
204 | * @param string $type |
||
205 | */ |
||
206 | protected function setType($type) |
||
210 | |||
211 | /** |
||
212 | * Returns value for `detail`. |
||
213 | * |
||
214 | * @return string |
||
215 | */ |
||
216 | public function detail() |
||
220 | |||
221 | /** |
||
222 | * Sets the value for `detail` property. |
||
223 | * |
||
224 | * @param string $detail |
||
225 | */ |
||
226 | protected function setDetail($detail) |
||
232 | |||
233 | /** |
||
234 | * Returns value for `status`. |
||
235 | * |
||
236 | * @return int |
||
237 | */ |
||
238 | public function status() |
||
242 | |||
243 | /** |
||
244 | * Sets the value for `status` property. |
||
245 | * |
||
246 | * @param int $status |
||
247 | */ |
||
248 | protected function setStatus($status) |
||
255 | } |
||
256 |