diff --git a/src/Request/HeaderList.php b/src/Request/HeaderList.php new file mode 100644 index 00000000..6724959b --- /dev/null +++ b/src/Request/HeaderList.php @@ -0,0 +1,59 @@ +accept_language). + * + * PHP.Gt (http://php.gt) + * @copyright Copyright Ⓒ 2014 Bright Flair Ltd. (http://brightflair.com) + * @license Apache Version 2.0, January 2004. http://www.apache.org/licenses + */ +namespace Gt\Request; +use \Gt\Core\Obj; + +class HeaderList implements \ArrayAccess { + +private $headerArray = []; + +public function __construct($serverArray) { + foreach ($serverArray as $key => $value) { + if(strpos($key, "HTTP_") !== 0) { + continue; + } + + $headerName = str_replace("_", "-", substr($key, 5)); + $headerName = strtolower($headerName); + $this->headerArray[$headerName] = $value; + } +} + +public function offsetExists($offset) { + $offset = strtolower($offset); + return isset($this->headerArray[$offset]); +} + +public function offsetGet($offset) { + $offset = strtolower($offset); + return $this->headerArray[$offset]; +} + +public function offsetSet($offset, $value) { + throw new \Gt\Core\Exception\InvalidAccessException(); +} + +public function offsetUnset($offset) { + throw new \Gt\Core\Exception\InvalidAccessException(); +} + +public function __get($name) { + $name = strtolower($name); + + $name = str_replace("_", "-", $name); + return $this->headerArray[$name]; +} + +}# \ No newline at end of file diff --git a/test/Unit/Request/HeaderList.test.php b/test/Unit/Request/HeaderList.test.php new file mode 100644 index 00000000..97d23a3d --- /dev/null +++ b/test/Unit/Request/HeaderList.test.php @@ -0,0 +1,72 @@ + "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "HTTP_ACCEPT_ENCODING" => "gzip,deflate,sdch", + "HTTP_COOKIE" => "FIRST=Les; LAST=McQueen; BAND=crème brûlée", + "HTTP_USER_AGENT" => "PHPUnit/PHP.Gt (http://php.gt)", +]; + +public function testArrayAccess() { + $headers = new HeaderList($this->server); + + $this->assertEquals( + $this->server["HTTP_ACCEPT"], $headers["accept"]); + $this->assertEquals( + $this->server["HTTP_ACCEPT_ENCODING"], $headers["accept-encoding"]); + $this->assertEquals( + $this->server["HTTP_COOKIE"], $headers["cookie"]); + $this->assertEquals( + $this->server["HTTP_USER_AGENT"], $headers["user-agent"]); +} + +public function testProperty() { + $headers = new HeaderList($this->server); + + $this->assertEquals( + $this->server["HTTP_ACCEPT"], $headers->accept); + $this->assertEquals( + $this->server["HTTP_ACCEPT_ENCODING"], $headers->accept_encoding); + $this->assertEquals( + $this->server["HTTP_COOKIE"], $headers->cookie); + $this->assertEquals( + $this->server["HTTP_USER_AGENT"], $headers->user_agent); +} + +public function testCase() { + $headers = new HeaderList($this->server); + + $this->assertEquals( + $this->server["HTTP_ACCEPT"], $headers["Accept"]); + $this->assertEquals( + $this->server["HTTP_ACCEPT"], $headers->Accept); +} + +public function testReadOnly() { + $headers = new HeaderList($this->server); + $this->setExpectedException("\Gt\Core\Exception\InvalidAccessException"); + + $headers["accept"] = "server); + $this->setExpectedException("\Gt\Core\Exception\InvalidAccessException"); + + unset($headers["accept"]); +} + +public function testIsset() { + $headers = new HeaderList($this->server); + $this->assertTrue(isset($headers["Accept"])); +} + +}# \ No newline at end of file