Changed to accept language do not return the qualifiers and order the values by the qualifier. Also, making the test case insensitive

This commit is contained in:
Juan Basso 2012-08-28 23:52:05 -04:00
parent 6a95b5746a
commit fd2f3aed46
2 changed files with 66 additions and 30 deletions

View file

@ -694,8 +694,50 @@ class CakeRequest implements ArrayAccess {
* @return array An array of prefValue => array(content/types)
*/
public function parseAccept() {
return $this->_parseAcceptWithQualifier($this->header('accept'));
}
/**
* Get the languages accepted by the client, or check if a specific language is accepted.
*
* Get the list of accepted languages:
*
* {{{ CakeRequest::acceptLanguage(); }}}
*
* Check if a specific language is accepted:
*
* {{{ CakeRequest::acceptLanguage('es-es'); }}}
*
* @param string $language The language to test.
* @return If a $language is provided, a boolean. Otherwise the array of accepted languages.
*/
public static function acceptLanguage($language = null) {
$raw = self::_parseAcceptWithQualifier(self::header('Accept-Language'));
$accept = array();
$header = explode(',', $this->header('accept'));
foreach ($raw as $qualifier => $languages) {
foreach ($languages as &$lang) {
if (strpos($lang, '_')) {
$lang = str_replace('_', '-', $lang);
}
$lang = strtolower($lang);
}
$accept = array_merge($accept, $languages);
}
if ($language === null) {
return $accept;
}
return in_array(strtolower($language), $accept);
}
/**
* Parse Accept* headers with qualifier options
*
* @param string $header
* @return array
*/
protected static function _parseAcceptWithQualifier($header) {
$accept = array();
$header = explode(',', $header);
foreach (array_filter($header) as $value) {
$prefPos = strpos($value, ';');
if ($prefPos !== false) {
@ -716,34 +758,6 @@ class CakeRequest implements ArrayAccess {
return $accept;
}
/**
* Get the languages accepted by the client, or check if a specific language is accepted.
*
* Get the list of accepted languages:
*
* {{{ CakeRequest::acceptLanguage(); }}}
*
* Check if a specific language is accepted:
*
* {{{ CakeRequest::acceptLanguage('es-es'); }}}
*
* @param string $language The language to test.
* @return If a $language is provided, a boolean. Otherwise the array of accepted languages.
*/
public static function acceptLanguage($language = null) {
$accepts = preg_split('/[;,]/', self::header('Accept-Language'));
foreach ($accepts as &$accept) {
$accept = strtolower($accept);
if (strpos($accept, '_') !== false) {
$accept = str_replace('_', '-', $accept);
}
}
if ($language === null) {
return $accepts;
}
return in_array($language, $accepts);
}
/**
* Provides a read/write accessor for `$this->data`. Allows you
* to use a syntax similar to `CakeSession` for reading post data.

View file

@ -1746,19 +1746,41 @@ class CakeRequestTest extends CakeTestCase {
* @return void
*/
public function testAcceptLanguage() {
// Weird language
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca';
$result = CakeRequest::acceptLanguage();
$this->assertEquals(array('inexistent', 'en-ca'), $result, 'Languages do not match');
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx;en_ca';
// No qualifier
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx,en_ca';
$result = CakeRequest::acceptLanguage();
$this->assertEquals(array('es-mx', 'en-ca'), $result, 'Languages do not match');
// With qualifier
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4';
$result = CakeRequest::acceptLanguage();
$this->assertEquals(array('en-us', 'en', 'pt-br', 'pt'), $result, 'Languages do not match');
// With spaces
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'da, en-gb;q=0.8, en;q=0.7';
$result = CakeRequest::acceptLanguage();
$this->assertEquals(array('da', 'en-gb', 'en'), $result, 'Languages do not match');
// Checking if requested
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx,en_ca';
$result = CakeRequest::acceptLanguage();
$result = CakeRequest::acceptLanguage('en-ca');
$this->assertTrue($result);
$result = CakeRequest::acceptLanguage('en-CA');
$this->assertTrue($result);
$result = CakeRequest::acceptLanguage('en-us');
$this->assertFalse($result);
$result = CakeRequest::acceptLanguage('en-US');
$this->assertFalse($result);
}
/**