2023-04-15 23:29:27 +08:00
|
|
|
<?php
|
2023-04-16 01:24:30 +08:00
|
|
|
declare(strict_types=1);
|
2023-04-15 23:29:27 +08:00
|
|
|
|
2023-04-15 23:31:16 +08:00
|
|
|
namespace Kiri\Router\Validator;
|
2023-04-15 23:29:27 +08:00
|
|
|
|
2023-04-15 23:31:16 +08:00
|
|
|
use Kiri\Router\Interface\ValidatorInterface;
|
2023-04-16 00:06:10 +08:00
|
|
|
use Kiri\Router\Request;
|
2023-04-15 23:31:16 +08:00
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2023-04-15 23:29:27 +08:00
|
|
|
|
2023-10-08 18:48:09 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* class Validator
|
|
|
|
|
*/
|
2023-04-15 23:29:27 +08:00
|
|
|
class Validator
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2023-08-18 21:08:58 +08:00
|
|
|
/**
|
|
|
|
|
* @var ValidatorInterface[]
|
|
|
|
|
*/
|
2023-08-21 17:24:55 +08:00
|
|
|
protected array $rules = [];
|
2023-04-15 23:29:27 +08:00
|
|
|
|
|
|
|
|
|
2023-08-21 17:24:55 +08:00
|
|
|
protected string $message = '';
|
2023-04-15 23:29:27 +08:00
|
|
|
|
|
|
|
|
|
2023-08-21 17:24:55 +08:00
|
|
|
protected object $formData;
|
2023-04-15 23:31:16 +08:00
|
|
|
|
|
|
|
|
|
2023-08-18 21:08:58 +08:00
|
|
|
/**
|
|
|
|
|
* @param object $formData
|
|
|
|
|
*/
|
|
|
|
|
public function setFormData(object $formData): void
|
|
|
|
|
{
|
|
|
|
|
$this->formData = $formData;
|
|
|
|
|
}
|
2023-04-16 03:51:18 +08:00
|
|
|
|
|
|
|
|
|
2023-08-18 21:08:58 +08:00
|
|
|
/**
|
|
|
|
|
* @return object
|
|
|
|
|
*/
|
|
|
|
|
public function getFormData(): object
|
|
|
|
|
{
|
|
|
|
|
return $this->formData;
|
|
|
|
|
}
|
2023-04-16 03:51:18 +08:00
|
|
|
|
2023-08-18 21:08:58 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param ValidatorInterface $rule
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function addRule(string $name, ValidatorInterface $rule): void
|
|
|
|
|
{
|
2023-11-09 20:45:16 +08:00
|
|
|
if (!isset($this->rules[$name])) {
|
|
|
|
|
$this->rules[$name] = [];
|
|
|
|
|
}
|
|
|
|
|
$this->rules[$name][] = $rule;
|
2023-08-18 21:08:58 +08:00
|
|
|
}
|
2023-04-15 23:31:16 +08:00
|
|
|
|
|
|
|
|
|
2023-05-02 15:28:28 +08:00
|
|
|
/**
|
|
|
|
|
* @param ServerRequestInterface|Request $request
|
|
|
|
|
* @return Validator
|
|
|
|
|
*/
|
2023-08-18 21:08:58 +08:00
|
|
|
public function bindData(ServerRequestInterface|Request $request): static
|
|
|
|
|
{
|
|
|
|
|
if ($request->isPost) {
|
|
|
|
|
$data = $request->getParsedBody();
|
|
|
|
|
} else {
|
|
|
|
|
$data = $request->getQueryParams();
|
|
|
|
|
}
|
|
|
|
|
foreach ($data as $key => $value) {
|
|
|
|
|
if (property_exists($this->formData, $key)) {
|
2023-05-02 15:28:28 +08:00
|
|
|
$type = new \ReflectionProperty($this->formData, $key);
|
2023-08-18 21:08:58 +08:00
|
|
|
if (!($type->getType() instanceof \ReflectionUnionType)) {
|
|
|
|
|
$value = match ($type->getType()?->getName()) {
|
2023-11-09 20:45:16 +08:00
|
|
|
'int' => (int)$value,
|
2023-08-18 21:08:58 +08:00
|
|
|
'float' => (float)$value,
|
|
|
|
|
default => $value
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-10-08 18:46:55 +08:00
|
|
|
if ($value === 'Null') {
|
2023-10-08 18:46:15 +08:00
|
|
|
$value = null;
|
|
|
|
|
}
|
2023-08-18 21:08:58 +08:00
|
|
|
$this->formData->{$key} = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2023-11-09 20:45:51 +08:00
|
|
|
public function run(): bool
|
2023-08-18 21:08:58 +08:00
|
|
|
{
|
|
|
|
|
foreach ($this->rules as $name => $rule) {
|
2023-11-09 20:45:16 +08:00
|
|
|
foreach ($rule as $item) {
|
|
|
|
|
if (!$item->dispatch($this->formData, $name)) {
|
|
|
|
|
return $this->addError($name);
|
|
|
|
|
}
|
2023-08-18 21:08:58 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $field
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function addError($field): bool
|
|
|
|
|
{
|
2023-11-09 20:45:16 +08:00
|
|
|
$this->message = 'Field ' . $field . ' param format fail.';
|
2023-08-18 21:08:58 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function error(): string
|
|
|
|
|
{
|
|
|
|
|
return $this->message;
|
|
|
|
|
}
|
2023-04-15 23:29:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|