From e02691ad276e6d31ce95fb20126a130b742e8cc7 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Thu, 7 Jun 2018 10:22:39 +1200 Subject: [PATCH] BUG Fix invalid locale being set for domain mode Fixes #419 --- src/Middleware/DetectLocaleMiddleware.php | 6 ++++-- src/State/FluentState.php | 15 +++++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Middleware/DetectLocaleMiddleware.php b/src/Middleware/DetectLocaleMiddleware.php index 405659a1..a74520b2 100644 --- a/src/Middleware/DetectLocaleMiddleware.php +++ b/src/Middleware/DetectLocaleMiddleware.php @@ -221,8 +221,10 @@ protected function getDomainLocale() // If the current domain has exactly one locale, the locale is non-ambiguous $locales = Locale::getCached()->filter('DomainID', $domainObj->ID); - if ($locales->count() === 1) { - return $locales->first(); + /** @var Locale $localeObject */ + $localeObject = $locales->first(); + if ($localeObject) { + return $localeObject->getLocale(); } return null; diff --git a/src/State/FluentState.php b/src/State/FluentState.php index 2afe66fe..45a407a6 100644 --- a/src/State/FluentState.php +++ b/src/State/FluentState.php @@ -2,6 +2,7 @@ namespace TractorCow\Fluent\State; +use InvalidArgumentException; use SilverStripe\Core\Injector\Injectable; use SilverStripe\Core\Injector\Injector; @@ -20,9 +21,9 @@ class FluentState protected $locale; /** - * Current domain + * Current domain, if set * - * @var string + * @var string|null */ protected $domain; @@ -58,6 +59,9 @@ public function getLocale() */ public function setLocale($locale) { + if (empty($locale) || !is_string($locale)) { + throw new InvalidArgumentException("Invalid locale"); + } $this->locale = $locale; return $this; } @@ -65,7 +69,7 @@ public function setLocale($locale) /** * Get the current domain code * - * @return string + * @return string|null */ public function getDomain() { @@ -75,11 +79,14 @@ public function getDomain() /** * Set the current domain code * - * @param string $domain + * @param string|null $domain * @return $this */ public function setDomain($domain) { + if ($domain && !is_string($domain)) { + throw new InvalidArgumentException("Invalid domain"); + } $this->domain = $domain; return $this; }