Elgg  Version master
Response.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Http;
4 
7 
14 abstract class Response implements ResponseBuilder {
15 
16  protected $content;
17 
18  protected int $status_code;
19 
20  protected ?string $forward_url = null;
21 
22  protected array $headers = [];
23 
24  protected ?\Exception $exception = null;
25 
26  protected bool $secure_forward_url = true;
27 
31  public function setContent($content = '') {
32  if (isset($content) && !is_scalar($content) && !is_array($content)) {
33  throw new InvalidArgumentException(__METHOD__ . ' expects content as a scalar value or array');
34  }
35 
36  $this->content = $content;
37  return $this;
38  }
39 
43  public function getContent() {
44  return $this->content;
45  }
46 
50  public function setException(\Exception $e) {
51  $this->exception = $e;
52  return $this;
53  }
54 
58  public function getException() {
59  return $this->exception;
60  }
61 
65  public function setStatusCode(int $status_code) {
66  if ($status_code < 100 || $status_code > 599) {
67  throw new RangeException(__METHOD__ . ' expects a valid HTTP status code');
68  }
69 
70  $this->status_code = $status_code;
71  return $this;
72  }
73 
77  public function getStatusCode() {
78  return $this->status_code;
79  }
80 
84  public function setForwardURL(string $forward_url = REFERRER) {
85  $this->forward_url = $forward_url;
86  return $this;
87  }
88 
92  public function getForwardURL(): ?string {
93  if (!isset($this->forward_url)) {
94  return null;
95  }
96 
98  if ($forward_url === REFERRER || !$this->secure_forward_url) {
99  return $forward_url;
100  }
101 
102  return elgg_normalize_site_url($forward_url) !== null ? $forward_url : '';
103  }
104 
108  public function setHeaders(array $headers = []) {
109  $this->headers = $headers;
110  return $this;
111  }
112 
116  public function getHeaders() {
117  return $this->headers;
118  }
119 
123  public function isInformational() {
124  return $this->status_code >= 100 && $this->status_code <= 199;
125  }
126 
130  public function isSuccessful() {
131  return $this->status_code >= 200 && $this->status_code <= 299;
132  }
133 
137  public function isOk() {
138  return $this->status_code === 200;
139  }
140 
144  public function isRedirection() {
145  return in_array($this->status_code, [201, 301, 302, 303, 307, 308]);
146  }
147 
151  public function isClientError() {
152  return $this->status_code >= 400 && $this->status_code <= 499;
153  }
154 
158  public function isServerError() {
159  return $this->status_code >= 500 && $this->status_code <= 599;
160  }
161 
165  public function isNotModified() {
166  return $this->status_code === 304;
167  }
168 }
Exception thrown if an argument is not of the expected type.
Exception thrown to indicate range errors during program execution.
Response builder.
Definition: Response.php:14
isClientError()
{Check if response is client error.bool}
Definition: Response.php:151
getException()
{Get the exception for this reponse.\Exception|null}
Definition: Response.php:58
isServerError()
{Check if response is server error.bool}
Definition: Response.php:158
setForwardURL(string $forward_url=REFERRER)
{Sets redirect URL.Forward URL self }
Definition: Response.php:84
bool $secure_forward_url
Definition: Response.php:26
string $forward_url
Definition: Response.php:20
isNotModified()
{Check if response has been modified.bool}
Definition: Response.php:165
setHeaders(array $headers=[])
{Sets additional response headers.Headers self}
Definition: Response.php:108
Exception $exception
Definition: Response.php:24
getForwardURL()
{Returns redirect URL.string|null string the forward url, null when no url was set or the constant RE...
Definition: Response.php:92
getHeaders()
{Returns additional response headers.array}
Definition: Response.php:116
isSuccessful()
{Check if response is successful.bool}
Definition: Response.php:130
isRedirection()
{Check if response is redirection.bool}
Definition: Response.php:144
setContent($content='')
{Sets response body.Content of the response as a scalar value or an array self }
Definition: Response.php:31
getContent()
{Returns response body.mixed}
Definition: Response.php:43
isInformational()
{Check if response is informational.bool}
Definition: Response.php:123
getStatusCode()
{Returns status code.int}
Definition: Response.php:77
isOk()
{Check if response is OK.bool}
Definition: Response.php:137
setException(\Exception $e)
{Set an exception for this response.the exception for this responseself}
Definition: Response.php:50
setStatusCode(int $status_code)
{Sets response HTTP status code.Status code self }
Definition: Response.php:65
const REFERRER
Used in calls to forward() to specify the browser should be redirected to the referring page.
Definition: constants.php:37
HTTP response builder interface.
$headers
Definition: section.php:21
elgg_normalize_site_url(string $unsafe_url)
From untrusted input, get a site URL safe for forwarding.
Definition: output.php:175
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10