-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from mishak87/dev_source_with_headers
More forgiving storage for headers
- Loading branch information
Showing
3 changed files
with
63 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace Httpful\Response; | ||
|
||
final class Headers implements \ArrayAccess, \Countable { | ||
|
||
private $headers; | ||
|
||
private function __construct($headers) | ||
{ | ||
$this->headers = $headers; | ||
} | ||
|
||
public static function fromString($string) | ||
{ | ||
$lines = preg_split("/(\r|\n)+/", $string, -1, PREG_SPLIT_NO_EMPTY); | ||
array_shift($lines); // HTTP HEADER | ||
$headers = array(); | ||
foreach ($lines as $line) { | ||
list($name, $value) = explode(':', $line, 2); | ||
$headers[strtolower(trim($name))] = trim($value); | ||
} | ||
return new self($headers); | ||
} | ||
|
||
public function offsetExists($offset) | ||
{ | ||
return isset($this->headers[strtolower($offset)]); | ||
} | ||
|
||
public function offsetGet($offset) | ||
{ | ||
if (isset($this->headers[$name = strtolower($offset)])) { | ||
return $this->headers[$name]; | ||
} | ||
} | ||
|
||
public function offsetSet($offset, $value) | ||
{ | ||
throw new \Exception("Headers are read-only."); | ||
} | ||
|
||
public function offsetUnset($offset) | ||
{ | ||
throw new \Exception("Headers are read-only."); | ||
} | ||
|
||
public function count() | ||
{ | ||
return count($this->headers); | ||
} | ||
|
||
public function toArray() | ||
{ | ||
return $this->headers; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters