Skip to content

Commit

Permalink
Merge branch '2.0' into 2.1
Browse files Browse the repository at this point in the history
* 2.0:
  fixed CS
  added doc comments
  [HttpKernel][Translator] Fixed type-hints
  [Translation] forced the catalogue to be regenerated when a resource is added (closes symfony/translation#1)
  [HttpFoundation] Fixed #5611 - Request::splitHttpAcceptHeader incorrect result order.

Conflicts:
	src/Symfony/Component/Process/Process.php
	tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php
  • Loading branch information
fabpot committed Oct 6, 2012
2 parents 64fe72c + 018fcbb commit 5f18e1d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
18 changes: 13 additions & 5 deletions Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -1277,23 +1277,31 @@ public function splitHttpAcceptHeader($header)
}

$values = array();
$groups = array();
foreach (array_filter(explode(',', $header)) as $value) {
// Cut off any q-value that might come after a semi-colon
if (preg_match('/;\s*(q=.*$)/', $value, $match)) {
$q = (float) substr(trim($match[1]), 2);
$q = substr(trim($match[1]), 2);
$value = trim(substr($value, 0, -strlen($match[0])));
} else {
$q = 1;
}

$groups[$q][] = $value;
}

krsort($groups);

foreach ($groups as $q => $items) {
$q = (float) $q;

if (0 < $q) {
$values[trim($value)] = $q;
foreach ($items as $value) {
$values[trim($value)] = $q;
}
}
}

arsort($values);
reset($values);

return $values;
}

Expand Down
16 changes: 14 additions & 2 deletions Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ public function testGetCharsets()

$request = new Request();
$request->headers->set('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7');
$this->assertEquals(array('ISO-8859-1', '*', 'utf-8'), $request->getCharsets());
$this->assertEquals(array('ISO-8859-1', 'utf-8', '*'), $request->getCharsets());
}

public function testGetAcceptableContentTypes()
Expand All @@ -925,7 +925,7 @@ public function testGetAcceptableContentTypes()

$request = new Request();
$request->headers->set('Accept', 'application/vnd.wap.wmlscriptc, text/vnd.wap.wml, application/vnd.wap.xhtml+xml, application/xhtml+xml, text/html, multipart/mixed, */*');
$this->assertEquals(array('multipart/mixed', '*/*', 'text/html', 'application/xhtml+xml', 'text/vnd.wap.wml', 'application/vnd.wap.xhtml+xml', 'application/vnd.wap.wmlscriptc'), $request->getAcceptableContentTypes());
$this->assertEquals(array('application/vnd.wap.wmlscriptc', 'text/vnd.wap.wml', 'application/vnd.wap.xhtml+xml', 'application/xhtml+xml', 'text/html', 'multipart/mixed', '*/*'), $request->getAcceptableContentTypes());
}

public function testGetLanguages()
Expand All @@ -938,6 +938,18 @@ public function testGetLanguages()
$this->assertEquals(array('zh', 'en_US', 'en'), $request->getLanguages());
$this->assertEquals(array('zh', 'en_US', 'en'), $request->getLanguages());

$request = new Request();
$request->headers->set('Accept-language', 'zh, en-us; q=0.6, en; q=0.8');
$this->assertEquals(array('zh', 'en', 'en_US'), $request->getLanguages()); // Test out of order qvalues

$request = new Request();
$request->headers->set('Accept-language', 'zh, en, en-us');
$this->assertEquals(array('zh', 'en', 'en_US'), $request->getLanguages()); // Test equal weighting without qvalues

$request = new Request();
$request->headers->set('Accept-language', 'zh; q=0.6, en, en-us; q=0.6');
$this->assertEquals(array('en', 'zh', 'en_US'), $request->getLanguages()); // Test equal weighting with qvalues

$request = new Request();
$request->headers->set('Accept-language', 'zh, i-cherokee; q=0.6');
$this->assertEquals(array('zh', 'cherokee'), $request->getLanguages());
Expand Down

0 comments on commit 5f18e1d

Please sign in to comment.