eee
This commit is contained in:
+13
-40
@@ -3,6 +3,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Kiri\Router\Validator;
|
namespace Kiri\Router\Validator;
|
||||||
|
|
||||||
|
use GuzzleHttp\Psr7\ServerRequest;
|
||||||
|
use Kiri\Router\Constrict\ConstrictRequest;
|
||||||
use Kiri\Router\Interface\ValidatorInterface;
|
use Kiri\Router\Interface\ValidatorInterface;
|
||||||
use Kiri\Router\Request;
|
use Kiri\Router\Request;
|
||||||
use Psr\Http\Message\RequestInterface;
|
use Psr\Http\Message\RequestInterface;
|
||||||
@@ -22,9 +24,15 @@ class Validator
|
|||||||
protected array $rules = [];
|
protected array $rules = [];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
protected string $message = '';
|
protected string $message = '';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var object
|
||||||
|
*/
|
||||||
protected object $formData;
|
protected object $formData;
|
||||||
|
|
||||||
|
|
||||||
@@ -60,52 +68,17 @@ class Validator
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ServerRequestInterface|Request $request
|
* @param RequestInterface|ServerRequestInterface|ConstrictRequest $request
|
||||||
* @return Validator
|
|
||||||
*/
|
|
||||||
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)) {
|
|
||||||
$this->addError($key);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
$type = new \ReflectionProperty($this->formData, $key);
|
|
||||||
if (!($type->getType() instanceof \ReflectionUnionType)) {
|
|
||||||
$value = match ($type->getType()?->getName()) {
|
|
||||||
'int' => (int)$value,
|
|
||||||
'float' => (float)$value,
|
|
||||||
default => $value
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if ($value === 'Null') {
|
|
||||||
$value = null;
|
|
||||||
}
|
|
||||||
$this->formData->{$key} = $value;
|
|
||||||
}
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param RequestInterface $request
|
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function run(RequestInterface $request): bool
|
public function run(RequestInterface|ServerRequestInterface|ConstrictRequest $request): bool
|
||||||
{
|
{
|
||||||
if (!empty($this->message)) {
|
if (!empty($this->message)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
$params = !$request->getIsPost() ? $request->getQueryParams() : $request->getParsedBody();
|
||||||
foreach ($this->rules as $name => $rule) {
|
foreach ($this->rules as $name => $rule) {
|
||||||
$value = $request->query($name, null);
|
$value = $params[$name] ?? null;
|
||||||
if ($request->getIsPost()) {
|
|
||||||
$value = $request->post($name, null);
|
|
||||||
}
|
|
||||||
foreach ($rule as $item) {
|
foreach ($rule as $item) {
|
||||||
/** @var ValidatorInterface $item */
|
/** @var ValidatorInterface $item */
|
||||||
if (!$item->dispatch($value, $this->formData)) {
|
if (!$item->dispatch($value, $this->formData)) {
|
||||||
@@ -124,7 +97,7 @@ class Validator
|
|||||||
*/
|
*/
|
||||||
private function addError($field): bool
|
private function addError($field): bool
|
||||||
{
|
{
|
||||||
$this->message = 'Field ' . $field . ' param format fail.';
|
$this->message = 'Field ' . $field . ' value format fail.';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user