kodedphp /
http
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the Koded package. |
||
| 5 | * |
||
| 6 | * (c) Mihail Binev <[email protected]> |
||
| 7 | * |
||
| 8 | * Please view the LICENSE distributed with this source code |
||
| 9 | * for the full copyright and license information. |
||
| 10 | * |
||
| 11 | */ |
||
| 12 | |||
| 13 | namespace Koded\Http; |
||
| 14 | |||
| 15 | use Koded\Http\Interfaces\{HttpStatus, Request, Response}; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Class ServerResponse |
||
| 19 | * |
||
| 20 | */ |
||
| 21 | class ServerResponse implements Response, \JsonSerializable |
||
| 22 | { |
||
| 23 | use HeaderTrait, MessageTrait, CookieTrait, JsonSerializeTrait; |
||
| 24 | |||
| 25 | private const E_CLIENT_RESPONSE_SEND = 'Cannot send the client response.'; |
||
| 26 | private const E_INVALID_STATUS_CODE = 'Invalid status code %s, expected range between [100-599]'; |
||
| 27 | |||
| 28 | protected int $statusCode = HttpStatus::OK; |
||
| 29 | protected string $reasonPhrase = 'OK'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * ServerResponse constructor. |
||
| 33 | * |
||
| 34 | * @param mixed $content [optional] |
||
| 35 | * @param int $statusCode [optional] |
||
| 36 | * @param array $headers [optional] |
||
| 37 | */ |
||
| 38 | public function __construct($content = '', int $statusCode = HttpStatus::OK, array $headers = []) |
||
| 39 | { |
||
| 40 | $this->setStatus($this, $statusCode); |
||
| 41 | $this->setHeaders($headers); |
||
| 42 | $this->stream = create_stream($content); |
||
| 43 | } |
||
| 44 | |||
| 45 | public function getStatusCode(): int |
||
| 46 | { |
||
| 47 | return $this->statusCode; |
||
| 48 | } |
||
| 49 | |||
| 50 | public function withStatus($code, $reasonPhrase = ''): static |
||
| 51 | { |
||
| 52 | return $this->setStatus(clone $this, (int)$code, (string)$reasonPhrase); |
||
| 53 | } |
||
| 54 | |||
| 55 | public function getReasonPhrase(): string |
||
| 56 | { |
||
| 57 | return (string)$this->reasonPhrase; |
||
| 58 | } |
||
| 59 | |||
| 60 | public function getContentType(): string |
||
| 61 | { |
||
| 62 | return $this->getHeaderLine('Content-Type') ?: 'text/html'; |
||
| 63 | } |
||
| 64 | |||
| 65 | public function sendHeaders(): void |
||
| 66 | { |
||
| 67 | $this->prepareResponse(); |
||
| 68 | if (false === headers_sent()) { |
||
| 69 | foreach ($this->getHeaders() as $name => $values) { |
||
| 70 | header($name . ':' . \join(',', (array)$values), false, $this->statusCode); |
||
| 71 | } |
||
| 72 | // Status header |
||
| 73 | header(\sprintf('HTTP/%s %d %s', |
||
| 74 | $this->getProtocolVersion(), |
||
| 75 | $this->getStatusCode(), |
||
| 76 | $this->getReasonPhrase()), |
||
| 77 | true, |
||
| 78 | $this->statusCode); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | public function sendBody(): string |
||
| 83 | { |
||
| 84 | try { |
||
| 85 | return (string)$this->stream; |
||
| 86 | } finally { |
||
| 87 | $this->stream->close(); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | public function send(): string |
||
| 92 | { |
||
| 93 | $this->sendHeaders(); |
||
| 94 | return $this->sendBody(); |
||
| 95 | } |
||
| 96 | |||
| 97 | protected function setStatus(ServerResponse $instance, int $statusCode, string $reasonPhrase = ''): ServerResponse |
||
| 98 | { |
||
| 99 | if ($statusCode < 100 || $statusCode > 599) { |
||
| 100 | throw new \InvalidArgumentException( |
||
| 101 | \sprintf(self::E_INVALID_STATUS_CODE, $statusCode), HttpStatus::UNPROCESSABLE_ENTITY |
||
| 102 | ); |
||
| 103 | } |
||
| 104 | $instance->statusCode = (int)$statusCode; |
||
| 105 | $instance->reasonPhrase = $reasonPhrase ? (string)$reasonPhrase : StatusCode::CODE[$statusCode]; |
||
| 106 | return $instance; |
||
| 107 | } |
||
| 108 | |||
| 109 | protected function prepareResponse(): void |
||
| 110 | { |
||
| 111 | if (\in_array($this->getStatusCode(), [100, 101, 102, 204, 304])) { |
||
| 112 | $this->stream = create_stream(null); |
||
| 113 | unset($this->headersMap['content-length'], $this->headers['Content-Length']); |
||
| 114 | unset($this->headersMap['content-type'], $this->headers['Content-Type']); |
||
| 115 | return; |
||
| 116 | } |
||
| 117 | if ($size = $this->stream->getSize()) { |
||
| 118 | $this->normalizeHeader('Content-Length', (string)$size, true); |
||
| 119 | } |
||
| 120 | $method = \strtoupper($_SERVER['REQUEST_METHOD'] ?? ''); |
||
| 121 | if (Request::HEAD === $method || Request::OPTIONS === $method) { |
||
| 122 | $this->stream = create_stream(null); |
||
| 123 | } |
||
| 124 | if ($this->hasHeader('Transfer-Encoding') || !$size) { |
||
|
0 ignored issues
–
show
|
|||
| 125 | unset($this->headersMap['content-length'], $this->headers['Content-Length']); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: