Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Added AcceptsString utility class for basic ACCEPT header string pars…
…ing.
- Loading branch information
1 parent
b032776
commit 81571f1
Showing
3 changed files
with
141 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
class AcceptsString { | ||
|
||
protected $range; | ||
|
||
protected $types; | ||
|
||
protected $qs = false; | ||
|
||
protected $key = 0; | ||
|
||
public function __construct($range) { | ||
$this->range = $range; | ||
} | ||
|
||
public function next() { | ||
if($this->qs === false) $this->init(); | ||
|
||
if(isset($this->qs[$this->key])) { | ||
return $this->qs[$this->key++]; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public function reset() { | ||
$this->key = 0; | ||
} | ||
|
||
protected function init() { | ||
// Break apart each type | ||
$this->types = explode(',', $this->range); | ||
|
||
$qs = array(); | ||
|
||
$count = count($this->types); | ||
|
||
// Iterate through each to clean-up and extract precedence | ||
for($t = 0; $t < $count; $t++) { | ||
$this->types[$t] = trim($this->types[$t]); | ||
|
||
$q = 1.0; | ||
|
||
// Break apart type parameters | ||
$params = explode(';', $this->types[$t]); | ||
|
||
$paramsCount = count($params); | ||
if(count($paramsCount > 1)) { | ||
for($p = 1; $p < $paramsCount; $p++) { | ||
$qPos = strpos($params[$p], 'q='); | ||
if($qPos !== false) { | ||
$qValue = trim(substr($params[$p], $qPos + 2)); | ||
if(is_numeric($qValue)) { | ||
$q = $qValue; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if($params[0][strlen($params[0])-1] === '*') { | ||
$q -= 0.01; | ||
|
||
if($params[0][0] === '*') { | ||
$q -= 0.01; | ||
} | ||
} | ||
|
||
if(!isset($qs[$q])) { | ||
$qs[(string)$q] = array(); | ||
} | ||
|
||
$qs[(string)$q][] = $params[0]; | ||
} | ||
|
||
// Sort keys of q-value in descending order | ||
krsort($qs); | ||
|
||
// Re-key qs 0,1,..N for simple iteration | ||
$this->qs = array_combine(range(0,count($qs)-1), $qs); | ||
} | ||
|
||
} | ||
|
||
?> |
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,52 @@ | ||
<?php | ||
Library::import('recess.http.AcceptsString'); | ||
|
||
class AcceptsStringTest extends PHPUnit_Framework_TestCase { | ||
protected $mediaString = 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'; | ||
protected $media2String = 'image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-shockwave-flash, application/x-silverlight-2-b2, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*'; | ||
protected $languageString = 'en-us,en;q=0.5'; | ||
protected $charsetString = 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'; | ||
protected $specificityString = 'text/*,text/html,text/plain;q=0.9,text/*;q=0.9,*/*;q=0.9'; | ||
|
||
function testMediaString() { | ||
$acceptsString = new AcceptsString($this->mediaString); | ||
$this->assertEquals($acceptsString->next(), array('text/xml','application/xml','application/xhtml+xml','image/png')); | ||
$this->assertEquals($acceptsString->next(), array('text/html')); | ||
$this->assertEquals($acceptsString->next(), array('text/plain')); | ||
$this->assertEquals($acceptsString->next(), array('*/*')); | ||
$this->assertEquals($acceptsString->next(), false); | ||
} | ||
|
||
function testMedia2String() { | ||
$acceptsString = new AcceptsString($this->media2String); | ||
$this->assertEquals($acceptsString->next(), array('image/gif', 'image/jpeg', 'image/pjpeg', 'application/x-ms-application', 'application/vnd.ms-xpsdocument', 'application/xaml+xml', 'application/x-ms-xbap', 'application/x-shockwave-flash', 'application/x-silverlight-2-b2', 'application/x-silverlight', 'application/vnd.ms-excel', 'application/vnd.ms-powerpoint', 'application/msword')); | ||
$this->assertEquals($acceptsString->next(), array('*/*')); | ||
$this->assertEquals($acceptsString->next(), false); | ||
} | ||
|
||
function testLanguageString() { | ||
$acceptsString = new AcceptsString($this->languageString); | ||
$this->assertEquals($acceptsString->next(), array('en-us')); | ||
$this->assertEquals($acceptsString->next(), array('en')); | ||
$this->assertEquals($acceptsString->next(), false); | ||
} | ||
|
||
function testCharsetString() { | ||
$acceptsString = new AcceptsString($this->charsetString); | ||
$this->assertEquals($acceptsString->next(), array('ISO-8859-1')); | ||
$this->assertEquals($acceptsString->next(), array('utf-8')); | ||
$this->assertEquals($acceptsString->next(), array('*')); | ||
$this->assertEquals($acceptsString->next(), false); | ||
} | ||
|
||
function testSpecificity() { | ||
$acceptsString = new AcceptsString($this->specificityString); | ||
$this->assertEquals($acceptsString->next(), array('text/html')); | ||
$this->assertEquals($acceptsString->next(), array('text/*')); | ||
$this->assertEquals($acceptsString->next(), array('text/plain')); | ||
$this->assertEquals($acceptsString->next(), array('text/*')); | ||
$this->assertEquals($acceptsString->next(), array('*/*')); | ||
$this->assertEquals($acceptsString->next(), false); | ||
} | ||
} | ||
?> |
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