You are in: home / community / @wezfurlong
Software Architect, OpenSourceror
Nice double-line spacing there :-/Posted in The Accept Header. Sun, 05 Jun 2011 at 01:06:57 GMT
Nice double-line spacing there :-/
Posted in The Accept Header.
Sun, 05 Jun 2011 at 01:06:57 GMT
Just so happens that I'm parsing this stuff for a hobby project this weekend; I have the following code snippet for PHP 5.3 that populates an array in order of preference. Feel free to take and adapt as appropriate. <?php protected function parseAcceptHeader() { $hdr = $this->headers['Accept']; $accept = array(); foreach (preg_split('/\s*,\s*/', $hdr) as $i => $term) { $o = new \stdclass; $o->pos = $i; if (preg_match(",^(\S+)\s*;\s*(?:q|level)=([0-9\.]+),i", $term, $M)) { $o->type = $M[1]; $o->q = (double)$M[2]; } else { $o->type = $term; $o->q = 1; } $accept[] = $o; } usort($accept, function ($a, $b) { /* first tier: highest q factor wins */ $diff = $b->q - $a->q; if ($diff > 0) { $diff = 1; } else if ($diff < 0) { $diff = -1; } else { /* tie-breaker: first listed item wins */ $diff = $a->pos - $b->pos; } return $diff; }); $this->accept = array(); foreach ($accept as $a) { $this->accept[$a->type] = $a->type; } } ?> And accompanying unit test: <?php function testAcceptHeader() { $c = new MVC\Controller('', '', array(), array('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json')); $this->assertEquals($c->accept, array( "text/html" => "text/html", "application/xhtml+xml" => "application/xhtml+xml", "application/json" => "application/json", "application/xml" => "application/xml", "*/*" => "*/*" )); } ?>Posted in The Accept Header. Sun, 05 Jun 2011 at 01:06:17 GMT
Just so happens that I'm parsing this stuff for a hobby project this weekend; I have the following code snippet for PHP 5.3 that populates an array in order of preference.
Feel free to take and adapt as appropriate.
<?php protected function parseAcceptHeader() { $hdr = $this->headers['Accept']; $accept = array(); foreach (preg_split('/\s*,\s*/', $hdr) as $i => $term) { $o = new \stdclass; $o->pos = $i; if (preg_match(",^(\S+)\s*;\s*(?:q|level)=([0-9\.]+),i", $term, $M)) { $o->type = $M[1]; $o->q = (double)$M[2]; } else { $o->type = $term; $o->q = 1; } $accept[] = $o; } usort($accept, function ($a, $b) { /* first tier: highest q factor wins */ $diff = $b->q - $a->q; if ($diff > 0) { $diff = 1; } else if ($diff < 0) { $diff = -1; } else { /* tie-breaker: first listed item wins */ $diff = $a->pos - $b->pos; } return $diff; }); $this->accept = array(); foreach ($accept as $a) { $this->accept[$a->type] = $a->type; } } ?>
<?php
protected function parseAcceptHeader() {
$hdr = $this->headers['Accept'];
$accept = array();
foreach (preg_split('/\s*,\s*/', $hdr) as $i => $term) {
$o = new \stdclass;
$o->pos = $i;
if (preg_match(",^(\S+)\s*;\s*(?:q|level)=([0-9\.]+),i", $term, $M)) {
$o->type = $M[1];
$o->q = (double)$M[2];
} else {
$o->type = $term;
$o->q = 1;
}
$accept[] = $o;
usort($accept, function ($a, $b) {
/* first tier: highest q factor wins */
$diff = $b->q - $a->q;
if ($diff > 0) {
$diff = 1;
} else if ($diff < 0) {
$diff = -1;
/* tie-breaker: first listed item wins */
$diff = $a->pos - $b->pos;
return $diff;
});
$this->accept = array();
foreach ($accept as $a) {
$this->accept[$a->type] = $a->type;
?>
And accompanying unit test:
<?php function testAcceptHeader() { $c = new MVC\Controller('', '', array(), array('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json')); $this->assertEquals($c->accept, array( "text/html" => "text/html", "application/xhtml+xml" => "application/xhtml+xml", "application/json" => "application/json", "application/xml" => "application/xml", "*/*" => "*/*" )); } ?>
function testAcceptHeader() {
$c = new MVC\Controller('', '', array(), array('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json'));
$this->assertEquals($c->accept, array(
"text/html" => "text/html",
"application/xhtml+xml" => "application/xhtml+xml",
"application/json" => "application/json",
"application/xml" => "application/xml",
"*/*" => "*/*"
));
Sun, 05 Jun 2011 at 01:06:17 GMT
Latest Comments
1
2