Elgg  Version 4.3
Response.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Http;
4 
6 
13 abstract class Response implements ResponseBuilder {
14 
18  protected $content;
19 
23  protected $status_code;
24 
28  protected $forward_url;
29 
33  protected $headers;
34 
38  protected $exception;
39 
43  public function setContent($content = '') {
44  if (isset($content) && !is_scalar($content) && !is_array($content)) {
45  throw new InvalidArgumentException(__METHOD__ . ' expects content as a scalar value or array');
46  }
47  $this->content = $content;
48  return $this;
49  }
50 
54  public function getContent() {
55  return $this->content;
56  }
57 
61  public function setException(\Exception $e) {
62  $this->exception = $e;
63  return $this;
64  }
65 
69  public function getException() {
70  return $this->exception;
71  }
72 
76  public function setStatusCode(int $status_code) {
77  if ($status_code < 100 || $status_code > 599) {
78  throw new InvalidArgumentException(__METHOD__ . ' expects a valid HTTP status code');
79  }
80  $this->status_code = (int) $status_code;
81  return $this;
82  }
83 
87  public function getStatusCode() {
88  return $this->status_code;
89  }
90 
94  public function setForwardURL($forward_url = REFERRER) {
95  if (isset($forward_url) && !is_string($forward_url) && $forward_url !== REFERRER) {
96  throw new InvalidArgumentException(__METHOD__ . ' expects a valid URL or REFERRER');
97  }
98  $this->forward_url = $forward_url;
99  return $this;
100  }
101 
105  public function getForwardURL() {
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
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:13
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:76
isInformational()
{Check if response is informational.bool}
Definition: Response.php:127
isSuccessful()
{Check if response is successful.bool}
Definition: Response.php:134
getException()
Get the exception for this reponse.|null
Definition: Response.php:69
getContent()
{Returns response body.mixed}
Definition: Response.php:54
getForwardURL()
{Returns redirect URL.string|int|null string the forward url, null when no url was set or the constan...
Definition: Response.php:105
const REFERRER
Definition: constants.php:42
getStatusCode()
{Returns status code.int}
Definition: Response.php:87
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:43
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:61
setForwardURL($forward_url=REFERRER)
{Sets redirect URL.Forward URL self }
Definition: Response.php:94
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