1 | <?php |
||
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) |
|
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) |
|
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) |
|
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) |
|
124 | |||
125 | /** |
||
126 | * Get the serialized response output. |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | 18 | protected function getOutput(): array |
|
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) |
|
148 | } |
||
149 |