Elgg  Version 5.1
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 
19  protected $content;
20 
24  protected $status_code;
25 
29  protected $forward_url;
30 
34  protected $headers;
35 
39  protected $exception;
40 
44  public function setContent($content = '') {
45  if (isset($content) && !is_scalar($content) && !is_array($content)) {
46  throw new InvalidArgumentException(__METHOD__ . ' expects content as a scalar value or array');
47  }
48 
49  $this->content = $content;
50  return $this;
51  }
52 
56  public function getContent() {
57  return $this->content;
58  }
59 
63  public function setException(\Exception $e) {
64  $this->exception = $e;
65  return $this;
66  }
67 
71  public function getException() {
72  return $this->exception;
73  }
74 
78  public function setStatusCode(int $status_code) {
79  if ($status_code < 100 || $status_code > 599) {
80  throw new RangeException(__METHOD__ . ' expects a valid HTTP status code');
81  }
82 
83  $this->status_code = $status_code;
84  return $this;
85  }
86 
90  public function getStatusCode() {
91  return $this->status_code;
92  }
93 
97  public function setForwardURL(string $forward_url = REFERRER) {
98  $this->forward_url = $forward_url;
99  return $this;
100  }
101 
105  public function getForwardURL(): ?string {
106  return $this->forward_url;
107  }
108 
112  public function setHeaders(array $headers = []) {
113  $this->headers = $headers;
114  return $this;
115  }
116 
120  public function getHeaders() {
121  return (array) $this->headers;
122  }
123 
127  public function isInformational() {
128  return $this->status_code >= 100 && $this->status_code <= 199;
129  }
130 
134  public function isSuccessful() {
135  return $this->status_code >= 200 && $this->status_code <= 299;
136  }
137 
141  public function isOk() {
142  return $this->status_code === 200;
143  }
144 
148  public function isRedirection() {
149  return in_array($this->status_code, [201, 301, 302, 303, 307, 308]);
150  }
151 
155  public function isClientError() {
156  return $this->status_code >= 400 && $this->status_code <= 499;
157  }
158 
162  public function isServerError() {
163  return $this->status_code >= 500 && $this->status_code <= 599;
164  }
165 
169  public function isNotModified() {
170  return $this->status_code === 304;
171  }
172 }
isNotModified()
{Check if response has been modified.bool}
Definition: Response.php:169
Exception thrown to indicate range errors during program execution.
HTTP response builder interface.
Exception thrown if an argument is not of the expected type.
getHeaders()
{Returns additional response headers.array}
Definition: Response.php:120
Response builder.
Definition: Response.php:14
isRedirection()
{Check if response is redirection.bool}
Definition: Response.php:148
setStatusCode(int $status_code)
{Sets response HTTP status code.Status code self }
Definition: Response.php:78
isInformational()
{Check if response is informational.bool}
Definition: Response.php:127
isSuccessful()
{Check if response is successful.bool}
Definition: Response.php:134
setForwardURL(string $forward_url=REFERRER)
{Sets redirect URL.Forward URL self }
Definition: Response.php:97
getException()
Get the exception for this reponse.|null
Definition: Response.php:71
getContent()
{Returns response body.mixed}
Definition: Response.php:56
getForwardURL()
{Returns redirect URL.string|null string the forward url, null when no url was set or the constant RE...
Definition: Response.php:105
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:90
setHeaders(array $headers=[])
{Sets additional response headers.Headers self}
Definition: Response.php:112
isOk()
{Check if response is OK.bool}
Definition: Response.php:141
setContent($content= '')
{Sets response body.Content of the response as a scalar value or an array self }
Definition: Response.php:44
isServerError()
{Check if response is server error.bool}
Definition: Response.php:162
isClientError()
{Check if response is client error.bool}
Definition: Response.php:155
setException(\Exception $e)
Set an exception for this response.the exception for this responseself
Definition: Response.php:63
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