2022-01-09 14:01:11 +08:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Created by PhpStorm.
|
|
|
|
|
* User: whwyy
|
|
|
|
|
* Date: 2018/4/3 0003
|
|
|
|
|
* Time: 15:42
|
|
|
|
|
*/
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace validator;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DateTimeValidator extends BaseValidator
|
|
|
|
|
{
|
2022-02-18 17:16:45 +08:00
|
|
|
|
2023-12-12 15:35:34 +08:00
|
|
|
const string DATE = 'date';
|
|
|
|
|
const string DATE_TIME = 'datetime';
|
|
|
|
|
const string TIME = 'time';
|
|
|
|
|
const string STR_TO_TIME = 'timestamp';
|
2022-01-09 14:01:11 +08:00
|
|
|
|
2023-12-12 15:35:34 +08:00
|
|
|
public string $method;
|
2022-02-18 17:16:45 +08:00
|
|
|
|
2023-12-12 15:35:34 +08:00
|
|
|
/**
|
2023-12-18 15:01:09 +08:00
|
|
|
* @param mixed $value
|
2023-12-12 15:35:34 +08:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2023-12-19 14:51:13 +08:00
|
|
|
public function trigger(mixed $value): bool
|
2023-12-12 15:35:34 +08:00
|
|
|
{
|
2023-12-18 15:01:09 +08:00
|
|
|
return match ($this->method) {
|
|
|
|
|
self::DATE => $this->validatorDate($value),
|
|
|
|
|
self::DATE_TIME => $this->validateDatetime($value),
|
|
|
|
|
self::TIME => $this->validatorTime($value),
|
|
|
|
|
self::STR_TO_TIME => $this->validatorTimestamp($value),
|
|
|
|
|
default => true,
|
|
|
|
|
};
|
2023-12-12 15:35:34 +08:00
|
|
|
}
|
2022-02-18 17:16:45 +08:00
|
|
|
|
2023-10-24 17:22:29 +08:00
|
|
|
/**
|
2023-12-18 18:24:39 +08:00
|
|
|
* @param string $value
|
2023-10-24 17:22:29 +08:00
|
|
|
* @return bool
|
|
|
|
|
*
|
|
|
|
|
* 效验分秒 格式如 01:02 or 01-02
|
|
|
|
|
*/
|
2023-12-18 18:24:39 +08:00
|
|
|
public function validatorTime(string $value): bool
|
2023-12-12 15:35:34 +08:00
|
|
|
{
|
2023-12-18 15:01:09 +08:00
|
|
|
return (bool)preg_match('/^\d{2}:\d{2}(:\d{2})?+(\.\d+)?$/', $value);
|
2023-12-12 15:35:34 +08:00
|
|
|
}
|
2022-02-18 17:16:45 +08:00
|
|
|
|
|
|
|
|
|
2023-10-24 17:22:29 +08:00
|
|
|
/**
|
2023-12-18 18:24:39 +08:00
|
|
|
* @param string $value
|
2023-10-24 17:22:29 +08:00
|
|
|
* @return bool
|
|
|
|
|
*
|
|
|
|
|
* 效验分秒 格式如 2017-12-22 01:02
|
|
|
|
|
*/
|
2023-12-18 18:24:39 +08:00
|
|
|
public function validateDatetime(string $value): bool
|
2023-12-12 15:35:34 +08:00
|
|
|
{
|
2023-12-18 15:01:09 +08:00
|
|
|
return (bool)preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}(:\d{2})?+(\.\d+)?$/', $value);
|
2023-12-12 15:35:34 +08:00
|
|
|
}
|
2022-02-18 17:16:45 +08:00
|
|
|
|
2023-10-24 17:22:29 +08:00
|
|
|
/**
|
2023-12-18 18:24:39 +08:00
|
|
|
* @param string $value
|
2023-10-24 17:22:29 +08:00
|
|
|
* @return bool
|
|
|
|
|
*
|
|
|
|
|
* 效验分秒 格式如 2017-12-22
|
|
|
|
|
*/
|
2023-12-18 18:24:39 +08:00
|
|
|
public function validatorDate(string $value): bool
|
2023-12-12 15:35:34 +08:00
|
|
|
{
|
2023-12-18 15:01:09 +08:00
|
|
|
return (bool)preg_match('/^\d{4}-\d{2}-\d{2}$/', $value);
|
2023-12-12 15:35:34 +08:00
|
|
|
}
|
2022-02-18 17:16:45 +08:00
|
|
|
|
2023-10-24 17:22:29 +08:00
|
|
|
/**
|
2023-12-18 18:24:39 +08:00
|
|
|
* @param string|int $value
|
2023-10-24 17:22:29 +08:00
|
|
|
* @return bool
|
|
|
|
|
*
|
|
|
|
|
* 效验时间戳 格式如 1521452254
|
|
|
|
|
*/
|
2023-12-18 18:24:39 +08:00
|
|
|
public function validatorTimestamp(string|int $value): bool
|
2023-12-12 15:35:34 +08:00
|
|
|
{
|
2023-12-18 15:01:09 +08:00
|
|
|
return (bool)preg_match('/^1\d{9}$/', $value);
|
2023-12-12 15:35:34 +08:00
|
|
|
}
|
2022-01-09 14:01:11 +08:00
|
|
|
}
|