2016-05-13 12:03:00 +03:00
|
|
|
<?php
|
2018-07-08 22:21:33 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-05-13 12:03:00 +03:00
|
|
|
namespace common\validators;
|
|
|
|
|
2016-06-16 23:32:23 +03:00
|
|
|
use common\helpers\Error as E;
|
2018-07-08 22:21:33 +03:00
|
|
|
use Locale;
|
|
|
|
use ResourceBundle;
|
2016-05-13 12:03:00 +03:00
|
|
|
use yii\validators\Validator;
|
|
|
|
|
|
|
|
class LanguageValidator extends Validator {
|
|
|
|
|
2016-06-16 23:32:23 +03:00
|
|
|
public $message = E::UNSUPPORTED_LANGUAGE;
|
2016-05-13 12:03:00 +03:00
|
|
|
|
2018-07-08 22:21:33 +03:00
|
|
|
/**
|
|
|
|
* 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 12:03:00 +03:00
|
|
|
if (empty($value)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-07-08 22:21:33 +03:00
|
|
|
$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, []];
|
2016-05-13 12:03:00 +03:00
|
|
|
}
|
|
|
|
|
2018-07-08 22:21:33 +03:00
|
|
|
return null;
|
2016-05-13 12:03:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|