From a86bf1a8cdbef70c6f10d961382b941c65ea378b Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 27 Feb 2023 10:21:40 +0900 Subject: [PATCH] feat: add setValidLocales() --- system/HTTP/IncomingRequest.php | 12 ++++++++++++ tests/system/HTTP/IncomingRequestTest.php | 15 +++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/system/HTTP/IncomingRequest.php b/system/HTTP/IncomingRequest.php index 008a865bcdcd..8012df3eb8e3 100755 --- a/system/HTTP/IncomingRequest.php +++ b/system/HTTP/IncomingRequest.php @@ -548,6 +548,18 @@ public function setLocale(string $locale) return $this; } + /** + * Set the valid locales. + * + * @return $this + */ + public function setValidLocales(array $locales) + { + $this->validLocales = $locales; + + return $this; + } + /** * Gets the current locale, with a fallback to the default * locale if none is set. diff --git a/tests/system/HTTP/IncomingRequestTest.php b/tests/system/HTTP/IncomingRequestTest.php index f6994f722ce9..2b61850ec579 100644 --- a/tests/system/HTTP/IncomingRequestTest.php +++ b/tests/system/HTTP/IncomingRequestTest.php @@ -205,6 +205,21 @@ public function testSetBadLocale() $this->assertSame('es', $request->getLocale()); } + public function testSetValidLocales() + { + $config = new App(); + $config->supportedLocales = ['en', 'es']; + $config->defaultLocale = 'es'; + $config->baseURL = 'http://example.com/'; + + $request = new IncomingRequest($config, new URI(), null, new UserAgent()); + + $request->setValidLocales(['ja']); + $request->setLocale('ja'); + + $this->assertSame('ja', $request->getLocale()); + } + /** * @see https://github.com/codeigniter4/CodeIgniter4/issues/2774 */