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