/
ApiProblem.php
561 lines (511 loc) · 15.7 KB
/
ApiProblem.php
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
<?php
/**
* This file is part of the ApiProblem library.
*
* (c) Larry Garfield <larry@garfieldtech.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Crell\ApiProblem
*/
namespace Crell\ApiProblem;
/**
* An API error of some form.
*
* This object generates errors in compliance with RFC 7807 "API Problem".
*
* This object should be configured via the appropriate methods, and then
* rendered using the asJson() or asXml() methods. The resulting string is
* safe to then send in response to an HTTP request. When sent, the response
* should have a mime type of application/problem+json or
* application/problem+xml, as appropriate.
*
* Subclassing this class to provide defaults for different problem types for
* your application is encouraged.
*
* For problem properties defined by the specification, use the methods provided
* to get/set those values. For extended values, use the ArrayAccess interface
* to specify arbitrary additional properties.
*
* @link http://tools.ietf.org/html/rfc7807
*
* @autor Larry Garfield
*/
class ApiProblem implements \ArrayAccess
{
/**
* The content type for a JSON based HTTP response carrying
* problem details.
*
* @var string
*/
const CONTENT_TYPE_JSON = 'application/problem+json';
/**
* The content type for a XML based HTTP response carrying
* problem details.
*
* @var string
*/
const CONTENT_TYPE_XML = 'application/problem+xml';
/**
* A short, human-readable summary of the problem type.
*
* It SHOULD NOT change from occurrence to occurrence of the problem,
* except for purposes of localization.
*
* @var string
*/
protected $title;
/**
* A URI reference (RFC3986) that identifies the problem type.
*
* This specification encourages that, when dereferenced, it provide
* human-readable documentation for the problem type (e.g., using HTML
* [W3C.REC-html5-20141028]). When this member is not present, its value
* is assumed to be "about:blank".
*
* Consumers MUST use the type string as the primary identifier for the
* problem type.
*
* This value may be an absolute or or relative URI. If relative, it MUST be
* resolved relative to the document's base URI, as per RFC3986, Section 5.
*
* @link http://tools.ietf.org/html/rfc3986
*
* @var string
*/
protected $type;
/**
* The HTTP status code set by the origin server for this occurrence of the problem.
*
* The status member, if present, is only advisory; it conveys the HTTP
* status code used for the convenience of the consumer. Generators MUST
* use the same status code in the actual HTTP response, to assure that
* generic HTTP software that does not understand this format still behaves
* correctly.
*
* @var int
*/
protected $status;
/**
* An human readable explanation specific to this occurrence of the problem.
*
* The "detail" member, if present, ought to focus on helping the client
* correct the problem, rather than giving debugging information.
*
* Consumers SHOULD NOT parse the "detail" member for information; extensions
* are more suitable and less error-prone ways to obtain such information.
*
* @var string
*/
protected $detail;
/**
* A URI reference that identifies the specific occurrence of the problem.
*
* It may or may not yield further information if dereferenced.
*
* This value may be an absolute or or relative URI. If relative, it MUST be
* resolved relative to the document's base URI, as per RFC3986, Section 5.
*
* @link http://tools.ietf.org/html/rfc3986
*
* @var string
*/
protected $instance;
/**
* Any arbitrary extension properties that have been assigned on this object.
*
* @var array
*/
protected $extensions = array();
/**
* Parses a JSON string into a Problem object.
*
* @param string $json
* The JSON string to parse.
* @return ApiProblem
* A newly constructed problem object.
*
* @throws JsonParseException
* Invalid JSON strings will result in a thrown exception.
*/
public static function fromJson($json)
{
if (empty($json)) {
throw (new JsonParseException('An empty string is not a valid JSON value', JSON_ERROR_SYNTAX))->setJson($json);
}
$parsed = json_decode($json, true);
switch (json_last_error()) {
case JSON_ERROR_NONE:
return static::decompile($parsed);
break;
case JSON_ERROR_DEPTH:
throw (new JsonParseException('Maximum stack depth exceeded', JSON_ERROR_DEPTH))->setJson($json);
break;
case JSON_ERROR_STATE_MISMATCH:
throw (new JsonParseException('Underflow or the modes mismatch', JSON_ERROR_STATE_MISMATCH))->setJson($json);
break;
case JSON_ERROR_CTRL_CHAR:
throw (new JsonParseException('Unexpected control character found', JSON_ERROR_CTRL_CHAR))->setJson($json);
break;
case JSON_ERROR_SYNTAX:
throw (new JsonParseException('Syntax error, malformed JSON', JSON_ERROR_SYNTAX))->setJson($json);
break;
case JSON_ERROR_UTF8:
throw (new JsonParseException('Malformed UTF-8 characters, possibly incorrectly encoded', JSON_ERROR_UTF8))->setJson($json);
break;
default:
throw (new JsonParseException('Unknown error'))->setJson($json);
break;
}
}
/**
* Converts a SimpleXMLElement to a nested array.
*
* @param \SimpleXMLElement $element
* The XML
* @return array
* A nested array corresponding to the XML element provided.
*/
protected static function xmlToArray(\SimpleXMLElement $element)
{
$data = (array)$element;
foreach ($data as $key => $value) {
if ($value instanceof \SimpleXMLElement) {
$data[$key] = static::xmlToArray($value);
}
}
return $data;
}
/**
* Parses an XML string into a Problem object.
*
* @param string $string
* The XML string to parse.
* @return ApiProblem
* A newly constructed problem object.
*/
public static function fromXml($string)
{
$xml = new \SimpleXMLElement($string);
$data = static::xmlToArray($xml);
return static::decompile($data);
}
/**
* Decompiles an array into an ApiProblem object.
*
* @param array $parsed
* An array parsed from JSON or XML to turn into an ApiProblem object.
* @return ApiProblem
* A new ApiProblem object.
*/
protected static function decompile(array $parsed)
{
$problem = new static();
if (!empty($parsed['title'])) {
$problem->setTitle($parsed['title']);
}
if (!empty($parsed['type'])) {
$problem->setType($parsed['type']);
}
if (!empty($parsed['status'])) {
$problem->setStatus($parsed['status']);
}
if (!empty($parsed['detail'])) {
$problem->setDetail($parsed['detail']);
}
if (!empty($parsed['instance'])) {
$problem->setInstance($parsed['instance']);
}
// Remove the defined keys. That means whatever is left must be a custom
// extension property.
unset($parsed['title'], $parsed['type'], $parsed['status'], $parsed['detail'], $parsed['instance']);
foreach ($parsed as $key => $value) {
$problem[$key] = $value;
}
return $problem;
}
/**
* Constructs a new ApiProblem.
*
* @param string $title
* A short, human-readable summary of the problem type. It SHOULD NOT
* change from occurrence to occurrence of the problem, except for
* purposes of localization.
* @param string $type
* An absolute URI (RFC3986) that identifies the problem type. When
* dereferenced, it SHOULD provide human-readable documentation for the
* problem type (e.g., using HTML).
*/
public function __construct($title = '', $type = 'about:blank')
{
if ($title) {
$this->title = $title;
}
if ($type) {
$this->type = $type;
}
}
/**
* Retrieves the title of the problem.
*
* @return string
* The current title.
*/
public function getTitle()
{
return $this->title;
}
/**
* Sets the title for this problem.
*
* @param string $title
* The title to set.
* @return ApiProblem
* The invoked object.
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Retrieves the problem type of this problem.
*
* @return string
* The problem type URI of this problem.
*/
public function getType()
{
return $this->type;
}
/**
* Sets the problem type of this problem.
*
* @param string $type
* The resolvable problem type URI of this problem.
* @return ApiProblem
* The invoked object.
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Retrieves the detail information of the problem.
*
* @return string
* The detail of this problem.
*/
public function getDetail()
{
return $this->detail;
}
/**
* Sets the detail for this problem.
*
* @param string $detail
* The human-readable detail string about this problem.
* @return ApiProblem
* The invoked object.
*/
public function setDetail($detail)
{
$this->detail = $detail;
return $this;
}
/**
* Returns the problem instance URI of this problem.
*
* @return string
* The problem instance URI of this problem.
*/
public function getInstance()
{
return $this->instance;
}
/**
* Sets the problem instance URI of this problem.
*
* @param string $instance
* An absolute URI that uniquely identifies this problem. It MAY link to
* further information about the error, but that is not required.
*
* @return ApiProblem
* The invoked object.
*/
public function setInstance($instance)
{
$this->instance = $instance;
return $this;
}
/**
* Returns the current HTTP status code.
*
* @return int
* The current HTTP status code. If not set, it will return 0.
*/
public function getStatus()
{
return $this->status ?: 0;
}
/**
* Sets the HTTP status code for this problem.
*
* It is an error for this value to be set to a different value than the
* actual HTTP response code.
*
* @param int $status
* A valid HTTP status code.
* @return ApiProblem
* The invoked object.
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Renders this problem as JSON.
*
* @param boolean $pretty
* Whether or not to pretty-print the JSON string for easier debugging.
* @return string
* A JSON string representing this problem.
*/
public function asJson($pretty = false)
{
$response = $this->compile();
$options = 0;
if ($pretty) {
$options = JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT;
}
return json_encode($response, $options);
}
/**
* Renders this problem as XML.
*
* @param boolean $pretty
* Whether or not to pretty-print the XML string for easier debugging.
* @return string
* An XML string representing this problem.
*/
public function asXml($pretty = false)
{
$doc = new \SimpleXMLElement('<problem></problem>');
$this->arrayToXml($this->compile(), $doc);
$dom = dom_import_simplexml($doc);
if ($pretty) {
$dom->ownerDocument->preserveWhiteSpace = false;
$dom->ownerDocument->formatOutput = true;
}
return $dom->ownerDocument->saveXML();
}
/**
* Renders this problem as a native PHP array.
*
* This is mostly useful for debugging, or for placing
* this problem response into, say, a Symfony JsonResponse object.
*
* @return array
* The API problem represented as an array.
*/
public function asArray()
{
return $this->compile();
}
/**
* Compiles the object down to an array format, suitable for serializing.
*
* @return array
* This object, rendered to an array.
*/
protected function compile()
{
// Start with any extensions, since that's already an array.
$response = $this->extensions;
// These properties are optional.
foreach (array('title', 'type', 'status', 'detail', 'instance') as $key) {
if (!empty($this->$key)) {
$response[$key] = $this->$key;
}
}
return $response;
}
/**
* Adds a nested array to a SimpleXML element.
*
* This method was shamelessly coped from the Nocarrier\Hal library:
*
* @link https://github.com/blongden/hal
*
* @param array $data
* The data to add to the element.
* @param \SimpleXmlElement $element
* The XML object to which to add data.
* @param mixed $parent
* Used for internal recursion only.
*/
protected function arrayToXml(array $data, \SimpleXmlElement $element, $parent = null)
{
foreach ($data as $key => $value) {
if (is_array($value)) {
if (!is_numeric($key)) {
if (count($value) > 0 && isset($value[0])) {
$this->arrayToXml($value, $element, $key);
} else {
$subnode = $element->addChild($key);
$this->arrayToXml($value, $subnode, $key);
}
} else {
$subnode = $element->addChild($parent);
$this->arrayToXml($value, $subnode, $parent);
}
} else {
if (!is_numeric($key)) {
if (substr($key, 0, 1) === '@') {
$element->addAttribute(substr($key, 1), $value);
} elseif ($key === 'value') {
$element->{0} = $value;
} elseif (is_bool($value)) {
$element->addChild($key, intval($value));
} else {
$element->addChild($key, htmlspecialchars($value, ENT_QUOTES));
}
} else {
$element->addChild($parent, htmlspecialchars($value, ENT_QUOTES));
}
}
}
}
/**
* {@inheritdoc}
*/
public function offsetExists($offset)
{
return array_key_exists($offset, $this->extensions);
}
/**
* {@inheritdoc}
*/
public function &offsetGet($offset)
{
return $this->extensions[$offset];
}
/**
* {@inheritdoc}
*/
public function offsetSet($offset, $value)
{
$this->extensions[$offset] = $value;
}
/**
* {@inheritdoc}
*/
public function offsetUnset($offset)
{
unset($this->extensions[$offset]);
}
}