Elgg  Version 2.3
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
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}
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: elgglib.php:2113
getStatusCode()
{Returns status code.int}
Definition: OkResponse.php:79
isClientError()
{Check if response is client error.bool}
Definition: OkResponse.php:147
const ELGG_HTTP_OK
Definition: elgglib.php:2131
isOk()
{Check if response is OK.bool}
Definition: OkResponse.php:133
getContent()
{Returns response body.mixed}
Definition: OkResponse.php:61
elgg widget content
Definition: admin.css.php:1305
Response builder.
Definition: OkResponse.php:10
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:5
setHeaders(array $headers=[])
{Sets additional response headers.Headers self}
Definition: OkResponse.php:104