Permalink
Browse files

Added LocaleInformation for @lsmith77

  • Loading branch information...
1 parent 8e2e902 commit 7149f62c317551dd2284ca8ab5b2e7ae20b5773e @lunetics committed Oct 21, 2012
@@ -0,0 +1,84 @@
+<?php
+/**
+ * This file is part of the LuneticsLocaleBundle package.
+ *
+ * <https://github.com/lunetics/LocaleBundle/>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that is distributed with this source code.
+ */
+namespace Lunetics\LocaleBundle\LocaleInformation;
+
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\Locale\Locale;
+use Lunetics\LocaleBundle\Validator\MetaValidator;
+
+/**
+ * Information about Locales
+ */
+class LocaleInformation
+{
+ private $metaValidator;
+ private $allowedLocales;
+
+ /**
+ * @param MetaValidator $metaValidator Validator
+ * @param array $allowedLocales Allowed locales from config
+ */
+ public function __construct(MetaValidator $metaValidator, $allowedLocales = array())
+ {
+ $this->metaValidator = $metaValidator;
+ $this->allowedLocales = $allowedLocales;
+ }
+
+ /**
+ * Returns the configuration of allowed locales
+ *
+ * @return array
+ */
+ public function getAllowedLocalesFromConfiguration()
+ {
+ return $this->allowedLocales;
+ }
+
+ /**
+ * Returns an array of all allowed locales based on the configuration
+ *
+ * @return array|bool
+ */
+ public function getAllAllowedLocales()
+ {
+ return $this->filterAllowed(Locale::getLocales());
+ }
+
+ /**
+ * Returns an array of all allowed languages based on the configuration
+ *
+ * @return array|bool
+ */
+ public function getAllAllowedLanguages()
+ {
+ return $this->filterAllowed(Locale::getLanguages());
+ }
+
+ /**
+ * Filter function which returns locales / languages
+ *
+ * @param array $localeList
+ *
+ * @return array|bool
+ */
+ private function filterAllowed(array $localeList)
+ {
+ $validator = $this->metaValidator;
+ $matchLocale = function ($locale) use ($validator) {
+ return $validator->isAllowed($locale);
+ };
+ $availableLocales = array_values(array_filter($localeList, $matchLocale));
+ if (!empty($availableLocales)) {
+ return $availableLocales;
+ }
+
+ return false;
+ }
+}
@@ -5,6 +5,7 @@
<parameters>
<parameter key="lunetics_locale.request_listener.class">Lunetics\LocaleBundle\EventListener\LocaleListener</parameter>
<parameter key="lunetics_locale.guesser_manager.class">Lunetics\LocaleBundle\LocaleGuesser\LocaleGuesserManager</parameter>
+ <parameter key="lunetics_locale.information.class">Lunetics\LocaleBundle\LocaleInformaion\LocaleInformation</parameter>
</parameters>
<services>
@@ -13,6 +14,11 @@
<argument type="service" id="logger" />
</service>
+ <service id="lunetics_locale.information" class="%lunetics_locale.information.class%">
+ <argument type="service" id="lunetics_locale.validator.meta" />
+ <argument>%lunetics_locale.allowed_locales%</argument>
+ </service>
+
<service id="lunetics_locale.locale_cookie" class="%lunetics_locale.cookie.class%">
<argument>%lunetics_locale.cookie.name%</argument>
<argument>%lunetics_locale.cookie.ttl%</argument>
@@ -0,0 +1,111 @@
+<?php
+/**
+ * This file is part of the LuneticsLocaleBundle package.
+ *
+ * <https://github.com/lunetics/LocaleBundle/>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that is distributed with this source code.
+ */
+namespace Lunetics\LocaleBundle\Tests\LocaleInformation;
+use Lunetics\LocaleBundle\Tests\Validator\BaseMetaValidator;
+use Lunetics\LocaleBundle\LocaleInformation\LocaleInformation;
+
+/**
+ * Test for the LocaleInformation
+ *
+ * @author Matthias Breddin <mb@lunetics.com>
+ */
+class LocaleInformationTest extends BaseMetaValidator
+{
+ protected $allowedLocales = array('en', 'de', 'fr_CH');
+
+ public function testGetAllowedLocalesFromConfiguration()
+ {
+ $metaValidator = $this->getMetaValidator($this->allowedLocales);
+ $information = new LocaleInformation($metaValidator, $this->allowedLocales);
+ $this->assertSame($this->allowedLocales, $information->getAllowedLocalesFromConfiguration());
+ }
+
+ /**
+ * @param bool $intlExtension
+ *
+ * @dataProvider intlExtensionInstalled
+ */
+ public function testGetAllAllowedLocales($intlExtension)
+ {
+ $metaValidator = $this->getMetaValidator($this->allowedLocales, $intlExtension);
+ $information = new LocaleInformation($metaValidator);
+ $foundLocales = $information->getAllAllowedLocales();
+
+ $this->assertContains('en_GB', $foundLocales);
+ $this->assertContains('en_US', $foundLocales);
+ $this->assertContains('de_CH', $foundLocales);
+ $this->assertContains('de_AT', $foundLocales);
+ $this->assertContains('fr_CH', $foundLocales);
+ $this->assertContains('de', $foundLocales);
+ $this->assertContains('en', $foundLocales);
+ }
+
+ /**
+ * @param bool $intlExtension
+ *
+ * @dataProvider intlExtensionInstalled
+ */
+ public function testGetAllAllowedLocalesStrict($intlExtension)
+ {
+ $metaValidator = $this->getMetaValidator($this->allowedLocales, $intlExtension, true);
+ $information = new LocaleInformation($metaValidator);
+ $foundLocales = $information->getAllAllowedLocales();
+ $this->assertNotContains('en_US', $foundLocales);
+ $this->assertNotContains('de_AT', $foundLocales);
+ $this->assertContains('de', $foundLocales);
+ $this->assertContains('en', $foundLocales);
+ $this->assertContains('fr_CH', $foundLocales);
+ }
+
+ /**
+ * @param bool $intlExtension
+ *
+ * @dataProvider intlExtensionInstalled
+ */
+ public function testGetAllAllowedLocalesLanguageIdenticalToRegion($intlExtension)
+ {
+ $this->markTestSkipped('symfony/locale is buggy');
+ $metaValidator = $this->getMetaValidator($this->allowedLocales, $intlExtension);
+ $information = new LocaleInformation($metaValidator);
+ $foundLocales = $information->getAllAllowedLocales();
+ $this->assertContains('de_DE', $foundLocales);
+ $this->assertContains('fr_FR', $foundLocales);
+ }
+
+ /**
+ * @param bool $intlExtension
+ *
+ * @dataProvider intlExtensionInstalled
+ */
+ public function testGetAllAllowedLanguages($intlExtension)
+ {
+ $metaValidator = $this->getMetaValidator($this->allowedLocales, $intlExtension);
+ $information = new LocaleInformation($metaValidator);
+ $foundLanguages = $information->getAllAllowedLanguages();
+ $this->assertContains('de_CH', $foundLanguages);
+ $this->assertNotContains('de_LI', $foundLanguages);
+ }
+
+ /**
+ * @param bool $intlExtension
+ *
+ * @dataProvider intlExtensionInstalled
+ */
+ public function testGetAllAllowedLanguagesStrict($intlExtension)
+ {
+ $metaValidator = $this->getMetaValidator($this->allowedLocales, $intlExtension, true);
+ $information = new LocaleInformation($metaValidator);
+ $foundLanguages = $information->getAllAllowedLanguages();
+ $this->assertCount(count($this->allowedLocales), $foundLanguages);
+ foreach ($foundLanguages as $locale) {
+ $this->assertContains($locale, $this->allowedLocales);
+ }
+ }
+}

0 comments on commit 7149f62

Please sign in to comment.