1 | <?php declare(strict_types=1); |
||
16 | abstract class Exception extends PhpException |
||
17 | { |
||
18 | /** |
||
19 | * @var integer The status code. |
||
20 | */ |
||
21 | private $statusCode; |
||
22 | |||
23 | /** |
||
24 | * @var string The reason phrase. |
||
25 | */ |
||
26 | private $reasonPhrase; |
||
27 | |||
28 | /** |
||
29 | * @var string[][] The headers. |
||
30 | */ |
||
31 | private $headers; |
||
32 | |||
33 | /** |
||
34 | * Creates a new HTTP client error exception. |
||
35 | * |
||
36 | * @param integer $statusCode A client error status code. |
||
37 | * @param string $reasonPhrase A reason phrase for the status code. |
||
38 | * @param string[][] $headers Headers to be sent along when preparing the exception's response. |
||
39 | * @throws InvalidArgumentException If a client error status code was not provided. |
||
40 | * @throws InvalidArgumentException If the reason phrase is empty. |
||
41 | */ |
||
42 | 7 | public function __construct(int $statusCode, string $reasonPhrase, array $headers = []) |
|
56 | |||
57 | /** |
||
58 | * Retrieve the status code. |
||
59 | * |
||
60 | * @return integer The status code. |
||
61 | */ |
||
62 | 1 | public function getStatusCode(): int |
|
66 | |||
67 | /** |
||
68 | * Retrieve the reason phrase. |
||
69 | * |
||
70 | * @return string The reason phrase. |
||
71 | */ |
||
72 | 1 | public function getReasonPhrase(): string |
|
76 | |||
77 | /** |
||
78 | * Retrieve the headers that should be sent along with the HTTP exception response. |
||
79 | * |
||
80 | * @return string[][] Headers to be sent, in a PSR7 compatible format. |
||
81 | */ |
||
82 | 1 | public function getHeaders(): array |
|
86 | |||
87 | /** |
||
88 | * Prepares a response to send, from the HTTP exception, with things like status code, reason phrase and headers. |
||
89 | * |
||
90 | * @param ResponseInterface $response The response to prepare. |
||
91 | * @return ResponseInterface The prepared response. |
||
92 | */ |
||
93 | 3 | public function prepare(ResponseInterface $response): ResponseInterface |
|
103 | |||
104 | /** |
||
105 | * Magic method to allow the exception to be cast to a string. |
||
106 | * |
||
107 | * @return string The status code and reason phrase on one line. |
||
108 | */ |
||
109 | 1 | public function __toString(): string |
|
113 | |||
114 | /** |
||
115 | * Validate status code is in range of sub class. |
||
116 | * |
||
117 | * @param integer $statusCode The status code to check against. |
||
118 | * @throws InvalidArgumentException If the status code is too low or too high. |
||
119 | */ |
||
120 | abstract protected function assertStatusCodeIsInRange(int $statusCode): void; |
||
121 | } |
||
122 |