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;
} }
+6 -6
View File
@@ -10,12 +10,12 @@ class Ignore implements ValidatorInterface
{ {
/** /**
* @param object $class * @param mixed $data
* @param string $name * @param object $class
* @return bool * @return bool
*/ */
public function dispatch(object $class, string $name): bool public function dispatch(mixed $data, object $class): bool
{ {
return true; return true;
} }
+7 -21
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
*/ */
@@ -27,18 +18,13 @@ class In implements ValidatorInterface
} }
/** /**
* @param object $class * @param mixed $data
* @param string $name * @param object $class
* @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;
} }
+7 -21
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
@@ -26,19 +18,13 @@ class Length implements ValidatorInterface
} }
/** /**
* @param object $class * @param mixed $data
* @param string $name * @param object $class
* @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;
} }
+7 -22
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
@@ -28,19 +19,13 @@ class MaxLength implements ValidatorInterface
} }
/** /**
* @param object $class * @param mixed $data
* @param string $name * @param object $class
* @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;
} }
+7 -22
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
*/ */
@@ -27,19 +18,13 @@ class Min implements ValidatorInterface
} }
/** /**
* @param object $class * @param mixed $data
* @param string $name * @param object $class
* @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;
} }
+7 -22
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
*/ */
@@ -27,19 +18,13 @@ class MinLength implements ValidatorInterface
} }
/** /**
* @param object $class * @param mixed $data
* @param string $name * @param object $class
* @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;
} }
+13 -28
View File
@@ -4,43 +4,28 @@ 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 * @param mixed $value
*/ */
#[Container(RequestInterface::class)] public function __construct(readonly public mixed $value)
public RequestInterface $request; {
}
/** /**
* @param mixed $value * @param mixed $data
*/ * @param object $class
public function __construct(readonly public mixed $value) * @return bool
{ */
} public function dispatch(mixed $data, object $class): bool
{
return $data === $this->value;
/** }
* @param object $class
* @param string $name
* @return bool
*/
public function dispatch(object $class, string $name): 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;
}
} }
+5 -20
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
* @param object $class
* @return bool
*/ */
#[Container(RequestInterface::class)] public function dispatch(mixed $data, object $class): bool
public RequestInterface $request; {
/**
* @param object $class
* @param string $name
* @return bool
*/
public function dispatch(object $class, string $name): 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);
} }
} }
+7 -23
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
*/ */
@@ -29,19 +19,13 @@ class NotIn implements ValidatorInterface
} }
/** /**
* @param object $class * @param mixed $data
* @param string $name * @param object $class
* @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);
} }
} }
+7 -21
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
* @param object $class
* @return bool
*/ */
#[Container(RequestInterface::class)] public function dispatch(mixed $data, object $class): bool
public RequestInterface $request; {
return !($data === null);
/** }
* @param object $class
* @param string $name
* @return bool
*/
public function dispatch(object $class, string $name): 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);
}
} }
+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);
} }
+8 -9
View File
@@ -18,14 +18,13 @@ class Round implements ValidatorInterface
} }
/** /**
* @param object $class * @param mixed $data
* @param string $name * @param object $class
* @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;
} }
} }
+28 -15
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,36 +71,48 @@ 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)) {
$type = new \ReflectionProperty($this->formData, $key); $this->addError($key);
if (!($type->getType() instanceof \ReflectionUnionType)) { return $this;
$value = match ($type->getType()?->getName()) {
'int' => (int)$value,
'float' => (float)$value,
default => $value
};
}
if ($value === 'Null') {
$value = null;
}
$this->formData->{$key} = $value;
} }
$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; 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);
} }