From 470b8244ecc19616245fd803309470c2b983f851 Mon Sep 17 00:00:00 2001 From: Meritoo Date: Sat, 27 Jan 2018 13:05:20 +0100 Subject: [PATCH] Utilities - Locale - getLocale() method - returns locale for given category --- src/Utilities/Locale.php | 21 +++++++++++++++++++++ tests/Utilities/LocaleTest.php | 15 +++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/Utilities/Locale.php b/src/Utilities/Locale.php index ee391a2..bb89a73 100644 --- a/src/Utilities/Locale.php +++ b/src/Utilities/Locale.php @@ -61,6 +61,27 @@ class Locale return setlocale($category, $localeLongForm); } + /** + * Returns locale for given category + * + * @param int $category Named constant specifying the category of the functions affected by the locale setting. + * It's the same constant as required by setlocale() function. + * @return string + * + * Available categories (values of $category argument): + * - LC_ALL for all of the below + * - LC_COLLATE for string comparison, see strcoll() + * - LC_CTYPE for character classification and conversion, for example strtoupper() + * - LC_MONETARY for localeconv() + * - LC_NUMERIC for decimal separator (See also localeconv()) + * - LC_TIME for date and time formatting with strftime() + * - LC_MESSAGES for system responses (available if PHP was compiled with libintl) + */ + public static function getLocale($category) + { + return setlocale($category, '0'); + } + /** * Returns long form of the locale * diff --git a/tests/Utilities/LocaleTest.php b/tests/Utilities/LocaleTest.php index c990235..b695469 100644 --- a/tests/Utilities/LocaleTest.php +++ b/tests/Utilities/LocaleTest.php @@ -75,6 +75,21 @@ class LocaleTest extends BaseTestCase self::assertEquals($expectedLocale, Locale::setLocale($category, $languageCode, $countryCode)); } + /** + * @param int $category Named constant specifying the category of the functions affected by the locale setting. + * It's the same constant as required by setlocale() function. + * @param string $languageCode Language code, in ISO 639-1 format. Short form of the locale, e.g. "fr". + * @param string $countryCode Country code, in ISO 3166-1 alpha-2 format, e.g. "FR" + * @param string $expectedLocale Expected locale + * + * @dataProvider provideCategoryLanguageCodeAndExpectedLocale + */ + public function testGetLocale($category, $languageCode, $countryCode, $expectedLocale) + { + Locale::setLocale($category, $languageCode, $countryCode); + self::assertEquals($expectedLocale, Locale::getLocale($category)); + } + /** * Provides language, encoding and country code *