105 lines
3.1 KiB
PHP
105 lines
3.1 KiB
PHP
<?php
|
|
namespace App\Core;
|
|
|
|
/**
|
|
* 输入验证器
|
|
*/
|
|
class Validator
|
|
{
|
|
private array $errors = [];
|
|
private array $data;
|
|
|
|
public function __construct(array $data)
|
|
{
|
|
$this->data = $data;
|
|
}
|
|
|
|
/**
|
|
* 验证规则
|
|
*/
|
|
public function validate(array $rules): bool
|
|
{
|
|
foreach ($rules as $field => $ruleSet) {
|
|
$fieldRules = is_string($ruleSet) ? explode('|', $ruleSet) : $ruleSet;
|
|
foreach ($fieldRules as $rule) {
|
|
$params = [];
|
|
if (str_contains($rule, ':')) {
|
|
[$rule, $paramStr] = explode(':', $rule, 2);
|
|
$params = explode(',', $paramStr);
|
|
}
|
|
$method = 'rule' . ucfirst($rule);
|
|
if (method_exists($this, $method)) {
|
|
$value = $this->data[$field] ?? null;
|
|
$this->$method($field, $value, $params);
|
|
}
|
|
}
|
|
}
|
|
return empty($this->errors);
|
|
}
|
|
|
|
public function getErrors(): array
|
|
{
|
|
return $this->errors;
|
|
}
|
|
|
|
public function getFirstError(): string
|
|
{
|
|
return $this->errors[0] ?? '验证失败';
|
|
}
|
|
|
|
private function addError(string $field, string $message): void
|
|
{
|
|
$this->errors[] = $message;
|
|
}
|
|
|
|
private function ruleRequired(string $field, mixed $value, array $params): void
|
|
{
|
|
if ($value === null || $value === '' || (is_array($value) && empty($value))) {
|
|
$this->addError($field, "{$field}不能为空");
|
|
}
|
|
}
|
|
|
|
private function ruleNumeric(string $field, mixed $value, array $params): void
|
|
{
|
|
if ($value !== null && $value !== '' && !is_numeric($value)) {
|
|
$this->addError($field, "{$field}必须是数字");
|
|
}
|
|
}
|
|
|
|
private function ruleInteger(string $field, mixed $value, array $params): void
|
|
{
|
|
if ($value !== null && $value !== '' && !filter_var($value, FILTER_VALIDATE_INT)) {
|
|
$this->addError($field, "{$field}必须是整数");
|
|
}
|
|
}
|
|
|
|
private function ruleEmail(string $field, mixed $value, array $params): void
|
|
{
|
|
if ($value !== null && $value !== '' && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
|
|
$this->addError($field, "{$field}格式不正确");
|
|
}
|
|
}
|
|
|
|
private function ruleMin(string $field, mixed $value, array $params): void
|
|
{
|
|
$min = (int) ($params[0] ?? 0);
|
|
if ($value !== null && $value !== '' && mb_strlen((string)$value) < $min) {
|
|
$this->addError($field, "{$field}长度不能少于{$min}个字符");
|
|
}
|
|
}
|
|
|
|
private function ruleMax(string $field, mixed $value, array $params): void
|
|
{
|
|
$max = (int) ($params[0] ?? 255);
|
|
if ($value !== null && $value !== '' && mb_strlen((string)$value) > $max) {
|
|
$this->addError($field, "{$field}长度不能超过{$max}个字符");
|
|
}
|
|
}
|
|
|
|
private function ruleIn(string $field, mixed $value, array $params): void
|
|
{
|
|
if ($value !== null && $value !== '' && !in_array($value, $params)) {
|
|
$this->addError($field, "{$field}值不在允许范围内");
|
|
}
|
|
}
|
|
} |