-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathApiProblem.php
More file actions
267 lines (240 loc) · 6.4 KB
/
ApiProblem.php
File metadata and controls
267 lines (240 loc) · 6.4 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 8/03/16
* Time: 22:29.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Api\Problem;
use Exception;
use NilPortugues\Assert\Assert;
/**
* Object describing an API-Problem payload.
*/
class ApiProblem
{
const RFC2616 = 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html';
/**
* Title of the error.
*
* @var string
*/
protected $title;
/**
* URL describing the problem type; defaults to HTTP status codes.
*
* @var string
*/
protected $type = self::RFC2616;
/**
* Description of the specific problem.
*
* @var string
*/
protected $detail = '';
/**
* HTTP status for the error.
*
* @var int
*/
protected $status;
/**
* @var array
*/
protected $additionalDetails = [];
/**
* Status titles for common problems.
*
* @var array
*/
protected static $problemStatus = [
// CLIENT ERROR
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Time-out',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Large',
415 => 'Unsupported Media Type',
416 => 'Requested range not satisfiable',
417 => 'Expectation Failed',
418 => 'I\'m a teapot',
422 => 'Unprocessable Entity',
423 => 'Locked',
424 => 'Failed Dependency',
425 => 'Unordered Collection',
426 => 'Upgrade Required',
428 => 'Precondition Required',
429 => 'Too Many Requests',
431 => 'Request Header Fields Too Large',
// SERVER ERROR
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Time-out',
505 => 'HTTP Version not supported',
506 => 'Variant Also Negotiates',
507 => 'Insufficient Storage',
508 => 'Loop Detected',
511 => 'Network Authentication Required',
];
/**
* ApiProblem constructor.
*
* @param $status
* @param $detail
* @param string $title
* @param string $type
* @param array $additionalDetails
*/
public function __construct($status, $detail, $title = '', $type = '', array $additionalDetails = [])
{
$this->setTitle($title);
$this->setType($type);
$this->setDetail($detail);
$this->setStatus($status);
$this->setAdditionalDetails($additionalDetails);
}
/**
* @param Exception $exception
* @param string $title
* @param string $type
* @param array $additionalDetails
*
* @return ApiProblem
*/
public static function fromException(Exception $exception, $title = '', $type = '', array $additionalDetails = [])
{
return self::fromThrowable($exception, $title, $type, $additionalDetails);
}
/**
* @param Throwable $throwable
* @param string $title
* @param string $type
* @param array $additionalDetails
*
* @return ApiProblem
*/
public static function fromThrowable(Throwable $throwable, $title = '', $type = '', array $additionalDetails = []) {
$eCode = $exception->getCode();
$code = (!empty($eCode) && is_int($eCode)) ? $eCode : 500;
if (0 === strlen($title) && 0 === strlen($type) && array_key_exists($code, self::$problemStatus)) {
$title = (self::$problemStatus[$code]);
$type = self::RFC2616;
}
$detail = $exception->getMessage();
if (empty($detail)) {
$className = explode('\\', get_class($exception));
$detail = array_pop($className);
}
return new self($code, $detail, $title, $type, $additionalDetails);
}
/**
* Returns value for `additionalDetails`.
*
* @return array
*/
public function additionalDetails()
{
return $this->additionalDetails;
}
/**
* Sets the value for `additionalDetails` property.
*
* @param array $additionalDetails
*/
protected function setAdditionalDetails(array &$additionalDetails)
{
Assert::hasKeyFormat($additionalDetails, function ($key) {
Assert::isAlpha($key, 'Key requires [A-Za-z] characters only.');
});
$this->additionalDetails = $additionalDetails;
}
/**
* Returns value for `title`.
*
* @return string
*/
public function title()
{
return $this->title;
}
/**
* Sets the value for `title` property.
*
* @param string $title
*/
protected function setTitle($title)
{
$this->title = $title;
}
/**
* Returns value for `type`.
*
* @return string
*/
public function type()
{
return $this->type;
}
/**
* Sets the value for `type` property.
*
* @param string $type
*/
protected function setType($type)
{
$this->type = $type;
}
/**
* Returns value for `detail`.
*
* @return string
*/
public function detail()
{
return $this->detail;
}
/**
* Sets the value for `detail` property.
*
* @param string $detail
*/
protected function setDetail($detail)
{
Assert::notEmpty($detail, 'Detail field cannot be an empty string');
$this->detail = $detail;
}
/**
* Returns value for `status`.
*
* @return int
*/
public function status()
{
return $this->status;
}
/**
* Sets the value for `status` property.
*
* @param int $status
*/
protected function setStatus($status)
{
Assert::isInteger($status, 'Status must be an integer value');
Assert::contains(array_keys(self::$problemStatus), $status, false, 'Status must be a valid HTTP 4xx or 5xx value.');
$this->status = $status;
}
}