Skip to content

Commit

Permalink
Rename HeaderList
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Jul 25, 2014
1 parent 8be54a9 commit c8774c5
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/Request/HeaderList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Represents the HTTP request header fields by normalising the $_SERVER array's
* keys that start with "HTTP_". Notice: These headers are sent by the requester
* and should not be trusted!
*
* Can be accessed as an associative array to reference headers in their
* original format (i.e. $headers["accept-language"]) or as object properties
* with underscores (i.e. $headers->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];
}

}#
72 changes: 72 additions & 0 deletions test/Unit/Request/HeaderList.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* 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;

class HeaderList_Test extends \PHPUnit_Framework_TestCase {

private $server = [
"HTTP_ACCEPT" => "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"] = "<?php system('echo \"HAX!\" && rm -rf /');";
}

public function testNotUnsettable() {
$headers = new HeaderList($this->server);
$this->setExpectedException("\Gt\Core\Exception\InvalidAccessException");

unset($headers["accept"]);
}

public function testIsset() {
$headers = new HeaderList($this->server);
$this->assertTrue(isset($headers["Accept"]));
}

}#

0 comments on commit c8774c5

Please sign in to comment.