Elgg  Version 3.0
OkResponse.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Http;
4 
6 
10 class OkResponse implements ResponseBuilder {
11 
15  protected $content;
16 
20  protected $status_code;
21 
25  protected $forward_url;
26 
30  protected $headers;
31 
41  public function __construct($content = '', $status_code = ELGG_HTTP_OK, $forward_url = null) {
42  $this->setContent($content);
45  }
46 
50  public function setContent($content = '') {
51  if (isset($content) && !is_scalar($content) && !is_array($content)) {
52  throw new InvalidArgumentException(__METHOD__ . ' expects content as a scalar value or array');
53  }
54  $this->content = $content;
55  return $this;
56  }
57 
61  public function getContent() {
62  return $this->content;
63  }
64 
69  if (isset($status_code) && (!is_numeric($status_code) || $status_code < 100 || $status_code > 599)) {
70  throw new InvalidArgumentException(__METHOD__ . ' expects a valid HTTP status code');
71  }
72  $this->status_code = (int) $status_code;
73  return $this;
74  }
75 
79  public function getStatusCode() {
80  return $this->status_code;
81  }
82 
86  public function setForwardURL($forward_url = REFERRER) {
87  if (isset($forward_url) && !is_string($forward_url) && $forward_url !== REFERRER) {
88  throw new InvalidArgumentException(__METHOD__ . ' expects a valid URL or REFERRER');
89  }
90  $this->forward_url = $forward_url;
91  return $this;
92  }
93 
97  public function getForwardURL() {
98  return $this->forward_url;
99  }
100 
104  public function setHeaders(array $headers = []) {
105  $this->headers = $headers;
106  return $this;
107  }
108 
112  public function getHeaders() {
113  return (array) $this->headers;
114  }
115 
119  public function isInformational() {
120  return $this->status_code >= 100 && $this->status_code <= 199;
121  }
122 
126  public function isSuccessful() {
127  return $this->status_code >= 200 && $this->status_code <= 299;
128  }
129 
133  public function isOk() {
134  return $this->status_code === 200;
135  }
136 
140  public function isRedirection() {
141  return in_array($this->status_code, [201, 301, 302, 303, 307, 308]);
142  }
143 
147  public function isClientError() {
148  return $this->status_code >= 400 && $this->status_code <= 499;
149  }
150 
154  public function isServerError() {
155  return $this->status_code >= 500 && $this->status_code <= 599;
156  }
157 
161  public function isNotModified() {
162  return $this->status_code === 304;
163  }
164 }
setStatusCode($status_code=ELGG_HTTP_OK)
{Sets response HTTP status code.Status code self }
Definition: OkResponse.php:68
HTTP response builder interface.
setForwardURL($forward_url=REFERRER)
{Sets redirect URL.Forward URL self }
Definition: OkResponse.php:86
isNotModified()
{Check if response has been modified.bool}
Definition: OkResponse.php:161
__construct($content= '', $status_code=ELGG_HTTP_OK, $forward_url=null)
Constructor.
Definition: OkResponse.php:41
const ELGG_HTTP_OK
Definition: constants.php:60
isRedirection()
{Check if response is redirection.bool}
Definition: OkResponse.php:140
setContent($content= '')
{Sets response body.Content of the response as a scalar value or an array self }
Definition: OkResponse.php:50
isInformational()
{Check if response is informational.bool}
Definition: OkResponse.php:119
getForwardURL()
{Returns redirect URL.string|int|null string the forward url, null when no url was set or the constan...
Definition: OkResponse.php:97
isServerError()
{Check if response is server error.bool}
Definition: OkResponse.php:154
getHeaders()
{Returns additional response headers.array}
Definition: OkResponse.php:112
isSuccessful()
{Check if response is successful.bool}
Definition: OkResponse.php:126
const REFERRER
Definition: constants.php:42
getStatusCode()
{Returns status code.int}
Definition: OkResponse.php:79
isClientError()
{Check if response is client error.bool}
Definition: OkResponse.php:147
isOk()
{Check if response is OK.bool}
Definition: OkResponse.php:133
getContent()
{Returns response body.mixed}
Definition: OkResponse.php:61
Response builder.
Definition: OkResponse.php:10
setHeaders(array $headers=[])
{Sets additional response headers.Headers self}
Definition: OkResponse.php:104