mirror of
https://github.com/elyby/accounts.git
synced 2025-02-23 19:27:34 +05:30
Allow any valid locale for account lang
This commit is contained in:
parent
7f42aadfcf
commit
a0d88e8a8f
@ -1,5 +1,4 @@
|
|||||||
<?php
|
<?php
|
||||||
Yii::setAlias('common', dirname(__DIR__));
|
Yii::setAlias('common', dirname(__DIR__));
|
||||||
Yii::setAlias('api', dirname(dirname(__DIR__)) . '/api');
|
Yii::setAlias('api', dirname(__DIR__, 2) . '/api');
|
||||||
Yii::setAlias('console', dirname(dirname(__DIR__)) . '/console');
|
Yii::setAlias('console', dirname(__DIR__, 2) . '/console');
|
||||||
Yii::setAlias('frontend', dirname(dirname(__DIR__)) . '/frontend');
|
|
||||||
|
@ -1,39 +1,37 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace common\validators;
|
namespace common\validators;
|
||||||
|
|
||||||
use common\helpers\Error as E;
|
use common\helpers\Error as E;
|
||||||
use Yii;
|
use Locale;
|
||||||
|
use ResourceBundle;
|
||||||
use yii\validators\Validator;
|
use yii\validators\Validator;
|
||||||
|
|
||||||
class LanguageValidator extends Validator {
|
class LanguageValidator extends Validator {
|
||||||
|
|
||||||
public $message = E::UNSUPPORTED_LANGUAGE;
|
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)) {
|
if (empty($value)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$files = $this->getFilesNames();
|
$primary = Locale::getPrimaryLanguage($value);
|
||||||
if (in_array($value, $files)) {
|
$region = Locale::getRegion($value);
|
||||||
return null;
|
$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, []];
|
||||||
}
|
}
|
||||||
|
|
||||||
return [$this->message, []];
|
return null;
|
||||||
}
|
|
||||||
|
|
||||||
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');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
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
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace codeception\common\unit\validators;
|
namespace codeception\common\unit\validators;
|
||||||
|
|
||||||
use Codeception\Specify;
|
|
||||||
use common\validators\LanguageValidator;
|
use common\validators\LanguageValidator;
|
||||||
use tests\codeception\common\_support\ProtectedCaller;
|
|
||||||
use tests\codeception\common\unit\TestCase;
|
use tests\codeception\common\unit\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \common\validators\LanguageValidator
|
||||||
|
*/
|
||||||
class LanguageValidatorTest extends TestCase {
|
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() {
|
public function testValidate(string $locale, bool $shouldBeValid): void {
|
||||||
return new class extends LanguageValidator {
|
$validator = new LanguageValidator();
|
||||||
public function getFolderPath() {
|
$result = $validator->validate($locale, $error);
|
||||||
return __DIR__ . '/../fixtures/data/i18n';
|
$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],
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user