PanovAlexey /
geo-coder
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace CodeblogPro\GeoCoder\Http; |
||
| 6 | |||
| 7 | use CodeblogPro\GeoCoder\Exceptions\InvalidArgumentException; |
||
| 8 | |||
| 9 | class Response implements ResponseInterface |
||
| 10 | { |
||
| 11 | private int $statusCode; |
||
| 12 | private string $body; |
||
| 13 | private array $headers; |
||
| 14 | |||
| 15 | public function __construct(int $statusCode, string $body, array $headers = []) |
||
| 16 | { |
||
| 17 | $this->setStatusCodeIsValid($statusCode); |
||
| 18 | $this->setBody($body); |
||
| 19 | $this->setHeadersIsValid($headers); |
||
| 20 | } |
||
| 21 | |||
| 22 | public function getStatusCode(): int |
||
| 23 | { |
||
| 24 | return $this->statusCode; |
||
| 25 | } |
||
| 26 | |||
| 27 | public function getBody(): string |
||
| 28 | { |
||
| 29 | return $this->body; |
||
| 30 | } |
||
| 31 | |||
| 32 | public function getHeaders(): array |
||
| 33 | { |
||
| 34 | return $this->headers; |
||
| 35 | } |
||
| 36 | |||
| 37 | private function setStatusCodeIsValid(int $statusCode): void |
||
| 38 | { |
||
| 39 | $statusCode = (int)$statusCode; |
||
| 40 | |||
| 41 | if ($statusCode < 100 || $statusCode > 599) { |
||
| 42 | throw new InvalidArgumentException('Invalid status code.'); |
||
| 43 | } |
||
| 44 | |||
| 45 | $this->statusCode = $statusCode; |
||
| 46 | } |
||
| 47 | |||
| 48 | private function setBody(string $body): void |
||
| 49 | { |
||
| 50 | $this->body = $body; |
||
| 51 | } |
||
| 52 | |||
| 53 | private function setHeadersIsValid(array $headers): void |
||
| 54 | { |
||
| 55 | if (!is_array($headers)) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 56 | throw new InvalidArgumentException('Headers must be an array type.'); |
||
| 57 | } |
||
| 58 | |||
| 59 | $this->headers = $headers; |
||
| 60 | } |
||
| 61 | } |
||
| 62 |