Skip to content
This repository
Browse code

Changed to accept language do not return the qualifiers and order the…

… values by the qualifier. Also, making the test case insensitive
  • Loading branch information...
commit fd2f3aed46122ffaf1c9554f9ab07f1374d0f016 1 parent 6a95b57
Juan Basso authored
68  lib/Cake/Network/CakeRequest.php
@@ -694,26 +694,7 @@ public function accepts($type = null) {
694 694
  * @return array An array of prefValue => array(content/types)
695 695
  */
696 696
 	public function parseAccept() {
697  
-		$accept = array();
698  
-		$header = explode(',', $this->header('accept'));
699  
-		foreach (array_filter($header) as $value) {
700  
-			$prefPos = strpos($value, ';');
701  
-			if ($prefPos !== false) {
702  
-				$prefValue = substr($value, strpos($value, '=') + 1);
703  
-				$value = trim(substr($value, 0, $prefPos));
704  
-			} else {
705  
-				$prefValue = '1.0';
706  
-				$value = trim($value);
707  
-			}
708  
-			if (!isset($accept[$prefValue])) {
709  
-				$accept[$prefValue] = array();
710  
-			}
711  
-			if ($prefValue) {
712  
-				$accept[$prefValue][] = $value;
713  
-			}
714  
-		}
715  
-		krsort($accept);
716  
-		return $accept;
  697
+		return $this->_parseAcceptWithQualifier($this->header('accept'));
717 698
 	}
718 699
 
719 700
 /**
@@ -731,17 +712,50 @@ public function parseAccept() {
731 712
  * @return If a $language is provided, a boolean. Otherwise the array of accepted languages.
732 713
  */
733 714
 	public static function acceptLanguage($language = null) {
734  
-		$accepts = preg_split('/[;,]/', self::header('Accept-Language'));
735  
-		foreach ($accepts as &$accept) {
736  
-			$accept = strtolower($accept);
737  
-			if (strpos($accept, '_') !== false) {
738  
-				$accept = str_replace('_', '-', $accept);
  715
+		$raw = self::_parseAcceptWithQualifier(self::header('Accept-Language'));
  716
+		$accept = array();
  717
+		foreach ($raw as $qualifier => $languages) {
  718
+			foreach ($languages as &$lang) {
  719
+				if (strpos($lang, '_')) {
  720
+					$lang = str_replace('_', '-', $lang);
  721
+				}
  722
+				$lang = strtolower($lang);
739 723
 			}
  724
+			$accept = array_merge($accept, $languages);
740 725
 		}
741 726
 		if ($language === null) {
742  
-			return $accepts;
  727
+			return $accept;
  728
+		}
  729
+		return in_array(strtolower($language), $accept);
  730
+	}
  731
+
  732
+/**
  733
+ * Parse Accept* headers with qualifier options
  734
+ *
  735
+ * @param string $header
  736
+ * @return array
  737
+ */
  738
+	protected static function _parseAcceptWithQualifier($header) {
  739
+		$accept = array();
  740
+		$header = explode(',', $header);
  741
+		foreach (array_filter($header) as $value) {
  742
+			$prefPos = strpos($value, ';');
  743
+			if ($prefPos !== false) {
  744
+				$prefValue = substr($value, strpos($value, '=') + 1);
  745
+				$value = trim(substr($value, 0, $prefPos));
  746
+			} else {
  747
+				$prefValue = '1.0';
  748
+				$value = trim($value);
  749
+			}
  750
+			if (!isset($accept[$prefValue])) {
  751
+				$accept[$prefValue] = array();
  752
+			}
  753
+			if ($prefValue) {
  754
+				$accept[$prefValue][] = $value;
  755
+			}
743 756
 		}
744  
-		return in_array($language, $accepts);
  757
+		krsort($accept);
  758
+		return $accept;
745 759
 	}
746 760
 
747 761
 /**
24  lib/Cake/Test/Case/Network/CakeRequestTest.php
@@ -1746,19 +1746,41 @@ public function testDataWritingFalsey() {
1746 1746
  * @return void
1747 1747
  */
1748 1748
 	public function testAcceptLanguage() {
  1749
+		// Weird language
1749 1750
 		$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca';
1750 1751
 		$result = CakeRequest::acceptLanguage();
1751 1752
 		$this->assertEquals(array('inexistent', 'en-ca'), $result, 'Languages do not match');
1752 1753
 
1753  
-		$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx;en_ca';
  1754
+		// No qualifier
  1755
+		$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx,en_ca';
1754 1756
 		$result = CakeRequest::acceptLanguage();
1755 1757
 		$this->assertEquals(array('es-mx', 'en-ca'), $result, 'Languages do not match');
1756 1758
 
  1759
+		// With qualifier
  1760
+		$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4';
  1761
+		$result = CakeRequest::acceptLanguage();
  1762
+		$this->assertEquals(array('en-us', 'en', 'pt-br', 'pt'), $result, 'Languages do not match');
  1763
+
  1764
+		// With spaces
  1765
+		$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'da, en-gb;q=0.8, en;q=0.7';
  1766
+		$result = CakeRequest::acceptLanguage();
  1767
+		$this->assertEquals(array('da', 'en-gb', 'en'), $result, 'Languages do not match');
  1768
+
  1769
+		// Checking if requested
  1770
+		$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx,en_ca';
  1771
+		$result = CakeRequest::acceptLanguage();
  1772
+
1757 1773
 		$result = CakeRequest::acceptLanguage('en-ca');
1758 1774
 		$this->assertTrue($result);
1759 1775
 
  1776
+		$result = CakeRequest::acceptLanguage('en-CA');
  1777
+		$this->assertTrue($result);
  1778
+
1760 1779
 		$result = CakeRequest::acceptLanguage('en-us');
1761 1780
 		$this->assertFalse($result);
  1781
+
  1782
+		$result = CakeRequest::acceptLanguage('en-US');
  1783
+		$this->assertFalse($result);
1762 1784
 	}
1763 1785
 
1764 1786
 /**

0 notes on commit fd2f3ae

Please sign in to comment.
Something went wrong with that request. Please try again.