Files
kiri-router/src/Validator/Validator.php
T

66 lines
840 B
PHP
Raw Normal View History

2023-04-15 23:29:27 +08:00
<?php
namespace Kiri\Inject\Validator;
use Kiri\Inject\Validator\Inject\ValidatorInterface;
class Validator
{
/**
* @var ValidatorInterface[]
*/
private array $rules = [];
private string $message;
/**
* @param string $name
* @param ValidatorInterface $rule
* @return void
*/
public function addRule(string $name, ValidatorInterface $rule): void
{
$this->rules[$name] = $rule;
}
/**
* @return bool
*/
public function run(): bool
{
foreach ($this->rules as $name => $rule) {
if (!$rule->dispatch($name)) {
return false;
}
}
return true;
}
/**
* @param $field
* @return bool
*/
private function addError($field): bool
{
$this->message = $field . ' error';
return false;
}
/**
* @return string
*/
public function error(): string
{
return $this->message;
}
}