Compare commits
48 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2c554e765c | |||
| 21aca8b642 | |||
| 6621c5f247 | |||
| 271e481035 | |||
| 8684ab7045 | |||
| 49cefbd1f9 | |||
| 55a656bb6f | |||
| 320bfb9b64 | |||
| 645d73453c | |||
| d860269a7c | |||
| 26030fd148 | |||
| f194684d4b | |||
| 8f680d5368 | |||
| 0547948b3b | |||
| 80f9bfc443 | |||
| 7579bf8fbe | |||
| 77c2d18bd7 | |||
| d18994fde2 | |||
| 448b1a68fc | |||
| 30b99ad98a | |||
| b2a3c30e24 | |||
| f368ff97ab | |||
| 726742f66f | |||
| 232d0535f5 | |||
| 5fdc507b71 | |||
| a61b0618e0 | |||
| 5f110403aa | |||
| c8194f3089 | |||
| 60cd694671 | |||
| 83e4c8a5ba | |||
| e2bdf81267 | |||
| 74eac11f6c | |||
| a87b6288be | |||
| 0ef74f1096 | |||
| ad4e1743b4 | |||
| e6dc3bf70f | |||
| ff58f2392a | |||
| db725b710d | |||
| 14dd7efda6 | |||
| ba410c90da | |||
| 1acf3fb48b | |||
| 485b39390e | |||
| 9fa7d2828c | |||
| 2288ea4778 | |||
| 84c345375b | |||
| 00a53ba0f3 | |||
| 011e0cd9b8 | |||
| a7531432d4 |
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: whwyy
|
||||||
|
* Date: 2018/4/3 0003
|
||||||
|
* Time: 15:28
|
||||||
|
*/
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace validator;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ArrayValidator
|
||||||
|
* @package validator
|
||||||
|
*/
|
||||||
|
class ArrayValidator extends BaseValidator
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
*
|
||||||
|
* 检查
|
||||||
|
*/
|
||||||
|
public function trigger(mixed $value): bool
|
||||||
|
{
|
||||||
|
return is_array($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace validator;
|
||||||
|
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class \validator\BaseValidator
|
||||||
|
*/
|
||||||
|
abstract class BaseValidator
|
||||||
|
{
|
||||||
|
|
||||||
|
public mixed $value;
|
||||||
|
|
||||||
|
|
||||||
|
protected string $message;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param float $value
|
||||||
|
* @return bool
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function trigger(mixed $value): bool
|
||||||
|
{
|
||||||
|
throw new Exception('Child Class must define method of trigger');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $field
|
||||||
|
* @param string $message
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function addError(string $field, string $message): bool
|
||||||
|
{
|
||||||
|
$this->message = str_replace(':attribute', $field, $message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getError(): string
|
||||||
|
{
|
||||||
|
return $this->message;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: whwyy
|
||||||
|
* Date: 2018/4/3 0003
|
||||||
|
* Time: 15:42
|
||||||
|
*/
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace validator;
|
||||||
|
|
||||||
|
|
||||||
|
class DateTimeValidator extends BaseValidator
|
||||||
|
{
|
||||||
|
|
||||||
|
const string DATE = 'date';
|
||||||
|
const string DATE_TIME = 'datetime';
|
||||||
|
const string TIME = 'time';
|
||||||
|
const string STR_TO_TIME = 'timestamp';
|
||||||
|
|
||||||
|
public string $method;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function trigger(mixed $value): bool
|
||||||
|
{
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $value
|
||||||
|
* @return bool
|
||||||
|
*
|
||||||
|
* 效验分秒 格式如 01:02 or 01-02
|
||||||
|
*/
|
||||||
|
public function validatorTime(string $value): bool
|
||||||
|
{
|
||||||
|
return (bool)preg_match('/^\d{2}:\d{2}(:\d{2})?+(\.\d+)?$/', $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $value
|
||||||
|
* @return bool
|
||||||
|
*
|
||||||
|
* 效验分秒 格式如 2017-12-22 01:02
|
||||||
|
*/
|
||||||
|
public function validateDatetime(string $value): bool
|
||||||
|
{
|
||||||
|
return (bool)preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}(:\d{2})?+(\.\d+)?$/', $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $value
|
||||||
|
* @return bool
|
||||||
|
*
|
||||||
|
* 效验分秒 格式如 2017-12-22
|
||||||
|
*/
|
||||||
|
public function validatorDate(string $value): bool
|
||||||
|
{
|
||||||
|
return (bool)preg_match('/^\d{4}-\d{2}-\d{2}$/', $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string|int $value
|
||||||
|
* @return bool
|
||||||
|
*
|
||||||
|
* 效验时间戳 格式如 1521452254
|
||||||
|
*/
|
||||||
|
public function validatorTimestamp(string|int $value): bool
|
||||||
|
{
|
||||||
|
return (bool)preg_match('/^1\d{9}$/', $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: whwyy
|
||||||
|
* Date: 2018/4/20 0020
|
||||||
|
* Time: 17:32
|
||||||
|
*/
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace validator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class EmailValidator extends BaseValidator
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
* 检查是否存在
|
||||||
|
*/
|
||||||
|
public function trigger(mixed $value): bool
|
||||||
|
{
|
||||||
|
return filter_var($value, FILTER_VALIDATE_EMAIL);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: whwyy
|
||||||
|
* Date: 2018/4/3 0003
|
||||||
|
* Time: 15:46
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
|
||||||
|
namespace validator;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class EmptyValidator extends BaseValidator
|
||||||
|
{
|
||||||
|
|
||||||
|
/** @var string [不能为空] */
|
||||||
|
const string CAN_NOT_EMPTY = 'not empty';
|
||||||
|
|
||||||
|
/** @var string [可为空, 不能为null] */
|
||||||
|
const string CAN_NOT_NULL = 'not null';
|
||||||
|
|
||||||
|
public string $method;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
*
|
||||||
|
* 检查参数是否为NULL
|
||||||
|
*/
|
||||||
|
public function trigger(mixed $value): bool
|
||||||
|
{
|
||||||
|
return match ($this->method) {
|
||||||
|
self::CAN_NOT_NULL => !is_null($value),
|
||||||
|
self::CAN_NOT_EMPTY => !empty($value),
|
||||||
|
default => true
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace validator;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class EnumValidator
|
||||||
|
* @package validator
|
||||||
|
*/
|
||||||
|
class EnumValidator extends BaseValidator
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function trigger(mixed $value): bool
|
||||||
|
{
|
||||||
|
return in_array($value, $this->value);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: whwyy
|
||||||
|
* Date: 2018/4/4 0004
|
||||||
|
* Time: 18:44
|
||||||
|
*/
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace validator;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class IntegerValidator extends BaseValidator
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param float $value
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function trigger(mixed $value): bool
|
||||||
|
{
|
||||||
|
return (float)$value == $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: whwyy
|
||||||
|
* Date: 2018/4/3 0003
|
||||||
|
* Time: 17:04
|
||||||
|
*/
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace validator;
|
||||||
|
|
||||||
|
|
||||||
|
class LengthValidator extends BaseValidator
|
||||||
|
{
|
||||||
|
|
||||||
|
const string MAX_LENGTH = 'max';
|
||||||
|
const string MIN_LENGTH = 'min';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public string $method = 'default';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function trigger(mixed $value): bool
|
||||||
|
{
|
||||||
|
return match ($this->method) {
|
||||||
|
self::MAX_LENGTH => $this->maxLength((string)$value),
|
||||||
|
self::MIN_LENGTH => $this->minLength((string)$value),
|
||||||
|
default => $this->defaultLength((string)$value),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $value
|
||||||
|
* @return bool
|
||||||
|
*
|
||||||
|
* 效验长度是否大于最大长度
|
||||||
|
*/
|
||||||
|
private function maxLength(string $value): bool
|
||||||
|
{
|
||||||
|
return mb_strlen($value) <= $this->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $value
|
||||||
|
* @return bool
|
||||||
|
*
|
||||||
|
* 效验长度是否小于最小长度
|
||||||
|
*/
|
||||||
|
private function minLength(string $value): bool
|
||||||
|
{
|
||||||
|
return mb_strlen($value) >= $this->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $value
|
||||||
|
* @return bool
|
||||||
|
*
|
||||||
|
* 效验长度是否小于最小长度
|
||||||
|
*/
|
||||||
|
private function defaultLength(string $value): bool
|
||||||
|
{
|
||||||
|
return mb_strlen($value) == $this->value;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: whwyy
|
||||||
|
* Date: 2018/4/3 0003
|
||||||
|
* Time: 15:47
|
||||||
|
*/
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace validator;
|
||||||
|
|
||||||
|
|
||||||
|
class RequiredValidator extends BaseValidator
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
* 检查是否存在
|
||||||
|
*/
|
||||||
|
public function trigger(mixed $value): bool
|
||||||
|
{
|
||||||
|
return !is_null($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace validator;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class RoundValidator
|
||||||
|
* @package validator
|
||||||
|
*/
|
||||||
|
class RoundValidator extends BaseValidator
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $field
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function trigger(mixed $value): bool
|
||||||
|
{
|
||||||
|
return round($value, $this->value) == $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: whwyy
|
||||||
|
* Date: 2018/4/4 0004
|
||||||
|
* Time: 18:44
|
||||||
|
*/
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace validator;
|
||||||
|
|
||||||
|
|
||||||
|
use function json_validate;
|
||||||
|
|
||||||
|
class TypesOfValidator extends BaseValidator
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
const string JSON = 'json';
|
||||||
|
const string FLOAT = 'float';
|
||||||
|
const string ARRAY = 'array';
|
||||||
|
const string STRING = 'string';
|
||||||
|
const string INTEGER = 'integer';
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
public string $method;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $field
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function trigger(mixed $value): bool
|
||||||
|
{
|
||||||
|
return match ($this->method) {
|
||||||
|
self::INTEGER => $this->integerFormat($value),
|
||||||
|
self::FLOAT => $this->floatFormat($value),
|
||||||
|
self::JSON => $this->jsonFormat($value),
|
||||||
|
self::STRING => $this->stringFormat($value),
|
||||||
|
self::ARRAY => $this->arrayFormat($value),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string|null $value
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function jsonFormat(?string $value): bool
|
||||||
|
{
|
||||||
|
return json_validate($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function arrayFormat(mixed $value): bool
|
||||||
|
{
|
||||||
|
return is_array($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function stringFormat(mixed $value): bool
|
||||||
|
{
|
||||||
|
return is_string($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function integerFormat(mixed $value): bool
|
||||||
|
{
|
||||||
|
return (int)$value == $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function floatFormat(mixed $value): bool
|
||||||
|
{
|
||||||
|
return (float)$value == $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: qv
|
||||||
|
* Date: 2018/10/16 0016
|
||||||
|
* Time: 10:24
|
||||||
|
*/
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace validator;
|
||||||
|
|
||||||
|
|
||||||
|
use Database\ModelInterface;
|
||||||
|
|
||||||
|
class UniqueValidator extends BaseValidator
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var ModelInterface
|
||||||
|
*/
|
||||||
|
public ModelInterface $model;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public string $field;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
* @throws
|
||||||
|
* 检查是否存在
|
||||||
|
*/
|
||||||
|
public function trigger(mixed $value): bool
|
||||||
|
{
|
||||||
|
return $this->model::query()->where([$this->field => $value])->exists() === false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+117
@@ -0,0 +1,117 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
|
||||||
|
namespace validator;
|
||||||
|
|
||||||
|
|
||||||
|
use Database\ModelInterface;
|
||||||
|
use Kiri;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Validator
|
||||||
|
* @package validator
|
||||||
|
*/
|
||||||
|
class Validator extends BaseValidator
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* classMap
|
||||||
|
*/
|
||||||
|
const array classMap = [
|
||||||
|
'not empty' => ['class' => EmptyValidator::class, 'method' => EmptyValidator::CAN_NOT_EMPTY,],
|
||||||
|
'not null' => ['class' => EmptyValidator::class, 'method' => EmptyValidator::CAN_NOT_NULL,],
|
||||||
|
'required' => ['class' => RequiredValidator::class,],
|
||||||
|
'enum' => ['class' => EnumValidator::class,],
|
||||||
|
'unique' => ['class' => UniqueValidator::class,],
|
||||||
|
'datetime' => ['class' => DateTimeValidator::class, 'method' => DateTimeValidator::DATE_TIME,],
|
||||||
|
'date' => ['class' => DateTimeValidator::class, 'method' => DateTimeValidator::DATE,],
|
||||||
|
'time' => ['class' => DateTimeValidator::class, 'method' => DateTimeValidator::TIME,],
|
||||||
|
'timestamp' => ['class' => DateTimeValidator::class, 'method' => DateTimeValidator::STR_TO_TIME,],
|
||||||
|
'string' => ['class' => TypesOfValidator::class, 'method' => TypesOfValidator::STRING,],
|
||||||
|
'int' => ['class' => TypesOfValidator::class, 'method' => TypesOfValidator::INTEGER,],
|
||||||
|
'min' => ['class' => IntegerValidator::class],
|
||||||
|
'max' => ['class' => IntegerValidator::class],
|
||||||
|
'json' => ['class' => TypesOfValidator::class, 'method' => TypesOfValidator::JSON,],
|
||||||
|
'float' => ['class' => TypesOfValidator::class, 'method' => TypesOfValidator::FLOAT,],
|
||||||
|
'array' => ['class' => TypesOfValidator::class, 'method' => TypesOfValidator::ARRAY,],
|
||||||
|
'maxLength' => ['class' => LengthValidator::class, 'method' => LengthValidator::MAX_LENGTH,],
|
||||||
|
'minLength' => ['class' => LengthValidator::class, 'method' => LengthValidator::MIN_LENGTH,],
|
||||||
|
'email' => ['class' => EmailValidator::class],
|
||||||
|
'length' => ['class' => LengthValidator::class],
|
||||||
|
'round' => ['class' => RoundValidator::class,],
|
||||||
|
];
|
||||||
|
|
||||||
|
/** @var BaseValidator[] */
|
||||||
|
private ?array $validators = [];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ModelInterface $model
|
||||||
|
* @param array $fields
|
||||||
|
* @param array $rules
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function make(ModelInterface $model, array $fields, array $rules): static
|
||||||
|
{
|
||||||
|
foreach ($fields as $field) {
|
||||||
|
if (!isset($this->validators[$field])) {
|
||||||
|
$this->validators[$field] = [];
|
||||||
|
}
|
||||||
|
foreach ($rules as $key => $val) {
|
||||||
|
if (is_numeric($key) && method_exists($model, $val)) {
|
||||||
|
$this->validators[$field][] = [$model, $val];
|
||||||
|
} else {
|
||||||
|
$this->validators[$field][] = $this->mapGen($model, $field, $key, $val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ModelInterface $model
|
||||||
|
* @param $key
|
||||||
|
* @param $val
|
||||||
|
* @return array
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
protected function mapGen(ModelInterface $model, $field, $key, $val): array
|
||||||
|
{
|
||||||
|
if (is_numeric($key)) {
|
||||||
|
$defined = self::classMap[$val];
|
||||||
|
} else {
|
||||||
|
$defined = self::classMap[$key];
|
||||||
|
$defined['value'] = $val;
|
||||||
|
}
|
||||||
|
if ($defined['class'] == UniqueValidator::class) {
|
||||||
|
$defined['model'] = $model;
|
||||||
|
$defined['field'] = $field;
|
||||||
|
}
|
||||||
|
return [Kiri::createObject($defined), 'trigger'];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $attributes
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function validation(array $attributes): bool
|
||||||
|
{
|
||||||
|
if (count($this->validators) < 1) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
foreach ($attributes as $field => $attribute) {
|
||||||
|
if (isset($this->validators[$field])) {
|
||||||
|
$validator = $this->validators[$field];
|
||||||
|
foreach ($validator as $value) {
|
||||||
|
if (!call_user_func($value, $attribute)) {
|
||||||
|
return $this->addError($field, 'field :attribute data format error.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-1
@@ -15,7 +15,7 @@
|
|||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"validator\\": "src/"
|
"validator\\": "./"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
|
|||||||
@@ -1,70 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: whwyy
|
|
||||||
* Date: 2018/4/3 0003
|
|
||||||
* Time: 15:28
|
|
||||||
*/
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace validator;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class ArrayValidator
|
|
||||||
* @package validator
|
|
||||||
*/
|
|
||||||
class ArrayValidator extends BaseValidator
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*
|
|
||||||
* 检查
|
|
||||||
*/
|
|
||||||
public function trigger(): bool
|
|
||||||
{
|
|
||||||
$param = $this->getParams();
|
|
||||||
if (empty($param) || !is_array($param)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (!isset($param[$this->field])) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (!is_array($param[$this->field])) {
|
|
||||||
return $this->addError("The param :attribute must a array");
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $data
|
|
||||||
* @return array
|
|
||||||
*
|
|
||||||
* 转成数组
|
|
||||||
*/
|
|
||||||
private function toArray($data): array
|
|
||||||
{
|
|
||||||
if (is_numeric($data)) {
|
|
||||||
return [];
|
|
||||||
} else if (is_null(json_decode($data, true))) {
|
|
||||||
return [];
|
|
||||||
} elseif (is_object($data)) {
|
|
||||||
$data = get_object_vars($data);
|
|
||||||
}
|
|
||||||
|
|
||||||
$_tmp = [];
|
|
||||||
foreach ($data as $key => $val) {
|
|
||||||
if (is_object($val)) {
|
|
||||||
$_tmp[$key] = $this->toArray($val);
|
|
||||||
} else if (is_array($val)) {
|
|
||||||
$_tmp[$key] = $this->toArray($val);
|
|
||||||
} else {
|
|
||||||
$_tmp[$key] = $val;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $_tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,137 +0,0 @@
|
|||||||
<?php
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace validator;
|
|
||||||
|
|
||||||
|
|
||||||
use Database\Model;
|
|
||||||
use Exception;
|
|
||||||
|
|
||||||
abstract class BaseValidator
|
|
||||||
{
|
|
||||||
|
|
||||||
public string $field = '';
|
|
||||||
|
|
||||||
public array $rules = [];
|
|
||||||
|
|
||||||
public string $method;
|
|
||||||
|
|
||||||
protected bool $isFail = TRUE;
|
|
||||||
|
|
||||||
protected string $message = '';
|
|
||||||
|
|
||||||
protected array $params = [];
|
|
||||||
|
|
||||||
protected ?Model $model = null;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $model
|
|
||||||
*/
|
|
||||||
public function setModel($model)
|
|
||||||
{
|
|
||||||
$this->model = $model;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Model|null
|
|
||||||
*/
|
|
||||||
public function getModel(): ?Model
|
|
||||||
{
|
|
||||||
return $this->model;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* BaseValidator constructor.
|
|
||||||
* @param array $config
|
|
||||||
*/
|
|
||||||
public function __construct(array $config = [])
|
|
||||||
{
|
|
||||||
$this->regConfig($config);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $config
|
|
||||||
*/
|
|
||||||
private function regConfig($config)
|
|
||||||
{
|
|
||||||
if (empty($config) || !is_array($config)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
foreach ($config as $key => $val) {
|
|
||||||
$this->$key = $val;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function trigger(): bool
|
|
||||||
{
|
|
||||||
throw new Exception('Child Class must define method of trigger');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
protected function getParams(): array
|
|
||||||
{
|
|
||||||
return $this->params;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array|null $data
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function setParams(?array $data): static
|
|
||||||
{
|
|
||||||
if (is_null($data)) {
|
|
||||||
$data = [];
|
|
||||||
}
|
|
||||||
$this->params = $data;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $message
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function addError($message): bool
|
|
||||||
{
|
|
||||||
$this->isFail = FALSE;
|
|
||||||
|
|
||||||
$message = str_replace(':attribute', $this->field, $message);
|
|
||||||
|
|
||||||
$this->message = $message;
|
|
||||||
|
|
||||||
return $this->isFail;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getError(): string
|
|
||||||
{
|
|
||||||
return $this->message;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $name
|
|
||||||
* @param $value
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function __set($name, $value)
|
|
||||||
{
|
|
||||||
$method = 'set' . ucfirst($name);
|
|
||||||
if (method_exists($this, $method)) {
|
|
||||||
$this->$method($value);
|
|
||||||
} else if (property_exists($this, $name)) {
|
|
||||||
$this->$name = $value;
|
|
||||||
} else {
|
|
||||||
throw new Exception('unknown property ' . $name . ' in class ' . static::class);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,128 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: whwyy
|
|
||||||
* Date: 2018/4/3 0003
|
|
||||||
* Time: 15:42
|
|
||||||
*/
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace validator;
|
|
||||||
|
|
||||||
|
|
||||||
class DateTimeValidator extends BaseValidator
|
|
||||||
{
|
|
||||||
|
|
||||||
const DATE = 'date';
|
|
||||||
const DATE_TIME = 'datetime';
|
|
||||||
const TIME = 'time';
|
|
||||||
const STR_TO_TIME = 'timestamp';
|
|
||||||
|
|
||||||
public string $method;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function trigger(): bool
|
|
||||||
{
|
|
||||||
$param = $this->getParams();
|
|
||||||
if (empty($param) || !is_array($param)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (!isset($param[$this->field]) || empty($param[$this->field])) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
$value = $param[$this->field];
|
|
||||||
switch (strtolower($this->method)) {
|
|
||||||
case self::DATE:
|
|
||||||
return $this->validatorDate($value);
|
|
||||||
case self::DATE_TIME:
|
|
||||||
return $this->validateDatetime($value);
|
|
||||||
case self::TIME:
|
|
||||||
return $this->validatorTime($value);
|
|
||||||
case self::STR_TO_TIME:
|
|
||||||
return $this->validatorTimestamp($value);
|
|
||||||
default:
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $value
|
|
||||||
* @return bool
|
|
||||||
*
|
|
||||||
* 效验分秒 格式如 01:02 or 01-02
|
|
||||||
*/
|
|
||||||
public function validatorTime($value): bool
|
|
||||||
{
|
|
||||||
if (empty($value) || !is_string($value)) {
|
|
||||||
return $this->addError('The param :attribute not is a date value');
|
|
||||||
}
|
|
||||||
$match = preg_match('/^[0-5]?\d{1}.{1}[0-5]?\d{1}$/', $value, $result);
|
|
||||||
if ($match && $result[0] == $value) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return $this->addError('The param :attribute format error');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $value
|
|
||||||
* @return bool
|
|
||||||
*
|
|
||||||
* 效验分秒 格式如 2017-12-22 01:02
|
|
||||||
*/
|
|
||||||
public function validateDatetime($value): bool
|
|
||||||
{
|
|
||||||
if (empty($value) || !is_string($value)) {
|
|
||||||
return $this->addError('The param :attribute not is a date value');
|
|
||||||
}
|
|
||||||
$match = '/^\d{4}\-\d{2}\-\d{2}\s+\d{2}:\d{2}:\d{2}$/';
|
|
||||||
$match = preg_match($match, $value, $result);
|
|
||||||
if ($match && $result[0] == $value) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return $this->addError('The param :attribute format error');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $value
|
|
||||||
* @return bool
|
|
||||||
*
|
|
||||||
* 效验分秒 格式如 2017-12-22
|
|
||||||
*/
|
|
||||||
public function validatorDate($value): bool
|
|
||||||
{
|
|
||||||
if (empty($value) || !is_string($value)) {
|
|
||||||
return $this->addError('The param :attribute not is a date value');
|
|
||||||
}
|
|
||||||
$match = preg_match('/^(\d{4}).*([0-12]).*([0-31]).*$/', $value, $result);
|
|
||||||
if ($match && $result[0] == $value) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return $this->addError('The param :attribute format error');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $value
|
|
||||||
* @return bool
|
|
||||||
*
|
|
||||||
* 效验时间戳 格式如 1521452254
|
|
||||||
*/
|
|
||||||
public function validatorTimestamp($value): bool
|
|
||||||
{
|
|
||||||
if (empty($value) || !is_numeric($value)) {
|
|
||||||
return $this->addError('The param :attribute not is a timestamp value');
|
|
||||||
}
|
|
||||||
if (strlen((string)$value) != 10) {
|
|
||||||
return $this->addError('The param :attribute not is a timestamp value');
|
|
||||||
}
|
|
||||||
if (!date('YmdHis', $value)) {
|
|
||||||
return $this->addError('The param :attribute format error');
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: whwyy
|
|
||||||
* Date: 2018/4/20 0020
|
|
||||||
* Time: 17:32
|
|
||||||
*/
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace validator;
|
|
||||||
|
|
||||||
|
|
||||||
class EmailValidator extends BaseValidator
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
* 检查是否存在
|
|
||||||
*/
|
|
||||||
public function trigger(): bool
|
|
||||||
{
|
|
||||||
$param = $this->getParams();
|
|
||||||
if (empty($param) || !isset($param[$this->field])) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
$value = $param[$this->field];
|
|
||||||
if (preg_match('/^[a-zA-Z0-9]+([\.\_]{1,})[a-zA-Z0-9]+@[a-zA-Z]+(\.\w+)+/', $value)) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return $this->addError('The param :attribute format error');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: whwyy
|
|
||||||
* Date: 2018/4/3 0003
|
|
||||||
* Time: 15:46
|
|
||||||
*/
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
|
|
||||||
namespace validator;
|
|
||||||
|
|
||||||
|
|
||||||
class EmptyValidator extends BaseValidator
|
|
||||||
{
|
|
||||||
|
|
||||||
/** @var string [不能为空] */
|
|
||||||
const CAN_NOT_EMPTY = 'not empty';
|
|
||||||
|
|
||||||
/** @var string [可为空, 不能为null] */
|
|
||||||
const CAN_NOT_NULL = 'not null';
|
|
||||||
|
|
||||||
public string $method;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*
|
|
||||||
* 检查参数是否为NULL
|
|
||||||
*/
|
|
||||||
public function trigger(): bool
|
|
||||||
{
|
|
||||||
$param = $this->getParams();
|
|
||||||
if (empty($param) || !isset($param[$this->field])) {
|
|
||||||
return $this->addError(':attribute not exists');
|
|
||||||
}
|
|
||||||
|
|
||||||
$value = $param[$this->field];
|
|
||||||
|
|
||||||
switch (strtolower($this->method)) {
|
|
||||||
case self::CAN_NOT_EMPTY:
|
|
||||||
if (strlen($value) < 1) {
|
|
||||||
return $this->addError('The :attribute can not empty.');
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case self::CAN_NOT_NULL:
|
|
||||||
if ($value === null) {
|
|
||||||
return $this->addError('The :attribute can not is null.');
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace validator;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class EnumValidator
|
|
||||||
* @package validator
|
|
||||||
*/
|
|
||||||
class EnumValidator extends BaseValidator
|
|
||||||
{
|
|
||||||
|
|
||||||
public array $value = [];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function trigger(): bool
|
|
||||||
{
|
|
||||||
$param = $this->getParams();
|
|
||||||
if (empty($param) || !isset($param[$this->field])) {
|
|
||||||
return $this->addError('The param :attribute is null');
|
|
||||||
}
|
|
||||||
$value = $param[$this->field];
|
|
||||||
if (is_null($value)) {
|
|
||||||
return $this->addError('The param :attribute is null');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!is_array($this->value)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (!in_array($value, $this->value)) {
|
|
||||||
return $this->addError($this->i());
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
private function i(): string
|
|
||||||
{
|
|
||||||
return 'The param :attribute value only in ' . implode(',', $this->value);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: whwyy
|
|
||||||
* Date: 2018/4/4 0004
|
|
||||||
* Time: 18:44
|
|
||||||
*/
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace validator;
|
|
||||||
|
|
||||||
|
|
||||||
class IntegerValidator extends BaseValidator
|
|
||||||
{
|
|
||||||
|
|
||||||
const MIN = 'min';
|
|
||||||
const MAX = 'max';
|
|
||||||
|
|
||||||
public ?int $value = null;
|
|
||||||
private string $type = '';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function trigger(): bool
|
|
||||||
{
|
|
||||||
$param = $this->getParams();
|
|
||||||
if (empty($param) || !isset($param[$this->field])) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
$value = $param[$this->field] ?? null;
|
|
||||||
if ($value === null) {
|
|
||||||
return $this->addError('The :attribute can not is null.');
|
|
||||||
}
|
|
||||||
if ($this->type !== self::MIN && $value < $this->value) {
|
|
||||||
return $this->addError('The ' . $this->field . ' cannot be less than the default value.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->type !== self::MAX && $value > $this->value) {
|
|
||||||
return $this->addError('The ' . $this->field . ' cannot be greater than the default value.');
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,115 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: whwyy
|
|
||||||
* Date: 2018/4/3 0003
|
|
||||||
* Time: 17:04
|
|
||||||
*/
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace validator;
|
|
||||||
|
|
||||||
|
|
||||||
class LengthValidator extends BaseValidator
|
|
||||||
{
|
|
||||||
|
|
||||||
const MAX_LENGTH = 'max';
|
|
||||||
const MIN_LENGTH = 'min';
|
|
||||||
|
|
||||||
public string $method;
|
|
||||||
|
|
||||||
public int $value;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function trigger(): bool
|
|
||||||
{
|
|
||||||
$param = $this->getParams();
|
|
||||||
if (empty($param) || !isset($param[$this->field])) {
|
|
||||||
if ($this->method != self::MAX_LENGTH) {
|
|
||||||
return $this->addError('The param :attribute not exists');
|
|
||||||
} else {
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$value = $param[$this->field];
|
|
||||||
if (is_null($value)) {
|
|
||||||
return $this->addError('The param :attribute is null');
|
|
||||||
}
|
|
||||||
return match (strtolower($this->method)) {
|
|
||||||
self::MAX_LENGTH => $this->maxLength($value),
|
|
||||||
self::MIN_LENGTH => $this->minLength($value),
|
|
||||||
default => $this->defaultLength($value),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $value
|
|
||||||
* @return bool
|
|
||||||
*
|
|
||||||
* 效验长度是否大于最大长度
|
|
||||||
*/
|
|
||||||
private function maxLength($value): bool
|
|
||||||
{
|
|
||||||
if (is_array($value)) {
|
|
||||||
if (count($value) > $value) {
|
|
||||||
return $this->addError('The param :attribute length overflow');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (is_numeric($value) && strlen((string)$value) > $this->value) {
|
|
||||||
return $this->addError('The param :attribute length overflow');
|
|
||||||
}
|
|
||||||
if (strlen($value) > $this->value) {
|
|
||||||
return $this->addError('The param :attribute length overflow');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $value
|
|
||||||
* @return bool
|
|
||||||
*
|
|
||||||
* 效验长度是否小于最小长度
|
|
||||||
*/
|
|
||||||
private function minLength($value): bool
|
|
||||||
{
|
|
||||||
if (is_array($value)) {
|
|
||||||
if (count($value) < $value) {
|
|
||||||
return $this->addError('The param :attribute length error');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (is_numeric($value) && strlen((string)$value) < $this->value) {
|
|
||||||
return $this->addError('The param :attribute length overflow');
|
|
||||||
}
|
|
||||||
if (strlen($value) < $this->value) {
|
|
||||||
return $this->addError('The param :attribute length error');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $value
|
|
||||||
* @return bool
|
|
||||||
*
|
|
||||||
* 效验长度是否小于最小长度
|
|
||||||
*/
|
|
||||||
private function defaultLength($value): bool
|
|
||||||
{
|
|
||||||
if (is_array($value)) {
|
|
||||||
if (count($value) !== $value) {
|
|
||||||
return $this->addError('The param :attribute length error');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (is_numeric($value) && strlen((string)$value) !== $this->value) {
|
|
||||||
return $this->addError('The param :attribute length overflow');
|
|
||||||
}
|
|
||||||
if (mb_strlen($value) !== $this->value) {
|
|
||||||
return $this->addError('The param :attribute length error; ' . mb_strlen($value) . ':' . $this->value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: whwyy
|
|
||||||
* Date: 2018/4/3 0003
|
|
||||||
* Time: 15:47
|
|
||||||
*/
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace validator;
|
|
||||||
|
|
||||||
|
|
||||||
class RequiredValidator extends BaseValidator
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
* 检查是否存在
|
|
||||||
*/
|
|
||||||
public function trigger(): bool
|
|
||||||
{
|
|
||||||
$param = $this->getParams();
|
|
||||||
if (!is_array($param) || !isset($param[$this->field])) {
|
|
||||||
return $this->addError('The param :attribute not exists');
|
|
||||||
} else {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace validator;
|
|
||||||
|
|
||||||
|
|
||||||
use Exception;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class RoundValidator
|
|
||||||
* @package validator
|
|
||||||
*/
|
|
||||||
class RoundValidator extends BaseValidator
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
public ?int $value = null;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function trigger(): bool
|
|
||||||
{
|
|
||||||
$value = $this->model->getAttribute($this->field);
|
|
||||||
if ($value == null || round($value, $this->value) != $value) {
|
|
||||||
return $this->addError('The param :attribute length error');
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,149 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: whwyy
|
|
||||||
* Date: 2018/4/4 0004
|
|
||||||
* Time: 18:44
|
|
||||||
*/
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace validator;
|
|
||||||
|
|
||||||
|
|
||||||
class TypesOfValidator extends BaseValidator
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
const JSON = 'json';
|
|
||||||
const FLOAT = 'float';
|
|
||||||
const ARRAY = 'array';
|
|
||||||
const STRING = 'string';
|
|
||||||
const INTEGER = 'integer';
|
|
||||||
const SERIALIZE = 'serialize';
|
|
||||||
|
|
||||||
private ?int $min = null;
|
|
||||||
private ?int $max = null;
|
|
||||||
|
|
||||||
/** @var array */
|
|
||||||
public array $types = [
|
|
||||||
self::JSON => 'json',
|
|
||||||
self::FLOAT => 'float',
|
|
||||||
self::ARRAY => 'array',
|
|
||||||
self::STRING => 'string',
|
|
||||||
self::INTEGER => 'integer',
|
|
||||||
self::SERIALIZE => 'serialize',
|
|
||||||
];
|
|
||||||
|
|
||||||
/** @var string */
|
|
||||||
public string $method;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function trigger(): bool
|
|
||||||
{
|
|
||||||
if (!in_array($this->method, $this->types)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
$param = $this->getParams();
|
|
||||||
if (empty($param) || !isset($param[$this->field])) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
$value = $param[$this->field];
|
|
||||||
|
|
||||||
$method = $this->method . 'Format';
|
|
||||||
|
|
||||||
if ($value === null) {
|
|
||||||
return $this->addError('This ' . $this->field . ' is not an empty data.');
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->{$method}($value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $value
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function jsonFormat($value): bool
|
|
||||||
{
|
|
||||||
if (!is_string($value) || is_numeric($value)) {
|
|
||||||
return $this->addError('The ' . $this->field . ' not is JSON data.');
|
|
||||||
}
|
|
||||||
if (is_null(json_decode($value))) {
|
|
||||||
return $this->addError('The ' . $this->field . ' not is JSON data.');
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $value
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function serializeFormat($value): bool
|
|
||||||
{
|
|
||||||
if (!is_string($value) || is_numeric($value)) {
|
|
||||||
return $this->addError('The ' . $this->field . ' not is serialize data.');
|
|
||||||
}
|
|
||||||
if (false === swoole_unserialize($value)) {
|
|
||||||
return $this->addError('The ' . $this->field . ' not is serialize data.');
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $value
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function arrayFormat($value): bool
|
|
||||||
{
|
|
||||||
if (!is_array($value)) {
|
|
||||||
return $this->addError('The ' . $this->field . ' not is array data.');
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $value
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function stringFormat($value): bool
|
|
||||||
{
|
|
||||||
if (is_array($value) || is_object($value) || is_bool($value)) {
|
|
||||||
return $this->addError('The ' . $this->field . ' not is string data.');
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $value
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function integerFormat($value): bool
|
|
||||||
{
|
|
||||||
if (!is_numeric($value)) {
|
|
||||||
return $this->addError('The ' . $this->field . ' not is number data.');
|
|
||||||
}
|
|
||||||
if ((int)$value != $value) {
|
|
||||||
return $this->addError('The ' . $this->field . ' not is number data.');
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $value
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function floatFormat($value): bool
|
|
||||||
{
|
|
||||||
$trim = (float)$value;
|
|
||||||
if ($trim != $value || !is_float($trim)) {
|
|
||||||
return $this->addError('The ' . $this->field . ' not is float data.');
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: qv
|
|
||||||
* Date: 2018/10/16 0016
|
|
||||||
* Time: 10:24
|
|
||||||
*/
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace validator;
|
|
||||||
|
|
||||||
|
|
||||||
class UniqueValidator extends BaseValidator
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
* @throws
|
|
||||||
* 检查是否存在
|
|
||||||
*/
|
|
||||||
public function trigger(): bool
|
|
||||||
{
|
|
||||||
$param = $this->getParams();
|
|
||||||
if (empty($param) || !isset($param[$this->field])) {
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (empty($this->model)) {
|
|
||||||
return $this->addError('Model error.');
|
|
||||||
}
|
|
||||||
|
|
||||||
$model = $this->model;
|
|
||||||
if (!$this->model->getIsCreate()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if ($model::query()->where([$this->field => $param[$this->field]])->exists()) {
|
|
||||||
return $this->addError('The :attribute \'' . $param[$this->field] . '\' is exists!');
|
|
||||||
}
|
|
||||||
return $this->isFail = TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,223 +0,0 @@
|
|||||||
<?php
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
|
|
||||||
namespace validator;
|
|
||||||
|
|
||||||
|
|
||||||
use Closure;
|
|
||||||
use Exception;
|
|
||||||
use Kiri\Kiri;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Validator
|
|
||||||
* @package validator
|
|
||||||
*/
|
|
||||||
class Validator extends BaseValidator
|
|
||||||
{
|
|
||||||
|
|
||||||
/** @var BaseValidator[] */
|
|
||||||
private ?array $validators = [];
|
|
||||||
|
|
||||||
/** @var ?Validator */
|
|
||||||
private static ?Validator $instance = null;
|
|
||||||
|
|
||||||
protected array $classMap = [
|
|
||||||
'not empty' => [
|
|
||||||
'class' => 'validator\EmptyValidator',
|
|
||||||
'method' => EmptyValidator::CAN_NOT_EMPTY,
|
|
||||||
],
|
|
||||||
'not null' => [
|
|
||||||
'class' => 'validator\EmptyValidator',
|
|
||||||
'method' => EmptyValidator::CAN_NOT_NULL,
|
|
||||||
],
|
|
||||||
'required' => [
|
|
||||||
'class' => 'validator\RequiredValidator',
|
|
||||||
],
|
|
||||||
'enum' => [
|
|
||||||
'class' => 'validator\EnumValidator',
|
|
||||||
],
|
|
||||||
'unique' => [
|
|
||||||
'class' => 'validator\UniqueValidator',
|
|
||||||
],
|
|
||||||
'datetime' => [
|
|
||||||
'class' => 'validator\DatetimeValidator',
|
|
||||||
'method' => DateTimeValidator::DATE_TIME,
|
|
||||||
],
|
|
||||||
'date' => [
|
|
||||||
'class' => 'validator\DatetimeValidator',
|
|
||||||
'method' => DateTimeValidator::DATE,
|
|
||||||
],
|
|
||||||
'time' => [
|
|
||||||
'class' => 'validator\DatetimeValidator',
|
|
||||||
'method' => DateTimeValidator::TIME,
|
|
||||||
],
|
|
||||||
'timestamp' => [
|
|
||||||
'class' => 'validator\DatetimeValidator',
|
|
||||||
'method' => DateTimeValidator::STR_TO_TIME,
|
|
||||||
],
|
|
||||||
'string' => [
|
|
||||||
'class' => 'validator\TypesOfValidator',
|
|
||||||
'method' => TypesOfValidator::STRING,
|
|
||||||
],
|
|
||||||
'int' => [
|
|
||||||
'class' => 'validator\TypesOfValidator',
|
|
||||||
'method' => TypesOfValidator::INTEGER,
|
|
||||||
],
|
|
||||||
'min' => [
|
|
||||||
'class' => IntegerValidator::class
|
|
||||||
],
|
|
||||||
'max' => [
|
|
||||||
'class' => IntegerValidator::class
|
|
||||||
],
|
|
||||||
'json' => [
|
|
||||||
'class' => 'validator\TypesOfValidator',
|
|
||||||
'method' => TypesOfValidator::JSON,
|
|
||||||
],
|
|
||||||
'float' => [
|
|
||||||
'class' => 'validator\TypesOfValidator',
|
|
||||||
'method' => TypesOfValidator::FLOAT,
|
|
||||||
],
|
|
||||||
'array' => [
|
|
||||||
'class' => 'validator\TypesOfValidator',
|
|
||||||
'method' => TypesOfValidator::ARRAY,
|
|
||||||
],
|
|
||||||
'serialize' => [
|
|
||||||
'class' => 'validator\TypesOfValidator',
|
|
||||||
'method' => TypesOfValidator::SERIALIZE,
|
|
||||||
],
|
|
||||||
'maxLength' => [
|
|
||||||
'class' => 'validator\LengthValidator',
|
|
||||||
'method' => 'max',
|
|
||||||
],
|
|
||||||
'minLength' => [
|
|
||||||
'class' => 'validator\LengthValidator',
|
|
||||||
'method' => 'min',
|
|
||||||
],
|
|
||||||
'email' => [
|
|
||||||
'class' => 'validator\EmailValidator',
|
|
||||||
'method' => 'email',
|
|
||||||
],
|
|
||||||
'length' => [
|
|
||||||
'class' => 'validator\LengthValidator',
|
|
||||||
'method' => 'default',
|
|
||||||
],
|
|
||||||
'round' => [
|
|
||||||
'class' => 'validator\RoundValidator',
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Validator|null
|
|
||||||
*/
|
|
||||||
public static function getInstance(): ?Validator
|
|
||||||
{
|
|
||||||
if (static::$instance == null) {
|
|
||||||
static::$instance = new Validator();
|
|
||||||
}
|
|
||||||
return static::$instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $field
|
|
||||||
* @param $rules
|
|
||||||
* @return $this
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function make($field, $rules): static
|
|
||||||
{
|
|
||||||
if (!is_array($field)) {
|
|
||||||
$field = [$field];
|
|
||||||
}
|
|
||||||
|
|
||||||
$param = $this->getParams();
|
|
||||||
$model = $this->getModel();
|
|
||||||
foreach ($field as $val) {
|
|
||||||
$this->createRule($val, $rules, $model, $param);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $field
|
|
||||||
* @param $rule
|
|
||||||
* @param $model
|
|
||||||
* @param $param
|
|
||||||
* @throws Exception
|
|
||||||
* ['maxLength'=>150, 'required', 'minLength' => 100]
|
|
||||||
*/
|
|
||||||
public function createRule($field, $rule, $model, $param)
|
|
||||||
{
|
|
||||||
$define = ['field' => $field];
|
|
||||||
|
|
||||||
$is_model = is_null($model);
|
|
||||||
foreach ($rule as $key => $val) {
|
|
||||||
if (!$is_model) {
|
|
||||||
if (is_string($val) && method_exists($model, $val)) {
|
|
||||||
$this->validators[] = [$model, $val];
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (is_string($key)) {
|
|
||||||
$type = strtolower($key);
|
|
||||||
$define['value'] = $val;
|
|
||||||
} else {
|
|
||||||
$type = strtolower($val);
|
|
||||||
}
|
|
||||||
if (!isset($this->classMap[$type])) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$constr = array_merge($this->classMap[$type], $define, [
|
|
||||||
'params' => $param,
|
|
||||||
'model' => $model
|
|
||||||
]);
|
|
||||||
$this->validators[] = $constr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function validation(): bool
|
|
||||||
{
|
|
||||||
if (count($this->validators) < 1) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
foreach ($this->validators as $val) {
|
|
||||||
[$result, $validator] = $this->check($val);
|
|
||||||
if ($result === true) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$isTrue = false;
|
|
||||||
if ($validator instanceof BaseValidator) {
|
|
||||||
$this->addError($validator->getError());
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$this->validators = null;
|
|
||||||
$this->validators = [];
|
|
||||||
return !isset($isTrue);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param BaseValidator|array|Closure $val
|
|
||||||
* @return mixed
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
private function check(BaseValidator|array|Closure $val): mixed
|
|
||||||
{
|
|
||||||
if (is_callable($val, true)) {
|
|
||||||
return [call_user_func($val, $this), $val];
|
|
||||||
}
|
|
||||||
|
|
||||||
$class = Kiri::getDi()->get($val['class']);
|
|
||||||
unset($val['class']);
|
|
||||||
|
|
||||||
Kiri::configure($class, $val);
|
|
||||||
|
|
||||||
return [$class->trigger(), $class];
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user