1 | <?php |
||
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) |
||
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 |
||
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 |
||
93 | |||
94 | /** |
||
95 | * Get the serialized response output. |
||
96 | * |
||
97 | * @return array |
||
98 | */ |
||
99 | protected function getOutput(): array |
||
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 |
||
117 | } |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.