function __construct(int $statusCode = 200, array $headers = [], $body = NULL) { if ($statusCode < 100 || $statusCode >= 600) { throw new \InvalidArgumentException('Status code must be between 100 and 600'); } foreach ($headers as $name => $header) { if (is_scalar($header)) { $headers[$name] = [$header]; } elseif (!is_array($header)) { throw new \InvalidArgumentException('Invalid header ' . $name); } } $headers = array_change_key_case($headers, CASE_UPPER); $headers += ['CACHE-CONTROL' => ['no-store', 'no-cache', 'must-revalidate', 'post-check=0', 'pre-check=0']]; if ($statusCode >= 100 && $statusCode < 200 || in_array($statusCode, [204, 304])) { unset($headers['CONTENT-TYPE']); unset($headers['CONTENT-LENGTH']); $body = NULL; } elseif (!isset($headers['CONTENT-TYPE'])) { $headers['CONTENT-TYPE'] = ['text/html; charset=utf-8']; } elseif (stripos($headers['CONTENT-TYPE'][0], 'text/') === 0 && stripos($headers['CONTENT-TYPE'][0], 'charset') === FALSE) { $headers['CONTENT-TYPE'][0] .= '; charset=utf-8'; } $this->statusCode = $statusCode; $this->headers = $headers; $this->body = stream_of($body); }
function __construct(string $method, $uri, array $headers = [], $body = NULL) { $this->method = strtoupper($method); $this->uri = is_array($uri) ? $uri : parse_url($uri); if ($this->uri === FALSE) { throw new \InvalidArgumentException('Invalid uri'); } $headers = array_change_key_case($headers, CASE_UPPER); if (isset($this->uri['host']) && !isset($headers['HOST'])) { $headers = ['HOST' => [$this->uri['host']]] + $headers; } $this->headers = $headers; $this->body = stream_of($body); }