2016-05-13 14:33:00 +05:30
|
|
|
<?php
|
2018-07-09 00:51:33 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-05-13 14:33:00 +05:30
|
|
|
namespace common\validators;
|
|
|
|
|
2016-06-17 02:02:23 +05:30
|
|
|
use common\helpers\Error as E;
|
2018-07-09 00:51:33 +05:30
|
|
|
use Locale;
|
|
|
|
use ResourceBundle;
|
2016-05-13 14:33:00 +05:30
|
|
|
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
|
|
|
|
2018-07-09 00:51:33 +05:30
|
|
|
/**
|
|
|
|
* 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 {
|
2016-05-13 14:33:00 +05:30
|
|
|
if (empty($value)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-07-09 00:51:33 +05:30
|
|
|
$primary = Locale::getPrimaryLanguage($value);
|
|
|
|
$region = Locale::getRegion($value);
|
|
|
|
$locales = ResourceBundle::getLocales(''); // http://php.net/manual/ru/resourcebundle.locales.php#115965
|
2024-12-02 15:40:55 +05:30
|
|
|
if (($region !== '' && strtolower((string)$primary) !== strtolower((string)$region)) && !in_array($value, $locales)) {
|
2018-07-09 00:51:33 +05:30
|
|
|
return [$this->message, []];
|
2016-05-13 14:33:00 +05:30
|
|
|
}
|
|
|
|
|
2018-07-09 00:51:33 +05:30
|
|
|
return null;
|
2016-05-13 14:33:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|