*/ private array $headers; private int $code; /** * @param int $code * @param array $headers */ public function __construct(int $code, array $headers) { $this->code = $code; $this->headers = $headers; } public function getCode(): int { return $this->code; } /** * @return array */ public function getHeaders(): array { return $this->headers; } /** * @param int $code * @return HttpResponse */ public static function fromCode(int $code): HttpResponse { return new HttpResponse($code, []); } /** * @param string $url the url to redirect * @param int $code only HTTP 3XX codes are accepted. * @return HttpResponse a response that will redirect client to given url */ public static function redirect(string $url, int $code = HttpCodes::FOUND): HttpResponse { global $basePath; return self::redirect_absolute($basePath . $url, $code); } /** * @param string $url the url to redirect * @param int $code only HTTP 3XX codes are accepted. * @return HttpResponse a response that will redirect client to given url */ public static function redirect_absolute(string $url, int $code = HttpCodes::FOUND): HttpResponse { if ($code < 300 || $code >= 400) { throw new \InvalidArgumentException("given code is not a redirection http code"); } return new HttpResponse($code, ["Location" => $url]); } }