2016-05-13 14:33:00 +05:30
|
|
|
<?php
|
|
|
|
namespace common\validators;
|
|
|
|
|
2016-06-17 02:02:23 +05:30
|
|
|
use common\helpers\Error as E;
|
2016-05-13 14:33:00 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\validators\Validator;
|
|
|
|
|
|
|
|
class LanguageValidator extends Validator {
|
|
|
|
|
2016-06-17 02:02:23 +05:30
|
|
|
public $message = E::UNSUPPORTED_LANGUAGE;
|
2016-05-13 14:33:00 +05:30
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|