mirror of
https://github.com/elyby/accounts.git
synced 2024-11-10 07:22:00 +05:30
39 lines
871 B
PHP
39 lines
871 B
PHP
|
<?php
|
||
|
namespace common\validators;
|
||
|
|
||
|
use Yii;
|
||
|
use yii\validators\Validator;
|
||
|
|
||
|
class LanguageValidator extends Validator {
|
||
|
|
||
|
public $message = 'error.unsupported_language';
|
||
|
|
||
|
protected function validateValue($value) {
|
||
|
if (empty($value)) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
$files = $this->getFilesNames();
|
||
|
if (in_array($value, $files)) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
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');
|
||
|
}
|
||
|
|
||
|
}
|