| Total Complexity | 10 |
| Total Lines | 51 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 5 | final class Headers implements \ArrayAccess, \Countable { |
||
| 6 | |||
| 7 | private $headers; |
||
| 8 | |||
| 9 | private function __construct($headers) |
||
| 10 | { |
||
| 11 | $this->headers = $headers; |
||
| 12 | } |
||
| 13 | |||
| 14 | public static function fromString($string) |
||
| 15 | { |
||
| 16 | $lines = preg_split("/(\r|\n)+/", $string, -1, PREG_SPLIT_NO_EMPTY); |
||
| 17 | array_shift($lines); // HTTP HEADER |
||
| 18 | $headers = array(); |
||
| 19 | foreach ($lines as $line) { |
||
| 20 | list($name, $value) = explode(':', $line, 2); |
||
| 21 | $headers[strtolower(trim($name))] = trim($value); |
||
| 22 | } |
||
| 23 | return new self($headers); |
||
| 24 | } |
||
| 25 | |||
| 26 | public function offsetExists($offset) |
||
| 27 | { |
||
| 28 | return isset($this->headers[strtolower($offset)]); |
||
| 29 | } |
||
| 30 | |||
| 31 | public function offsetGet($offset) |
||
| 32 | { |
||
| 33 | if (isset($this->headers[$name = strtolower($offset)])) { |
||
| 34 | return $this->headers[$name]; |
||
| 35 | } |
||
| 36 | } |
||
| 37 | |||
| 38 | public function offsetSet($offset, $value) |
||
| 39 | { |
||
| 40 | throw new \Exception("Headers are read-only."); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function offsetUnset($offset) |
||
| 44 | { |
||
| 45 | throw new \Exception("Headers are read-only."); |
||
| 46 | } |
||
| 47 | |||
| 48 | public function count() |
||
| 49 | { |
||
| 50 | return count($this->headers); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function toArray() |
||
| 54 | { |
||
| 55 | return $this->headers; |
||
| 56 | } |
||
| 58 | } |