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 
97  $forward_url = $this->forward_url;
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 }
isNotModified()
{Check if response has been modified.bool}
Definition: Response.php:165
string $forward_url
Definition: Response.php:20
Exception thrown to indicate range errors during program execution.
HTTP response builder interface.
Exception thrown if an argument is not of the expected type.
bool $secure_forward_url
Definition: Response.php:26
getHeaders()
{Returns additional response headers.array}
Definition: Response.php:116
elgg_normalize_site_url(string $unsafe_url)
From untrusted input, get a site URL safe for forwarding.
Definition: output.php:175
Response builder.
Definition: Response.php:14
isRedirection()
{Check if response is redirection.bool}
Definition: Response.php:144
setStatusCode(int $status_code)
{Sets response HTTP status code.Status code self }
Definition: Response.php:65
isInformational()
{Check if response is informational.bool}
Definition: Response.php:123
isSuccessful()
{Check if response is successful.bool}
Definition: Response.php:130
setForwardURL(string $forward_url=REFERRER)
{Sets redirect URL.Forward URL self }
Definition: Response.php:84
getException()
{Get the exception for this reponse.|null}
Definition: Response.php:58
getContent()
{Returns response body.mixed}
Definition: Response.php:43
getForwardURL()
{Returns redirect URL.string|null string the forward url, null when no url was set or the constant RE...
Definition: Response.php:92
const REFERRER
Used in calls to forward() to specify the browser should be redirected to the referring page...
Definition: constants.php:37
getStatusCode()
{Returns status code.int}
Definition: Response.php:77
setHeaders(array $headers=[])
{Sets additional response headers.Headers self}
Definition: Response.php:108
isOk()
{Check if response is OK.bool}
Definition: Response.php:137
Exception $exception
Definition: Response.php:24
setContent($content= '')
{Sets response body.Content of the response as a scalar value or an array self }
Definition: Response.php:31
isServerError()
{Check if response is server error.bool}
Definition: Response.php:158
isClientError()
{Check if response is client error.bool}
Definition: Response.php:151
setException(\Exception $e)
{Set an exception for this response.the exception for this responseself}
Definition: Response.php:50
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special exception
Definition: LICENSE.txt:210