mirror of
				https://github.com/elyby/accounts.git
				synced 2025-05-31 14:11:46 +05:30 
			
		
		
		
	Allow any valid locale for account lang
This commit is contained in:
		@@ -1,5 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
Yii::setAlias('common', dirname(__DIR__));
 | 
			
		||||
Yii::setAlias('api', dirname(dirname(__DIR__)) . '/api');
 | 
			
		||||
Yii::setAlias('console', dirname(dirname(__DIR__)) . '/console');
 | 
			
		||||
Yii::setAlias('frontend', dirname(dirname(__DIR__)) . '/frontend');
 | 
			
		||||
Yii::setAlias('api', dirname(__DIR__, 2) . '/api');
 | 
			
		||||
Yii::setAlias('console', dirname(__DIR__, 2) . '/console');
 | 
			
		||||
 
 | 
			
		||||
@@ -1,39 +1,37 @@
 | 
			
		||||
<?php
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace common\validators;
 | 
			
		||||
 | 
			
		||||
use common\helpers\Error as E;
 | 
			
		||||
use Yii;
 | 
			
		||||
use Locale;
 | 
			
		||||
use ResourceBundle;
 | 
			
		||||
use yii\validators\Validator;
 | 
			
		||||
 | 
			
		||||
class LanguageValidator extends Validator {
 | 
			
		||||
 | 
			
		||||
    public $message = E::UNSUPPORTED_LANGUAGE;
 | 
			
		||||
 | 
			
		||||
    protected function validateValue($value) {
 | 
			
		||||
    /**
 | 
			
		||||
     * The idea of this validator belongs to
 | 
			
		||||
     * https://github.com/lunetics/LocaleBundle/blob/1f5ee7f1802/Validator/LocaleValidator.php#L82-L88
 | 
			
		||||
     *
 | 
			
		||||
     * @param mixed $value
 | 
			
		||||
     * @return array|null
 | 
			
		||||
     */
 | 
			
		||||
    protected function validateValue($value): ?array {
 | 
			
		||||
        if (empty($value)) {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $files = $this->getFilesNames();
 | 
			
		||||
        if (in_array($value, $files)) {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $primary = Locale::getPrimaryLanguage($value);
 | 
			
		||||
        $region = Locale::getRegion($value);
 | 
			
		||||
        $locales = ResourceBundle::getLocales(''); // http://php.net/manual/ru/resourcebundle.locales.php#115965
 | 
			
		||||
        if (($region !== '' && strtolower($primary) !== strtolower($region)) && !in_array($value, $locales)) {
 | 
			
		||||
            return [$this->message, []];
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    protected function getFilesNames() {
 | 
			
		||||
        $files = array_values(array_filter(scandir($this->getFolderPath()), function(&$value) {
 | 
			
		||||
            return $value !== '..' && $value !== '.';
 | 
			
		||||
        }));
 | 
			
		||||
 | 
			
		||||
        return array_map(function($value) {
 | 
			
		||||
            return basename($value, '.json');
 | 
			
		||||
        }, $files);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function getFolderPath() {
 | 
			
		||||
        return Yii::getAlias('@frontend/src/i18n');
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										15
									
								
								console/migrations/m180708_155425_extends_locale_field.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								console/migrations/m180708_155425_extends_locale_field.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
use console\db\Migration;
 | 
			
		||||
 | 
			
		||||
class m180708_155425_extends_locale_field extends Migration {
 | 
			
		||||
 | 
			
		||||
    public function safeUp() {
 | 
			
		||||
        $this->alterColumn('{{%accounts}}', 'lang', $this->string()->notNull()->defaultValue('en'));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function safeDown() {
 | 
			
		||||
        $this->alterColumn('{{%accounts}}', 'lang', $this->string(5)->notNull()->defaultValue('en'));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										0
									
								
								tests/codeception/common/unit/fixtures/.gitkeep
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								tests/codeception/common/unit/fixtures/.gitkeep
									
									
									
									
									
										Normal file
									
								
							@@ -1,3 +0,0 @@
 | 
			
		||||
{
 | 
			
		||||
    "testString": "testValue"
 | 
			
		||||
}
 | 
			
		||||
@@ -1,3 +0,0 @@
 | 
			
		||||
{
 | 
			
		||||
    "testString": "тестовоеЗначение"
 | 
			
		||||
}
 | 
			
		||||
@@ -1,39 +1,50 @@
 | 
			
		||||
<?php
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace codeception\common\unit\validators;
 | 
			
		||||
 | 
			
		||||
use Codeception\Specify;
 | 
			
		||||
use common\validators\LanguageValidator;
 | 
			
		||||
use tests\codeception\common\_support\ProtectedCaller;
 | 
			
		||||
use tests\codeception\common\unit\TestCase;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @covers \common\validators\LanguageValidator
 | 
			
		||||
 */
 | 
			
		||||
class LanguageValidatorTest extends TestCase {
 | 
			
		||||
    use Specify;
 | 
			
		||||
    use ProtectedCaller;
 | 
			
		||||
 | 
			
		||||
    public function testGetFilesNames() {
 | 
			
		||||
        $model = $this->createModelWithFixturePath();
 | 
			
		||||
        $this->assertEquals(['en', 'ru'], $this->callProtected($model, 'getFilesNames'));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testValidateValueSupportedLanguage() {
 | 
			
		||||
        $model = $this->createModelWithFixturePath();
 | 
			
		||||
        $this->assertNull($this->callProtected($model, 'validateValue', 'ru'));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testValidateNotSupportedLanguage() {
 | 
			
		||||
        $model = $this->createModelWithFixturePath();
 | 
			
		||||
        $this->assertEquals([$model->message, []], $this->callProtected($model, 'validateValue', 'by'));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return LanguageValidator
 | 
			
		||||
     * @param string $locale
 | 
			
		||||
     * @param bool $shouldBeValid
 | 
			
		||||
     *
 | 
			
		||||
     * @dataProvider getTestCases
 | 
			
		||||
     */
 | 
			
		||||
    private function createModelWithFixturePath() {
 | 
			
		||||
        return new class extends LanguageValidator {
 | 
			
		||||
            public function getFolderPath() {
 | 
			
		||||
                return __DIR__ . '/../fixtures/data/i18n';
 | 
			
		||||
    public function testValidate(string $locale, bool $shouldBeValid): void {
 | 
			
		||||
        $validator = new LanguageValidator();
 | 
			
		||||
        $result = $validator->validate($locale, $error);
 | 
			
		||||
        $this->assertSame($shouldBeValid, $result, $locale);
 | 
			
		||||
        if (!$shouldBeValid) {
 | 
			
		||||
            $this->assertSame($validator->message, $error);
 | 
			
		||||
        }
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getTestCases(): array {
 | 
			
		||||
        return [
 | 
			
		||||
            // valid
 | 
			
		||||
            ['de', true],
 | 
			
		||||
            ['de_DE', true],
 | 
			
		||||
            ['deu', true],
 | 
			
		||||
            ['en', true],
 | 
			
		||||
            ['en_US', true],
 | 
			
		||||
            ['fil', true],
 | 
			
		||||
            ['fil_PH', true],
 | 
			
		||||
            ['zh', true],
 | 
			
		||||
            ['zh_Hans_CN', true],
 | 
			
		||||
            ['zh_Hant_HK', true],
 | 
			
		||||
            // invalid
 | 
			
		||||
            ['de_FR', false],
 | 
			
		||||
            ['fr_US', false],
 | 
			
		||||
            ['foo_bar', false],
 | 
			
		||||
            ['foo_bar_baz', false],
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user