From 02335bd103fd1edae436e387d495102ce2cb5eed Mon Sep 17 00:00:00 2001 From: Florent Viel Date: Thu, 19 Nov 2015 11:09:07 +0100 Subject: [PATCH 1/2] allow to have more than to parts * rename parts to be more understandable * catch accept header with one, two or three parts --- src/Negotiation/AcceptLanguage.php | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Negotiation/AcceptLanguage.php b/src/Negotiation/AcceptLanguage.php index a49b7d7..ad1c032 100644 --- a/src/Negotiation/AcceptLanguage.php +++ b/src/Negotiation/AcceptLanguage.php @@ -6,9 +6,9 @@ final class AcceptLanguage extends BaseAccept implements AcceptHeader { - private $basePart; - - private $subPart; + private $language; + private $script; + private $region; public function __construct($value) { @@ -17,10 +17,14 @@ public function __construct($value) $parts = explode('-', $this->type); if (2 === count($parts)) { - $this->basePart = $parts[0]; - $this->subPart = $parts[1]; + $this->language = $parts[0]; + $this->region = $parts[1]; } elseif (1 === count($parts)) { - $this->basePart = $parts[0]; + $this->language = $parts[0]; + } elseif (3 === count($parts)) { + $this->language = $parts[0]; + $this->script = $parts[1]; + $this->region = $parts[2]; } else { // TODO: this part is never reached... throw new InvalidLanguage(); @@ -32,7 +36,7 @@ public function __construct($value) */ public function getSubPart() { - return $this->subPart; + return $this->region; } /** @@ -40,6 +44,6 @@ public function getSubPart() */ public function getBasePart() { - return $this->basePart; + return $this->language; } } From 58d40eba3cb3fb315f26cf2197952ffdcc775483 Mon Sep 17 00:00:00 2001 From: Florent Viel Date: Thu, 19 Nov 2015 11:10:54 +0100 Subject: [PATCH 2/2] add test case with three parts locale --- tests/Negotiation/Tests/LanguageNegotiatorTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Negotiation/Tests/LanguageNegotiatorTest.php b/tests/Negotiation/Tests/LanguageNegotiatorTest.php index d72b9d5..23fcd16 100644 --- a/tests/Negotiation/Tests/LanguageNegotiatorTest.php +++ b/tests/Negotiation/Tests/LanguageNegotiatorTest.php @@ -48,6 +48,7 @@ public static function dataProviderForTestGetBest() array('en; q=0.1, fr; q=0.4, bu; q=1.0', array('en', 'fr'), 'fr'), array('en; q=0.1, fr; q=0.4, fu; q=0.9, de; q=0.2', array('en', 'fu'), 'fu'), array('', array('en', 'fu'), new InvalidArgument('The header string should not be empty.')), + array('fr, zh-Hans-CN;q=0.3', array('fr'), 'fr'), ); }