Skip to content

Commit

Permalink
Using the Accept* classes in the Negotiator.
Browse files Browse the repository at this point in the history
  • Loading branch information
winmillwill committed Apr 14, 2012
1 parent dd199cd commit eb5bda8
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 149 deletions.
142 changes: 75 additions & 67 deletions lib/BadFaith/Negotiator.php
Expand Up @@ -24,85 +24,93 @@
* THE SOFTWARE.
*/

namespace BadFaith;

/**
* BadFaith content negotiation class.
*
* @package BadFaith
* @author William Milton
*/
class Negotiator {
public $accept;
public $accept_charset;
public $accept_encoding;
public $accept_language;
class Negotiator
{
public $headerLiterals = array();

function __construct($headers = array()) {
if (empty($headers)) {
$this->headers_from_globals();
}
else {
$this->headers_from_arg($headers);
}
}
public $headerLists = array();

/**
* Sets properties using the $_SERVER array
*/
function headers_from_globals() {
$keys = array(
'accept',
'accept_charset',
'accept_encoding',
'accept_language',
);
foreach ($keys as $key) {
$this->$key = $_SERVER['HTTP_' . strtoupper($key)];
/**
* Constructor that initializes with given dict or $_SERVER.
* @param array $headers a dict of header strings
*/
function __construct($headers = array())
{
if (empty($headers)) {
$this->headersFromGlobals();
} else {
$this->headersFromArg($headers);
}
$this->initLists();
}
}

/**
* Sets properties using the constructor arg.
*/
function headers_from_arg($arg) {
foreach ($arg as $key => $value) {
if (property_exists('Negotiator', $key)) {
$this->$key = $value;
}
/**
* Sets properties using the $_SERVER array
*/
function headersFromGlobals()
{
$keys = array(
'accept',
'accept_charset',
'accept_encoding',
'accept_language',
);
foreach ($keys as $key) {
$this->headerLiterals[$key] = $_SERVER['HTTP_' . strtoupper($key)];
}
}
}

/**
* Splits the Accept header media ranges
*/
function accept_range_split($header) {
$media_ranges = explode(',', $header);
return $media_ranges;
}

/**
* Splits the Accept header media range types
*/
function accept_range_type_split() {
$type_subtype = explode('/', $this->accept);
return $media_ranges;
}
/**
* Sets properties using the constructor arg.
*/
function headersFromArg(array $arg)
{
foreach ($arg as $key => $value) {
$this->headerLiterals[$key] = $value;
}
}

/**
* Splits the Accept header media range parameters
*/
function accept_range_parameter_split($range) {
$parameters = explode(';', $this->accept);
$type = array_pop($parameters);
return array('type' => $type, 'parameters' => $parameters);
}
/**
* Initializes the list objects for the different Accept* headers.
*/
function initLists()
{
foreach ($this->headerLiterals as $key => $value) {
$class = $this->listClass($key);
$this->headerLists[$key] = new $class($value);
}
}

/**
* Parses the Accept header media ranges
*/
function accept_range_parse() {
$media_ranges = $this->accept_range_split($this->accept);
$type_with_params = $this->accept_range_parameter_split($range);
$parsed_ranges = array();
return $parsed_ranges;
}
/**
* Maps the type of header field to the list class that will hold its
* entries.
* @param $type string the key of the literals array
* @return the namespaced classname
*/
function listClass($type)
{
switch (strtoupper($type)) {
case 'ACCEPT':
$class = 'AcceptList';
break;
case 'ACCEPT_CHARSET':
$class = 'AcceptCharsetList';
break;
case 'ACCEPT_ENCODING':
$class = 'AcceptEncodingList';
break;
case 'ACCEPT_LANGUAGE':
$class = 'AcceptLanguageList';
break;
}
return __NAMESPACE__ . '\\' . $class;
}
}
124 changes: 42 additions & 82 deletions tests/BadFaith/Tests/NegotiatorTest.php
Expand Up @@ -32,94 +32,54 @@
* Negotiator Test
*
* @package BadFaith
* @author William Milton
* @author William Milton
*/
class NegotiatorTest extends \PHPUnit_Framework_TestCase
{
protected $server;
protected $server;

public function setUp()
{
$this->server = array (
'HTTP_ACCEPT' => 'text/html;level=2;q=0.7,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'HTTP_ACCEPT_ENCODING' => 'gzip,deflate,sdch',
'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.8',
'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
);
$this->headers = array (
'accept' => 'text/html;level=2;q=0.7,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'accept_encoding' => 'gzip,deflate,sdch',
'accept_language' => 'en-US,en;q=0.8',
'accept_charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
);
$this->media_ranges = array(
'text/html;level=2;q=0.7',
'text/html',
'application/xhtml+xml',
'application/xml;q=0.9',
'*/*;q=0.8',
);
$this->parsed_accept = array(
'text' => array(
'html' => array(
'q' => 0.7,
'level' => 2,
),
'html' => array(
'q' => 1,
),
),
'application' => array(
'xhtml+xml' => array(
'q' => 1,
),
'xml' => array(
'q' => 0.9,
),
),
'*' => array(
'*' => array(
'q' => 0.8,
),
),
);
}
public function setUp()
{
$this->server = array (
'HTTP_ACCEPT' => 'text/html;level=2;q=0.7,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'HTTP_ACCEPT_ENCODING' => 'gzip,deflate,sdch',
'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.8',
'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
);
$this->headers = array (
'accept' => 'text/html;level=2;q=0.7,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'accept_encoding' => 'gzip,deflate,sdch',
'accept_language' => 'en-US,en;q=0.8',
'accept_charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
);
$this->media_ranges = array(
'text/html;level=2;q=0.7',
'text/html',
'application/xhtml+xml',
'application/xml;q=0.9',
'*/*;q=0.8',
);
}

public function testInitAcceptsWithNothing()
{
$_SERVER = $this->server;
$negotiator = new Negotiator();
public function testInitAcceptsWithNothing()
{
$_SERVER = $this->server;
$negotiator = new \BadFaith\Negotiator();

$this->assertEquals($_SERVER['HTTP_ACCEPT'], $negotiator->accept);
}
$this->assertEquals(
$_SERVER['HTTP_ACCEPT'],
$negotiator->headerLiterals['accept']
);
}

public function testInitAcceptsWithArg()
{
$headers = $this->headers;
$negotiator = new Negotiator($headers);
public function testInitAcceptsWithArg()
{
$headers = $this->headers;
$negotiator = new \BadFaith\Negotiator($headers);

$this->assertEquals($headers['accept'], $negotiator->accept);
}

/**
* @depends testInitAcceptsWithArg
*/
public function testSplitMediaRanges()
{
$headers = $this->headers;
$negotiator = new Negotiator($headers);

$this->assertEquals($this->media_ranges, $negotiator->accept_range_split($negotiator->accept));
}

/**
* @depends testSplitMediaRanges
*/
public function testSplitMediaRangeParameters()
{
$headers = $this->headers;
$negotiator = new Negotiator($headers);

$ranges = $negotiator->accept_range_split($negotiator->accept);
}
$this->assertEquals(
$headers['accept'],
$negotiator->headerLiterals['accept']
);
}
}

0 comments on commit eb5bda8

Please sign in to comment.