Hoa central
Http.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Http;
38 
39 use Hoa\Core;
40 
49 abstract class Http implements \ArrayAccess, \IteratorAggregate, \Countable
50 {
56  protected static $_fcgi = null;
57 
63  protected $_httpVersion = 1.1;
64 
70  protected $_headers = [];
71 
77  protected $_body = null;
78 
79 
80 
86  public function __construct()
87  {
88  if (null === self::$_fcgi) {
89  self::$_fcgi = 'cgi-fcgi' === PHP_SAPI;
90  }
91 
92  return;
93  }
94 
101  public function setHttpVersion($version)
102  {
103  $old = $this->_httpVersion;
104  $this->_httpVersion = $version;
105 
106  return $old;
107  }
108 
114  public function getHttpVersion()
115  {
116  return $this->_httpVersion;
117  }
118 
126  abstract public function parse($packet);
127 
134  protected function _parse(Array $headers)
135  {
136  unset($this->_headers);
137  $this->_headers = [];
138 
139  foreach ($headers as $header) {
140  list($name, $value) = explode(':', $header, 2);
141  $this->_headers[strtolower($name)] = trim($value);
142  }
143 
144  return $this->_headers;
145  }
146 
152  public function getHeaders()
153  {
154  return $this->_headers;
155  }
156 
162  public function getHeadersFormatted()
163  {
164  $out = [];
165 
166  foreach ($this->getHeaders() as $header => $value) {
167  if ('x-' == strtolower(substr($header, 0, 2))) {
168  $header = 'http_' . $header;
169  }
170 
171  $out[strtoupper(str_replace('-', '_', $header))] = $value;
172  }
173 
174  return $out;
175  }
176 
183  public function offsetExists($offset)
184  {
185  return array_key_exists($offset, $this->_headers);
186  }
187 
194  public function offsetGet($offset)
195  {
196  if (false === $this->offsetExists($offset)) {
197  return null;
198  }
199 
200  return $this->_headers[$offset];
201  }
202 
210  public function offsetSet($offset, $value)
211  {
212  $this->_headers[$offset] = $value;
213 
214  return;
215  }
216 
223  public function offsetUnset($offset)
224  {
225  unset($this->_headers[$offset]);
226 
227  return;
228  }
229 
235  public function getIterator()
236  {
237  return new \ArrayIterator($this->getHeaders());
238  }
239 
245  public function count()
246  {
247  return count($this->getHeaders());
248  }
249 
256  public function setBody($body)
257  {
258  $old = $this->_body;
259  $this->_body = $body;
260 
261  return $old;
262  }
263 
269  public function getBody()
270  {
271  return $this->_body;
272  }
273 
279  public function __toString()
280  {
281  $out = null;
282 
283  foreach ($this->getHeaders() as $key => $value) {
284  $out .= $key . ': ' . $value . CRLF;
285  }
286 
287  return $out;
288  }
289 }
290 
294 Core\Consistency::flexEntity('Hoa\Http\Http');
offsetExists($offset)
Definition: Http.php:183
setBody($body)
Definition: Http.php:256
static $_fcgi
Definition: Http.php:56
offsetGet($offset)
Definition: Http.php:194
__toString()
Definition: Http.php:279
setHttpVersion($version)
Definition: Http.php:101
offsetSet($offset, $value)
Definition: Http.php:210
getHeaders()
Definition: Http.php:152
__construct()
Definition: Http.php:86
_parse(Array $headers)
Definition: Http.php:134
getHttpVersion()
Definition: Http.php:114
getIterator()
Definition: Http.php:235
offsetUnset($offset)
Definition: Http.php:223
getHeadersFormatted()
Definition: Http.php:162
parse($packet)