|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- <?php
-
-
- namespace yii\validators;
-
- use DateTime;
- use IntlDateFormatter;
- use Yii;
- use yii\base\InvalidConfigException;
- use yii\helpers\FormatConverter;
-
-
- class DateValidator extends Validator
- {
-
-
- const TYPE_DATE = 'date';
-
-
- const TYPE_DATETIME = 'datetime';
-
-
- const TYPE_TIME = 'time';
-
-
-
- public $type = self::TYPE_DATE;
-
-
- public $format;
-
-
- public $locale;
-
-
- public $timeZone;
-
-
- public $timestampAttribute;
-
-
- public $timestampAttributeFormat;
-
-
- public $timestampAttributeTimeZone = 'UTC';
-
-
- public $max;
-
-
- public $min;
-
-
- public $tooBig;
-
-
- public $tooSmall;
-
-
- public $maxString;
-
-
- public $minString;
-
-
-
- private $_dateFormats = [
- 'short' => 3,
- 'medium' => 2,
- 'long' => 1,
- 'full' => 0,
- ];
-
-
-
-
- public function init()
- {
- parent::init();
- if ($this->message === null) {
- $this->message = Yii::t('yii', 'The format of {attribute} is invalid.');
- }
- if ($this->format === null) {
- if ($this->type === self::TYPE_DATE) {
- $this->format = Yii::$app->formatter->dateFormat;
- } elseif ($this->type === self::TYPE_DATETIME) {
- $this->format = Yii::$app->formatter->datetimeFormat;
- } elseif ($this->type === self::TYPE_TIME) {
- $this->format = Yii::$app->formatter->timeFormat;
- } else {
- throw new InvalidConfigException('Unknown validation type set for DateValidator::$type: ' . $this->type);
- }
- }
- if ($this->locale === null) {
- $this->locale = Yii::$app->language;
- }
- if ($this->timeZone === null) {
- $this->timeZone = Yii::$app->timeZone;
- }
- if ($this->min !== null && $this->tooSmall === null) {
- $this->tooSmall = Yii::t('yii', '{attribute} must be no less than {min}.');
- }
- if ($this->max !== null && $this->tooBig === null) {
- $this->tooBig = Yii::t('yii', '{attribute} must be no greater than {max}.');
- }
- if ($this->maxString === null) {
- $this->maxString = (string) $this->max;
- }
- if ($this->minString === null) {
- $this->minString = (string) $this->min;
- }
- if ($this->max !== null && is_string($this->max)) {
- $timestamp = $this->parseDateValue($this->max);
- if ($timestamp === false) {
- throw new InvalidConfigException("Invalid max date value: {$this->max}");
- }
- $this->max = $timestamp;
- }
- if ($this->min !== null && is_string($this->min)) {
- $timestamp = $this->parseDateValue($this->min);
- if ($timestamp === false) {
- throw new InvalidConfigException("Invalid min date value: {$this->min}");
- }
- $this->min = $timestamp;
- }
- }
-
-
-
- public function validateAttribute($model, $attribute)
- {
- $value = $model->$attribute;
- $timestamp = $this->parseDateValue($value);
- if ($timestamp === false) {
- if ($this->timestampAttribute === $attribute) {
- if ($this->timestampAttributeFormat === null) {
- if (is_int($value)) {
- return;
- }
- } else {
- if ($this->parseDateValueFormat($value, $this->timestampAttributeFormat) !== false) {
- return;
- }
- }
- }
- $this->addError($model, $attribute, $this->message, []);
- } elseif ($this->min !== null && $timestamp < $this->min) {
- $this->addError($model, $attribute, $this->tooSmall, ['min' => $this->minString]);
- } elseif ($this->max !== null && $timestamp > $this->max) {
- $this->addError($model, $attribute, $this->tooBig, ['max' => $this->maxString]);
- } elseif ($this->timestampAttribute !== null) {
- if ($this->timestampAttributeFormat === null) {
- $model->{$this->timestampAttribute} = $timestamp;
- } else {
- $model->{$this->timestampAttribute} = $this->formatTimestamp($timestamp, $this->timestampAttributeFormat);
- }
- }
- }
-
-
-
- protected function validateValue($value)
- {
- $timestamp = $this->parseDateValue($value);
- if ($timestamp === false) {
- return [$this->message, []];
- } elseif ($this->min !== null && $timestamp < $this->min) {
- return [$this->tooSmall, ['min' => $this->minString]];
- } elseif ($this->max !== null && $timestamp > $this->max) {
- return [$this->tooBig, ['max' => $this->maxString]];
- } else {
- return null;
- }
- }
-
-
-
- protected function parseDateValue($value)
- {
-
- return $this->parseDateValueFormat($value, $this->format);
- }
-
-
-
- private function parseDateValueFormat($value, $format)
- {
- if (is_array($value)) {
- return false;
- }
- if (strncmp($format, 'php:', 4) === 0) {
- $format = substr($format, 4);
- } else {
- if (extension_loaded('intl')) {
- return $this->parseDateValueIntl($value, $format);
- } else {
-
- $format = FormatConverter::convertDateIcuToPhp($format, 'date');
- }
- }
- return $this->parseDateValuePHP($value, $format);
- }
-
-
-
- private function parseDateValueIntl($value, $format)
- {
- if (isset($this->_dateFormats[$format])) {
- if ($this->type === self::TYPE_DATE) {
- $formatter = new IntlDateFormatter($this->locale, $this->_dateFormats[$format], IntlDateFormatter::NONE, 'UTC');
- } elseif ($this->type === self::TYPE_DATETIME) {
- $formatter = new IntlDateFormatter($this->locale, $this->_dateFormats[$format], $this->_dateFormats[$format], $this->timeZone);
- } elseif ($this->type === self::TYPE_TIME) {
- $formatter = new IntlDateFormatter($this->locale, IntlDateFormatter::NONE, $this->_dateFormats[$format], $this->timeZone);
- } else {
- throw new InvalidConfigException('Unknown validation type set for DateValidator::$type: ' . $this->type);
- }
- } else {
-
- $hasTimeInfo = (strpbrk($format, 'ahHkKmsSA') !== false);
- $formatter = new IntlDateFormatter($this->locale, IntlDateFormatter::NONE, IntlDateFormatter::NONE, $hasTimeInfo ? $this->timeZone : 'UTC', null, $format);
- }
-
- $formatter->setLenient(false);
-
-
-
- $parsePos = 0;
- $parsedDate = @$formatter->parse($value, $parsePos);
- if ($parsedDate === false || $parsePos !== mb_strlen($value, Yii::$app ? Yii::$app->charset : 'UTF-8')) {
- return false;
- }
-
- return $parsedDate;
- }
-
-
-
- private function parseDateValuePHP($value, $format)
- {
-
- $hasTimeInfo = (strpbrk($format, 'HhGgis') !== false);
-
- $date = DateTime::createFromFormat($format, $value, new \DateTimeZone($hasTimeInfo ? $this->timeZone : 'UTC'));
- $errors = DateTime::getLastErrors();
- if ($date === false || $errors['error_count'] || $errors['warning_count']) {
- return false;
- }
-
- if (!$hasTimeInfo) {
- $date->setTime(0, 0, 0);
- }
- return $date->getTimestamp();
- }
-
-
-
- private function formatTimestamp($timestamp, $format)
- {
- if (strncmp($format, 'php:', 4) === 0) {
- $format = substr($format, 4);
- } else {
- $format = FormatConverter::convertDateIcuToPhp($format, 'date');
- }
-
- $date = new DateTime();
- $date->setTimestamp($timestamp);
- $date->setTimezone(new \DateTimeZone($this->timestampAttributeTimeZone));
- return $date->format($format);
- }
- }
|