Skip to content

Commit 9cfd53b

Browse files
committed
Update Response.php
Added response status text setter & getter
1 parent 3a517d1 commit 9cfd53b

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

src/Response.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
namespace Evas\Http;
66

7-
use \Exception;
7+
use Evas\Http\HttpException;
88
use Evas\Http\BodyTrait;
99
use Evas\Http\HeadersTrait;
1010
use Evas\Http\ResponseInterface;
@@ -51,16 +51,28 @@ class Response implements ResponseInterface
5151

5252
/**
5353
* Установка кода статуса.
54-
* @param int
55-
* @throws Exception
54+
* @param int код статуса
55+
* @param string|null кастомный текст статуса
56+
* @throws HttpException
5657
* @return self
5758
*/
58-
public function withStatusCode(int $code)
59+
public function withStatusCode(int $code, string $statusText = null)
5960
{
6061
$this->statusCode = $code;
61-
$this->statusText = static::HTTP_STATUSES[$this->statusCode] ?? null;
62+
return $this->withStatusText($statusText);
63+
}
64+
65+
/**
66+
* Установка текста статуса.
67+
* @param string|null кастомный текст статуса
68+
* @throws HttpException
69+
* @return self
70+
*/
71+
public function withStatusText(string $statusText = null)
72+
{
73+
$this->statusText = $statusText ?? static::HTTP_STATUSES[$this->statusCode] ?? null;
6274
if (! $this->statusText) {
63-
throw new Exception(static::ERROR_HTTP_STATUS_NOT_FOUND . " $this->statusCode");
75+
throw new HttpException(static::ERROR_HTTP_STATUS_NOT_FOUND . " $this->statusCode");
6476
}
6577
return $this;
6678
}
@@ -75,8 +87,7 @@ public function write(string $message)
7587
$this->body .= $message;
7688
return $this;
7789
}
78-
79-
90+
8091

8192
/**
8293
* Получение кода статуса.
@@ -87,6 +98,14 @@ public function getStatusCode(): int
8798
return $this->statusCode;
8899
}
89100

101+
/**
102+
* Получение текста статуса.
103+
* @return string|null
104+
*/
105+
public function getStatusText(): ?string
106+
{
107+
return $this->statusText ?? static::HTTP_STATUSES[$this->statusCode] ?? null;
108+
}
90109

91110

92111
/**

0 commit comments

Comments
 (0)