1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
4
|
|
|
* Date: 8/03/16 |
5
|
|
|
* Time: 23:34. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace NilPortugues\Api\Problem; |
12
|
|
|
|
13
|
|
|
use Exception; |
14
|
|
|
use GuzzleHttp\Psr7\Response; |
15
|
|
|
use NilPortugues\Api\Problem\Presenter\JsonPresenter; |
16
|
|
|
use NilPortugues\Api\Problem\Presenter\Presenter; |
17
|
|
|
use NilPortugues\Api\Problem\Presenter\XmlPresenter; |
18
|
|
|
|
19
|
|
|
class ApiProblemResponse extends Response |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* ApiProblemResponse constructor. |
23
|
|
|
* |
24
|
|
|
* @param Presenter $presenter |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Presenter $presenter) |
27
|
|
|
{ |
28
|
|
|
parent::__construct( |
29
|
|
|
$presenter->apiProblem()->status(), |
30
|
|
|
['Content-type' => $this->responseHeader($presenter)], |
31
|
|
|
$presenter->contents() |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Presenter $presenter |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
protected function responseHeader(Presenter $presenter) |
41
|
|
|
{ |
42
|
|
|
return ($presenter->format() === 'xml') ? 'application/problem+xml' : 'application/problem+json'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param $status |
47
|
|
|
* @param $detail |
48
|
|
|
* @param string $title |
49
|
|
|
* @param string $type |
50
|
|
|
* @param array $additionalDetails |
51
|
|
|
* |
52
|
|
|
* @return ApiProblemResponse |
53
|
|
|
*/ |
54
|
|
|
public static function json($status, $detail, $title = '', $type = '', array $additionalDetails = []) |
55
|
|
|
{ |
56
|
|
|
return new self(new JsonPresenter(new ApiProblem($status, $detail, $title, $type, $additionalDetails))); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Exception $exception |
61
|
|
|
* @param string $title |
62
|
|
|
* @param string $type |
63
|
|
|
* @param array $additionalDetails |
64
|
|
|
* |
65
|
|
|
* @return ApiProblemResponse |
66
|
|
|
*/ |
67
|
|
|
public static function fromExceptionToJson( |
68
|
|
|
Exception $exception, |
69
|
|
|
$title = '', |
70
|
|
|
$type = '', |
71
|
|
|
array $additionalDetails = [] |
72
|
|
|
) { |
73
|
|
|
return new self(new JsonPresenter(ApiProblem::fromException($exception, $title, $type, $additionalDetails))); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $status |
78
|
|
|
* @param $detail |
79
|
|
|
* @param string $title |
80
|
|
|
* @param string $type |
81
|
|
|
* @param array $additionalDetails |
82
|
|
|
* |
83
|
|
|
* @return ApiProblemResponse |
84
|
|
|
*/ |
85
|
|
|
public static function xml($status, $detail, $title = '', $type = '', array $additionalDetails = []) |
86
|
|
|
{ |
87
|
|
|
return new self(new XmlPresenter(new ApiProblem($status, $detail, $title, $type, $additionalDetails))); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param Exception $exception |
92
|
|
|
* @param string $title |
93
|
|
|
* @param string $type |
94
|
|
|
* @param array $additionalDetails |
95
|
|
|
* |
96
|
|
|
* @return ApiProblemResponse |
97
|
|
|
*/ |
98
|
|
|
public static function fromExceptionToXml( |
99
|
|
|
Exception $exception, |
100
|
|
|
$title = '', |
101
|
|
|
$type = '', |
102
|
|
|
array $additionalDetails = [] |
103
|
|
|
) { |
104
|
|
|
return new self(new XmlPresenter(ApiProblem::fromException($exception, $title, $type, $additionalDetails))); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|