|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Flugg\Responder\Http\Responses; |
|
4
|
|
|
|
|
5
|
|
|
use Flugg\Responder\Contracts\ErrorFactory; |
|
6
|
|
|
use Flugg\Responder\Contracts\ErrorSerializer; |
|
7
|
|
|
use Flugg\Responder\Contracts\ResponseFactory; |
|
8
|
|
|
use Flugg\Responder\Exceptions\InvalidErrorSerializerException; |
|
9
|
|
|
use InvalidArgumentException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* A builder class for building error responses. |
|
13
|
|
|
* |
|
14
|
|
|
* @package flugger/laravel-responder |
|
15
|
|
|
* @author Alexander Tømmerås <[email protected]> |
|
16
|
|
|
* @license The MIT License |
|
17
|
|
|
*/ |
|
18
|
|
|
class ErrorResponseBuilder extends ResponseBuilder |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* A factory for building error data output. |
|
22
|
|
|
* |
|
23
|
|
|
* @var \Flugg\Responder\Contracts\ErrorFactory |
|
24
|
|
|
*/ |
|
25
|
|
|
private $errorFactory; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* A serializer for formatting error data. |
|
29
|
|
|
* |
|
30
|
|
|
* @var \Flugg\Responder\Contracts\ErrorSerializer |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $serializer; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* A code representing the error. |
|
36
|
|
|
* |
|
37
|
|
|
* @var string|null |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $errorCode = null; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* A message descibing the error. |
|
43
|
|
|
* |
|
44
|
|
|
* @var string|null |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $message = null; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Additional data included with the error. |
|
50
|
|
|
* |
|
51
|
|
|
* @var array|null |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $data = null; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* A HTTP status code for the response. |
|
57
|
|
|
* |
|
58
|
|
|
* @var int |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $status = 500; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Construct the builder class. |
|
64
|
|
|
* |
|
65
|
|
|
* @param \Flugg\Responder\Contracts\ResponseFactory $responseFactory |
|
66
|
|
|
* @param \Flugg\Responder\Contracts\ErrorFactory $errorFactory |
|
67
|
|
|
*/ |
|
68
|
66 |
|
public function __construct(ResponseFactory $responseFactory, ErrorFactory $errorFactory) |
|
69
|
|
|
{ |
|
70
|
66 |
|
$this->errorFactory = $errorFactory; |
|
71
|
|
|
|
|
72
|
66 |
|
parent::__construct($responseFactory); |
|
73
|
66 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Set the error code and message. |
|
77
|
|
|
* |
|
78
|
|
|
* @param mixed|null $errorCode |
|
79
|
|
|
* @param string|null $message |
|
80
|
|
|
* @return $this |
|
81
|
|
|
*/ |
|
82
|
10 |
|
public function error($errorCode = null, string $message = null) |
|
83
|
|
|
{ |
|
84
|
10 |
|
$this->errorCode = $errorCode; |
|
85
|
10 |
|
$this->message = $message; |
|
86
|
|
|
|
|
87
|
10 |
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Add additional data to the error. |
|
92
|
|
|
* |
|
93
|
|
|
* @param array|null $data |
|
94
|
|
|
* @return $this |
|
95
|
|
|
*/ |
|
96
|
1 |
|
public function data(array $data = null) |
|
97
|
|
|
{ |
|
98
|
1 |
|
$this->data = array_merge((array) $this->data, (array) $data); |
|
99
|
|
|
|
|
100
|
1 |
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Set the error serializer. |
|
105
|
|
|
* |
|
106
|
|
|
* @param \Flugg\Responder\Contracts\ErrorSerializer|string $serializer |
|
107
|
|
|
* @return $this |
|
108
|
|
|
* @throws \Flugg\Responder\Exceptions\InvalidErrorSerializerException |
|
109
|
|
|
*/ |
|
110
|
66 |
|
public function serializer($serializer) |
|
111
|
|
|
{ |
|
112
|
66 |
|
if (is_string($serializer)) { |
|
113
|
2 |
|
$serializer = new $serializer; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
66 |
|
if (! $serializer instanceof ErrorSerializer) { |
|
117
|
1 |
|
throw new InvalidErrorSerializerException; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
66 |
|
$this->serializer = $serializer; |
|
121
|
|
|
|
|
122
|
66 |
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Get the serialized response output. |
|
127
|
|
|
* |
|
128
|
|
|
* @return array |
|
129
|
|
|
*/ |
|
130
|
18 |
|
protected function getOutput(): array |
|
131
|
|
|
{ |
|
132
|
18 |
|
return $this->errorFactory->make($this->serializer, $this->errorCode, $this->message, $this->data); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Validate the HTTP status code for the response. |
|
137
|
|
|
* |
|
138
|
|
|
* @param int $status |
|
139
|
|
|
* @return void |
|
140
|
|
|
* @throws \InvalidArgumentException |
|
141
|
|
|
*/ |
|
142
|
3 |
|
protected function validateStatusCode(int $status) |
|
143
|
|
|
{ |
|
144
|
3 |
|
if ($status < 400 || $status >= 600) { |
|
145
|
1 |
|
throw new InvalidArgumentException("{$status} is not a valid error HTTP status code."); |
|
146
|
|
|
} |
|
147
|
2 |
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|