This commit is contained in:
xl
2023-11-09 22:08:24 +08:00
parent 1112341e3f
commit 0db2958351
18 changed files with 121 additions and 297 deletions
+2 -2
View File
@@ -8,10 +8,10 @@ interface ValidatorInterface
/** /**
* @param mixed $data
* @param object $class * @param object $class
* @param string $name
* @return bool * @return bool
*/ */
public function dispatch(object $class, string $name): bool; public function dispatch(mixed $data, object $class): bool;
} }
+2 -16
View File
@@ -3,22 +3,13 @@ declare(strict_types=1);
namespace Kiri\Router\Validator\Inject; namespace Kiri\Router\Validator\Inject;
use Kiri\Di\Inject\Container;
use Kiri\Router\Interface\ValidatorInterface; use Kiri\Router\Interface\ValidatorInterface;
use Psr\Http\Message\RequestInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)] #[\Attribute(\Attribute::TARGET_PROPERTY)]
class Email implements ValidatorInterface class Email implements ValidatorInterface
{ {
/**
* @var RequestInterface
*/
#[Container(RequestInterface::class)]
public RequestInterface $request;
/** /**
* @return string * @return string
*/ */
@@ -29,17 +20,12 @@ class Email implements ValidatorInterface
/** /**
* @param mixed $data
* @param object $class * @param object $class
* @param string $name
* @return bool * @return bool
*/ */
public function dispatch(object $class, string $name): bool public function dispatch(mixed $data, object $class): bool
{ {
if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
if ($data === null) { if ($data === null) {
return false; return false;
} }
+2 -2
View File
@@ -11,11 +11,11 @@ class Ignore implements ValidatorInterface
/** /**
* @param mixed $data
* @param object $class * @param object $class
* @param string $name
* @return bool * @return bool
*/ */
public function dispatch(object $class, string $name): bool public function dispatch(mixed $data, object $class): bool
{ {
return true; return true;
} }
+2 -16
View File
@@ -3,22 +3,13 @@ declare(strict_types=1);
namespace Kiri\Router\Validator\Inject; namespace Kiri\Router\Validator\Inject;
use Kiri\Di\Inject\Container;
use Kiri\Router\Interface\ValidatorInterface; use Kiri\Router\Interface\ValidatorInterface;
use Psr\Http\Message\RequestInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)] #[\Attribute(\Attribute::TARGET_PROPERTY)]
class In implements ValidatorInterface class In implements ValidatorInterface
{ {
/**
* @var RequestInterface
*/
#[Container(RequestInterface::class)]
public RequestInterface $request;
/** /**
* @param array $value * @param array $value
*/ */
@@ -28,17 +19,12 @@ class In implements ValidatorInterface
/** /**
* @param mixed $data
* @param object $class * @param object $class
* @param string $name
* @return bool * @return bool
*/ */
public function dispatch(object $class, string $name): bool public function dispatch(mixed $data, object $class): bool
{ {
if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
if ($data === null) { if ($data === null) {
return false; return false;
} }
+2 -16
View File
@@ -3,20 +3,12 @@ declare(strict_types=1);
namespace Kiri\Router\Validator\Inject; namespace Kiri\Router\Validator\Inject;
use Kiri\Di\Inject\Container;
use Kiri\Router\Interface\ValidatorInterface; use Kiri\Router\Interface\ValidatorInterface;
use Psr\Http\Message\RequestInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)] #[\Attribute(\Attribute::TARGET_PROPERTY)]
class Length implements ValidatorInterface class Length implements ValidatorInterface
{ {
/**
* @var RequestInterface
*/
#[Container(RequestInterface::class)]
public RequestInterface $request;
/** /**
* @param int $value * @param int $value
@@ -27,18 +19,12 @@ class Length implements ValidatorInterface
/** /**
* @param mixed $data
* @param object $class * @param object $class
* @param string $name
* @return bool * @return bool
*/ */
public function dispatch(object $class, string $name): bool public function dispatch(mixed $data, object $class): bool
{ {
// TODO: Implement dispatch() method.
if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
if ($data === null) { if ($data === null) {
return false; return false;
} }
+2 -16
View File
@@ -3,20 +3,12 @@ declare(strict_types=1);
namespace Kiri\Router\Validator\Inject; namespace Kiri\Router\Validator\Inject;
use Kiri\Di\Inject\Container;
use Kiri\Router\Interface\ValidatorInterface; use Kiri\Router\Interface\ValidatorInterface;
use Psr\Http\Message\RequestInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)] #[\Attribute(\Attribute::TARGET_PROPERTY)]
class Max implements ValidatorInterface class Max implements ValidatorInterface
{ {
/**
* @var RequestInterface
*/
#[Container(RequestInterface::class)]
public RequestInterface $request;
/** /**
* @param int $value * @param int $value
@@ -27,18 +19,12 @@ class Max implements ValidatorInterface
/** /**
* @param mixed $data
* @param object $class * @param object $class
* @param string $name
* @return bool * @return bool
*/ */
public function dispatch(object $class, string $name): bool public function dispatch(mixed $data, object $class): bool
{ {
// TODO: Implement dispatch() method.
if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
if ($data === null) { if ($data === null) {
return false; return false;
} }
+2 -17
View File
@@ -3,22 +3,13 @@ declare(strict_types=1);
namespace Kiri\Router\Validator\Inject; namespace Kiri\Router\Validator\Inject;
use Kiri\Di\Inject\Container;
use Kiri\Router\Interface\ValidatorInterface; use Kiri\Router\Interface\ValidatorInterface;
use Psr\Http\Message\RequestInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)] #[\Attribute(\Attribute::TARGET_PROPERTY)]
class MaxLength implements ValidatorInterface class MaxLength implements ValidatorInterface
{ {
/**
* @var RequestInterface
*/
#[Container(RequestInterface::class)]
public RequestInterface $request;
/** /**
* @param int $value * @param int $value
@@ -29,18 +20,12 @@ class MaxLength implements ValidatorInterface
/** /**
* @param mixed $data
* @param object $class * @param object $class
* @param string $name
* @return bool * @return bool
*/ */
public function dispatch(object $class, string $name): bool public function dispatch(mixed $data, object $class): bool
{ {
// TODO: Implement dispatch() method.
if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
if ($data === null) { if ($data === null) {
return false; return false;
} }
+2 -17
View File
@@ -3,22 +3,13 @@ declare(strict_types=1);
namespace Kiri\Router\Validator\Inject; namespace Kiri\Router\Validator\Inject;
use Kiri\Di\Inject\Container;
use Kiri\Router\Interface\ValidatorInterface; use Kiri\Router\Interface\ValidatorInterface;
use Psr\Http\Message\RequestInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)] #[\Attribute(\Attribute::TARGET_PROPERTY)]
class Min implements ValidatorInterface class Min implements ValidatorInterface
{ {
/**
* @var RequestInterface
*/
#[Container(RequestInterface::class)]
public RequestInterface $request;
/** /**
* @param int $value * @param int $value
*/ */
@@ -28,18 +19,12 @@ class Min implements ValidatorInterface
/** /**
* @param mixed $data
* @param object $class * @param object $class
* @param string $name
* @return bool * @return bool
*/ */
public function dispatch(object $class, string $name): bool public function dispatch(mixed $data, object $class): bool
{ {
// TODO: Implement dispatch() method.
if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
if ($data === null) { if ($data === null) {
return false; return false;
} }
+2 -17
View File
@@ -3,22 +3,13 @@ declare(strict_types=1);
namespace Kiri\Router\Validator\Inject; namespace Kiri\Router\Validator\Inject;
use Kiri\Di\Inject\Container;
use Kiri\Router\Interface\ValidatorInterface; use Kiri\Router\Interface\ValidatorInterface;
use Psr\Http\Message\RequestInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)] #[\Attribute(\Attribute::TARGET_PROPERTY)]
class MinLength implements ValidatorInterface class MinLength implements ValidatorInterface
{ {
/**
* @var RequestInterface
*/
#[Container(RequestInterface::class)]
public RequestInterface $request;
/** /**
* @param int $value * @param int $value
*/ */
@@ -28,18 +19,12 @@ class MinLength implements ValidatorInterface
/** /**
* @param mixed $data
* @param object $class * @param object $class
* @param string $name
* @return bool * @return bool
*/ */
public function dispatch(object $class, string $name): bool public function dispatch(mixed $data, object $class): bool
{ {
// TODO: Implement dispatch() method.
if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
if ($data === null) { if ($data === null) {
return false; return false;
} }
+2 -17
View File
@@ -4,21 +4,12 @@ declare(strict_types=1);
namespace Kiri\Router\Validator\Inject; namespace Kiri\Router\Validator\Inject;
use Kiri\Di\Inject\Container;
use Kiri\Router\Interface\ValidatorInterface; use Kiri\Router\Interface\ValidatorInterface;
use Psr\Http\Message\RequestInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)] #[\Attribute(\Attribute::TARGET_PROPERTY)]
class Must implements ValidatorInterface class Must implements ValidatorInterface
{ {
/**
* @var RequestInterface
*/
#[Container(RequestInterface::class)]
public RequestInterface $request;
/** /**
* @param mixed $value * @param mixed $value
*/ */
@@ -28,18 +19,12 @@ class Must implements ValidatorInterface
/** /**
* @param mixed $data
* @param object $class * @param object $class
* @param string $name
* @return bool * @return bool
*/ */
public function dispatch(object $class, string $name): bool public function dispatch(mixed $data, object $class): bool
{ {
// TODO: Implement dispatch() method.
if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
return $data === $this->value; return $data === $this->value;
} }
+2 -17
View File
@@ -3,9 +3,7 @@ declare(strict_types=1);
namespace Kiri\Router\Validator\Inject; namespace Kiri\Router\Validator\Inject;
use Kiri\Di\Inject\Container;
use Kiri\Router\Interface\ValidatorInterface; use Kiri\Router\Interface\ValidatorInterface;
use Psr\Http\Message\RequestInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)] #[\Attribute(\Attribute::TARGET_PROPERTY)]
class NotEmpty implements ValidatorInterface class NotEmpty implements ValidatorInterface
@@ -13,25 +11,12 @@ class NotEmpty implements ValidatorInterface
/** /**
* @var RequestInterface * @param mixed $data
*/
#[Container(RequestInterface::class)]
public RequestInterface $request;
/**
* @param object $class * @param object $class
* @param string $name
* @return bool * @return bool
*/ */
public function dispatch(object $class, string $name): bool public function dispatch(mixed $data, object $class): bool
{ {
// TODO: Implement dispatch() method.
if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
return !empty($data); return !empty($data);
} }
} }
+2 -18
View File
@@ -3,9 +3,7 @@ declare(strict_types=1);
namespace Kiri\Router\Validator\Inject; namespace Kiri\Router\Validator\Inject;
use Kiri\Di\Inject\Container;
use Kiri\Router\Interface\ValidatorInterface; use Kiri\Router\Interface\ValidatorInterface;
use Psr\Http\Message\RequestInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)] #[\Attribute(\Attribute::TARGET_PROPERTY)]
@@ -13,14 +11,6 @@ class NotIn implements ValidatorInterface
{ {
/**
* @var RequestInterface
*/
#[Container(RequestInterface::class)]
public RequestInterface $request;
/** /**
* @param array $value * @param array $value
*/ */
@@ -30,18 +20,12 @@ class NotIn implements ValidatorInterface
/** /**
* @param mixed $data
* @param object $class * @param object $class
* @param string $name
* @return bool * @return bool
*/ */
public function dispatch(object $class, string $name): bool public function dispatch(mixed $data, object $class): bool
{ {
// TODO: Implement dispatch() method.
if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
return !in_array($data, $this->value); return !in_array($data, $this->value);
} }
} }
+2 -16
View File
@@ -3,9 +3,7 @@ declare(strict_types=1);
namespace Kiri\Router\Validator\Inject; namespace Kiri\Router\Validator\Inject;
use Kiri\Di\Inject\Container;
use Kiri\Router\Interface\ValidatorInterface; use Kiri\Router\Interface\ValidatorInterface;
use Psr\Http\Message\RequestInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)] #[\Attribute(\Attribute::TARGET_PROPERTY)]
class NotNull implements ValidatorInterface class NotNull implements ValidatorInterface
@@ -13,24 +11,12 @@ class NotNull implements ValidatorInterface
/** /**
* @var RequestInterface * @param mixed $data
*/
#[Container(RequestInterface::class)]
public RequestInterface $request;
/**
* @param object $class * @param object $class
* @param string $name
* @return bool * @return bool
*/ */
public function dispatch(object $class, string $name): bool public function dispatch(mixed $data, object $class): bool
{ {
// TODO: Implement dispatch() method.
if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
return !($data === null); return !($data === null);
} }
} }
+2 -15
View File
@@ -3,9 +3,7 @@ declare(strict_types=1);
namespace Kiri\Router\Validator\Inject; namespace Kiri\Router\Validator\Inject;
use Kiri\Di\Inject\Container;
use Kiri\Router\Interface\ValidatorInterface; use Kiri\Router\Interface\ValidatorInterface;
use Psr\Http\Message\RequestInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)] #[\Attribute(\Attribute::TARGET_PROPERTY)]
class Phone implements ValidatorInterface class Phone implements ValidatorInterface
@@ -14,23 +12,12 @@ class Phone implements ValidatorInterface
/** /**
* @var RequestInterface * @param mixed $data
*/
#[Container(RequestInterface::class)]
public RequestInterface $request;
/**
* @param object $class * @param object $class
* @param string $name
* @return bool * @return bool
*/ */
public function dispatch(object $class, string $name): bool public function dispatch(mixed $data, object $class): bool
{ {
if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
return preg_match(self::REG, $data); return preg_match(self::REG, $data);
} }
} }
+2 -15
View File
@@ -3,9 +3,7 @@ declare(strict_types=1);
namespace Kiri\Router\Validator\Inject; namespace Kiri\Router\Validator\Inject;
use Kiri\Di\Inject\Container;
use Kiri\Router\Interface\ValidatorInterface; use Kiri\Router\Interface\ValidatorInterface;
use Psr\Http\Message\RequestInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)] #[\Attribute(\Attribute::TARGET_PROPERTY)]
class Required implements ValidatorInterface class Required implements ValidatorInterface
@@ -13,23 +11,12 @@ class Required implements ValidatorInterface
/** /**
* @var RequestInterface * @param mixed $data
*/
#[Container(RequestInterface::class)]
public RequestInterface $request;
/**
* @param object $class * @param object $class
* @param string $name
* @return bool * @return bool
*/ */
public function dispatch(object $class, string $name): bool public function dispatch(mixed $data, object $class): bool
{ {
if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
return !($data === null); return !($data === null);
} }
+3 -4
View File
@@ -19,13 +19,12 @@ class Round implements ValidatorInterface
/** /**
* @param mixed $data
* @param object $class * @param object $class
* @param string $name
* @return bool * @return bool
*/ */
public function dispatch(object $class, string $name): bool public function dispatch(mixed $data, object $class): bool
{ {
// TODO: Implement dispatch() method. return round($data, $this->value) === $data;
return round($class->$name, $this->value) === $class->$name;
} }
} }
+17 -4
View File
@@ -5,6 +5,7 @@ namespace Kiri\Router\Validator;
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\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
@@ -70,7 +71,10 @@ class Validator
$data = $request->getQueryParams(); $data = $request->getQueryParams();
} }
foreach ($data as $key => $value) { foreach ($data as $key => $value) {
if (property_exists($this->formData, $key)) { if (!property_exists($this->formData, $key)) {
$this->addError($key);
return $this;
}
$type = new \ReflectionProperty($this->formData, $key); $type = new \ReflectionProperty($this->formData, $key);
if (!($type->getType() instanceof \ReflectionUnionType)) { if (!($type->getType() instanceof \ReflectionUnionType)) {
$value = match ($type->getType()?->getName()) { $value = match ($type->getType()?->getName()) {
@@ -84,22 +88,31 @@ class Validator
} }
$this->formData->{$key} = $value; $this->formData->{$key} = $value;
} }
}
return $this; return $this;
} }
/** /**
* @param RequestInterface $request
* @return bool * @return bool
*/ */
public function run(): bool public function run(RequestInterface $request): bool
{ {
if (!empty($this->message)) {
return false;
}
foreach ($this->rules as $name => $rule) { foreach ($this->rules as $name => $rule) {
$value = $request->query($name, null);
if ($request->getIsPost()) {
$value = $request->post($name, null);
}
foreach ($rule as $item) { foreach ($rule as $item) {
if (!$item->dispatch($this->formData, $name)) { /** @var ValidatorInterface $item */
if (!$item->dispatch($value, $this->formData)) {
return $this->addError($name); return $this->addError($name);
} }
} }
$this->formData->{$name} = $value;
} }
return true; return true;
} }
+2 -3
View File
@@ -35,9 +35,8 @@ class ValidatorMiddleware implements MiddlewareInterface
*/ */
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{ {
$validator = $this->validator->bindData($request); if (!$this->validator->run($request)) {
if (!$validator->run()) { return $this->response->html($this->validator->error(), 415);
return $this->response->html($validator->error(), 415);
} else { } else {
return $handler->handle($request); return $handler->handle($request);
} }