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-11-10 10:18:22 +08:00
|
|
|
use Kiri\Router\Constrict\ConstrictRequest;
|
2023-04-15 23:31:16 +08:00
|
|
|
use Kiri\Router\Interface\ValidatorInterface;
|
2023-12-15 16:14:39 +08:00
|
|
|
use Kiri\Router\Validator\RequestFilter\RequiredValidatorFilter;
|
|
|
|
|
use Kiri\Router\Validator\Types\TypesProxy;
|
2023-11-09 22:08:24 +08:00
|
|
|
use Psr\Http\Message\RequestInterface;
|
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
|
|
|
/**
|
2023-12-15 16:14:39 +08:00
|
|
|
* @var array<array<ValidatorInterface|TypesProxy>>
|
2023-08-18 21:08:58 +08:00
|
|
|
*/
|
2023-08-21 17:24:55 +08:00
|
|
|
protected array $rules = [];
|
2023-04-15 23:29:27 +08:00
|
|
|
|
|
|
|
|
|
2023-11-10 10:18:22 +08:00
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2023-08-21 17:24:55 +08:00
|
|
|
protected string $message = '';
|
2023-04-15 23:29:27 +08:00
|
|
|
|
|
|
|
|
|
2023-11-10 10:18:22 +08:00
|
|
|
/**
|
|
|
|
|
* @var object
|
|
|
|
|
*/
|
2023-08-21 17:24:55 +08:00
|
|
|
protected object $formData;
|
2023-04-15 23:31:16 +08:00
|
|
|
|
|
|
|
|
|
2023-12-15 18:52:12 +08:00
|
|
|
/**
|
|
|
|
|
* @var array<string, string>
|
|
|
|
|
*/
|
|
|
|
|
protected array $alias = [];
|
|
|
|
|
|
|
|
|
|
|
2023-12-15 16:14:39 +08:00
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected array $ignoring = [];
|
|
|
|
|
|
|
|
|
|
|
2023-12-04 22:15:18 +08:00
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected array $types = [];
|
|
|
|
|
|
|
|
|
|
|
2023-08-18 21:08:58 +08:00
|
|
|
/**
|
|
|
|
|
* @param object $formData
|
2023-12-04 21:17:04 +08:00
|
|
|
* @return object
|
2023-08-18 21:08:58 +08:00
|
|
|
*/
|
2023-12-04 21:17:04 +08:00
|
|
|
public function setFormData(object $formData): object
|
2023-08-18 21:08:58 +08:00
|
|
|
{
|
|
|
|
|
$this->formData = $formData;
|
2023-12-04 21:17:04 +08:00
|
|
|
return $formData;
|
2023-08-18 21:08:58 +08:00
|
|
|
}
|
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-12-15 18:52:12 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $alias
|
|
|
|
|
* @param string $property
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function setAlias(string $alias, string $property): void
|
|
|
|
|
{
|
|
|
|
|
$this->alias[$alias] = $property;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-18 21:08:58 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $name
|
2023-12-15 16:14:39 +08:00
|
|
|
* @param array $rule
|
2023-08-18 21:08:58 +08:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2023-12-15 16:14:39 +08:00
|
|
|
public function addRule(string $name, array $rule): void
|
2023-08-18 21:08:58 +08:00
|
|
|
{
|
2023-11-09 20:45:16 +08:00
|
|
|
if (!isset($this->rules[$name])) {
|
|
|
|
|
$this->rules[$name] = [];
|
|
|
|
|
}
|
2023-12-15 16:14:39 +08:00
|
|
|
foreach ($rule as $item) {
|
2023-12-15 16:33:36 +08:00
|
|
|
$isFirst = array_pop($item);
|
|
|
|
|
$dispatch = count($item) > 1 ? $item : [$item[0], 'dispatch'];
|
2023-12-15 16:14:39 +08:00
|
|
|
if ($isFirst) {
|
|
|
|
|
array_unshift($this->rules[$name], $dispatch);
|
|
|
|
|
} else {
|
|
|
|
|
$this->rules[$name][] = $dispatch;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-18 21:08:58 +08:00
|
|
|
}
|
2023-04-15 23:31:16 +08:00
|
|
|
|
|
|
|
|
|
2023-05-02 15:28:28 +08:00
|
|
|
/**
|
2023-11-10 10:18:22 +08:00
|
|
|
* @param RequestInterface|ServerRequestInterface|ConstrictRequest $request
|
2023-08-18 21:08:58 +08:00
|
|
|
* @return bool
|
2023-12-12 15:35:35 +08:00
|
|
|
* @throws
|
2023-08-18 21:08:58 +08:00
|
|
|
*/
|
2023-11-10 10:18:22 +08:00
|
|
|
public function run(RequestInterface|ServerRequestInterface|ConstrictRequest $request): bool
|
2023-08-18 21:08:58 +08:00
|
|
|
{
|
2023-12-12 16:25:31 +08:00
|
|
|
$params = !$request->isPost() ? $request->getQueryParams() : $request->getParsedBody();
|
2023-12-15 16:14:39 +08:00
|
|
|
foreach ($this->rules as $name => $rules) {
|
2023-12-15 16:33:36 +08:00
|
|
|
/** @var array<array<TypesProxy,string>> $typeValidator */
|
|
|
|
|
$typeValidator = array_pop($rules);
|
2023-12-18 02:25:34 +08:00
|
|
|
if (!isset($params[$name])) {
|
|
|
|
|
if (!empty($rules) && $rules[0] instanceof RequiredValidatorFilter) {
|
2023-12-15 16:14:39 +08:00
|
|
|
return $this->addError('The request field ' . $name . ' is mandatory and indispensable');
|
2023-12-04 22:05:34 +08:00
|
|
|
}
|
2023-11-10 15:19:51 +08:00
|
|
|
}
|
2023-08-18 21:08:58 +08:00
|
|
|
|
2023-12-18 02:25:34 +08:00
|
|
|
if (!call_user_func($typeValidator, $this->formData, $name, $params[$name] ?? null)) {
|
2023-12-15 16:14:39 +08:00
|
|
|
return $this->addError('The parameter type used in the request field ' . $name . ' is incorrect');
|
|
|
|
|
}
|
2023-08-18 21:08:58 +08:00
|
|
|
|
2023-12-15 16:33:36 +08:00
|
|
|
/** @var array $rule */
|
2023-12-15 16:14:39 +08:00
|
|
|
foreach ($rules as $rule) {
|
2023-12-15 16:33:36 +08:00
|
|
|
if (!call_user_func($rule, $this->formData->{$name})) {
|
2023-12-15 16:14:39 +08:00
|
|
|
return $this->addError('Request field ' . $name . ' value format error');
|
|
|
|
|
}
|
2023-12-13 18:59:56 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-12-15 16:14:39 +08:00
|
|
|
return true;
|
2023-12-13 18:59:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-08-18 21:08:58 +08:00
|
|
|
/**
|
|
|
|
|
* @param $field
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function addError($field): bool
|
|
|
|
|
{
|
2023-12-15 16:14:39 +08:00
|
|
|
$this->message = $field;
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|