This commit is contained in:
2023-04-15 23:31:16 +08:00
parent 2d9ac93a7a
commit 41a53200b3
69 changed files with 1678 additions and 749 deletions
+5 -15
View File
@@ -1,11 +1,8 @@
<?php
namespace Kiri\Inject\Validator\Inject;
namespace Kiri\Router\Validator\Inject;
use Exception;
use Kiri\Inject\Route\LocalService;
use ReflectionException;
use Kiri\Inject\Route\Container;
use Kiri\Router\Interface\ValidatorInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Email implements ValidatorInterface
@@ -22,19 +19,12 @@ class Email implements ValidatorInterface
/**
* @param object $class
* @param string $name
* @return bool
* @throws ReflectionException
* @throws Exception
*/
public function dispatch(string $name): bool
public function dispatch(object $class, string $name): bool
{
// TODO: Implement dispatch() method.
$service = Container::getContext()->get(LocalService::class)->get('request');
if ($service->isPost) {
return filter_var($service->post($name, null), FILTER_VALIDATE_EMAIL);
} else {
return filter_var($service->query($name, null), FILTER_VALIDATE_EMAIL);
}
return filter_var($class->{$name}, FILTER_VALIDATE_EMAIL);
}
}
+5 -3
View File
@@ -1,6 +1,8 @@
<?php
namespace Kiri\Inject\Validator\Inject;
namespace Kiri\Router\Validator\Inject;
use Kiri\Router\Interface\ValidatorInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Ignore implements ValidatorInterface
@@ -8,12 +10,12 @@ class Ignore implements ValidatorInterface
/**
* @param object $class
* @param string $name
* @return bool
*/
public function dispatch(string $name): bool
public function dispatch(object $class, string $name): bool
{
// TODO: Implement dispatch() method.
return true;
}
}
+6 -4
View File
@@ -1,6 +1,8 @@
<?php
namespace Kiri\Inject\Validator\Inject;
namespace Kiri\Router\Validator\Inject;
use Kiri\Router\Interface\ValidatorInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class In implements ValidatorInterface
@@ -16,12 +18,12 @@ class In implements ValidatorInterface
/**
* @param object $class
* @param string $name
* @return bool
*/
public function dispatch(string $name): bool
public function dispatch(object $class, string $name): bool
{
// TODO: Implement dispatch() method.
return true;
return in_array($class->{$name}, $this->value);
}
}
+6 -3
View File
@@ -1,6 +1,8 @@
<?php
namespace Kiri\Inject\Validator\Inject;
namespace Kiri\Router\Validator\Inject;
use Kiri\Router\Interface\ValidatorInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Length implements ValidatorInterface
@@ -16,12 +18,13 @@ class Length implements ValidatorInterface
/**
* @param object $class
* @param string $name
* @return bool
*/
public function dispatch(string $name): bool
public function dispatch(object $class, string $name): bool
{
// TODO: Implement dispatch() method.
return true;
return mb_strlen($class->{$name}) === $this->value;
}
}
+5 -3
View File
@@ -1,7 +1,8 @@
<?php
namespace Kiri\Inject\Validator\Inject;
namespace Kiri\Router\Validator\Inject;
use Kiri\Router\Interface\ValidatorInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Max implements ValidatorInterface
@@ -17,12 +18,13 @@ class Max implements ValidatorInterface
/**
* @param object $class
* @param string $name
* @return bool
*/
public function dispatch(string $name): bool
public function dispatch(object $class, string $name): bool
{
// TODO: Implement dispatch() method.
return true;
return $class->{$name} <= $this->value;
}
}
+6 -3
View File
@@ -1,6 +1,8 @@
<?php
namespace Kiri\Inject\Validator\Inject;
namespace Kiri\Router\Validator\Inject;
use Kiri\Router\Interface\ValidatorInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class MaxLength implements ValidatorInterface
@@ -16,12 +18,13 @@ class MaxLength implements ValidatorInterface
/**
* @param object $class
* @param string $name
* @return bool
*/
public function dispatch(string $name): bool
public function dispatch(object $class, string $name): bool
{
// TODO: Implement dispatch() method.
return true;
return mb_strlen($class->{$name}) <= $this->value;
}
}
+5 -3
View File
@@ -1,7 +1,8 @@
<?php
namespace Kiri\Inject\Validator\Inject;
namespace Kiri\Router\Validator\Inject;
use Kiri\Router\Interface\ValidatorInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Min implements ValidatorInterface
@@ -17,12 +18,13 @@ class Min implements ValidatorInterface
/**
* @param object $class
* @param string $name
* @return bool
*/
public function dispatch(string $name): bool
public function dispatch(object $class, string $name): bool
{
// TODO: Implement dispatch() method.
return true;
return $class->{$name} >= $this->value;
}
}
+6 -3
View File
@@ -1,6 +1,8 @@
<?php
namespace Kiri\Inject\Validator\Inject;
namespace Kiri\Router\Validator\Inject;
use Kiri\Router\Interface\ValidatorInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class MinLength implements ValidatorInterface
@@ -16,12 +18,13 @@ class MinLength implements ValidatorInterface
/**
* @param object $class
* @param string $name
* @return bool
*/
public function dispatch(string $name): bool
public function dispatch(object $class, string $name): bool
{
// TODO: Implement dispatch() method.
return true;
return mb_strlen($class->{$name}) <= $this->value;
}
}
+6 -3
View File
@@ -1,8 +1,10 @@
<?php
namespace Kiri\Inject\Validator\Inject;
namespace Kiri\Router\Validator\Inject;
use Kiri\Router\Interface\ValidatorInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Must implements ValidatorInterface
{
@@ -17,13 +19,14 @@ class Must implements ValidatorInterface
/**
* @param object $class
* @param string $name
* @return bool
*/
public function dispatch(string $name): bool
public function dispatch(object $class, string $name): bool
{
// TODO: Implement dispatch() method.
return true;
return $class->{$name} === $this->value;
}
}
+6 -3
View File
@@ -1,18 +1,21 @@
<?php
namespace Kiri\Inject\Validator\Inject;
namespace Kiri\Router\Validator\Inject;
use Kiri\Router\Interface\ValidatorInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class NotEmpty implements ValidatorInterface
{
/**
* @param object $class
* @param string $name
* @return bool
*/
public function dispatch(string $name): bool
public function dispatch(object $class, string $name): bool
{
// TODO: Implement dispatch() method.
return true;
return !empty($class->{$name});
}
}
+6 -3
View File
@@ -1,6 +1,8 @@
<?php
namespace Kiri\Inject\Validator\Inject;
namespace Kiri\Router\Validator\Inject;
use Kiri\Router\Interface\ValidatorInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
@@ -17,12 +19,13 @@ class NotIn implements ValidatorInterface
/**
* @param object $class
* @param string $name
* @return bool
*/
public function dispatch(string $name): bool
public function dispatch(object $class, string $name): bool
{
// TODO: Implement dispatch() method.
return true;
return !in_array($class->{$name}, $this->value);
}
}
+6 -3
View File
@@ -1,6 +1,8 @@
<?php
namespace Kiri\Inject\Validator\Inject;
namespace Kiri\Router\Validator\Inject;
use Kiri\Router\Interface\ValidatorInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class NotNull implements ValidatorInterface
@@ -8,12 +10,13 @@ class NotNull implements ValidatorInterface
/**
* @param object $class
* @param string $name
* @return bool
*/
public function dispatch(string $name): bool
public function dispatch(object $class, string $name): bool
{
// TODO: Implement dispatch() method.
return true;
return $class->{$name} !== null;
}
}
+5 -16
View File
@@ -1,11 +1,8 @@
<?php
namespace Kiri\Inject\Validator\Inject;
namespace Kiri\Router\Validator\Inject;
use Exception;
use Kiri\Inject\Route\LocalService;
use ReflectionException;
use Kiri\Inject\Route\Container;
use Kiri\Router\Interface\ValidatorInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Phone implements ValidatorInterface
@@ -13,20 +10,12 @@ class Phone implements ValidatorInterface
const REG = '/^1[356789]\d{9}$/';
/**
* @param object $class
* @param string $name
* @return bool
* @throws ReflectionException
* @throws Exception
*/
public function dispatch(string $name): bool
public function dispatch(object $class, string $name): bool
{
// TODO: Implement dispatch() method.
$service = Container::getContext()->get(LocalService::class)->get('request');
if ($service->isPost) {
$data = $service->post($name, null);
} else {
$data = $service->query($name, null);
}
return preg_match(self::REG, $data);
return preg_match(self::REG, $class->{$name});
}
}
+9 -3
View File
@@ -1,17 +1,23 @@
<?php
namespace Kiri\Inject\Validator\Inject;
namespace Kiri\Router\Validator\Inject;
use Kiri\Router\Interface\ValidatorInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Required implements ValidatorInterface
{
public function dispatch(string $name): bool
/**
* @param object $class
* @param string $name
* @return bool
*/
public function dispatch(object $class, string $name): bool
{
// TODO: Implement dispatch() method.
return true;
return $class->$name === null;
}
}
+5 -3
View File
@@ -1,7 +1,8 @@
<?php
namespace Kiri\Inject\Validator\Inject;
namespace Kiri\Router\Validator\Inject;
use Kiri\Router\Interface\ValidatorInterface;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Round implements ValidatorInterface
@@ -17,12 +18,13 @@ class Round implements ValidatorInterface
/**
* @param object $class
* @param string $name
* @return bool
*/
public function dispatch(string $name): bool
public function dispatch(object $class, string $name): bool
{
// TODO: Implement dispatch() method.
return round($name, $this->value) == $name;
return round($class->$name, $this->value) === $class->$name;
}
}
+31 -4
View File
@@ -1,8 +1,10 @@
<?php
namespace Kiri\Inject\Validator;
namespace Kiri\Router\Validator;
use Kiri\Inject\Validator\Inject\ValidatorInterface;
use Kiri\Router\Interface\ValidatorInterface;
use Kiri\Router\ServerRequest;
use Psr\Http\Message\ServerRequestInterface;
class Validator
{
@@ -17,14 +19,39 @@ class Validator
private string $message;
private object $formData;
/**
* @param string $name
* @param object $data
* @param ValidatorInterface $rule
* @return void
*/
public function addRule(string $name, ValidatorInterface $rule): void
public function addRule(string $name, object $data, ValidatorInterface $rule): void
{
$this->rules[$name] = $rule;
$this->formData = $data;
}
/**
* @param ServerRequestInterface|ServerRequest $request
* @return Validator
*/
public function bindData(ServerRequestInterface|ServerRequest $request): static
{
if ($request->isPost) {
$data = $request->getParsedBody();
} else {
$data = $request->getQueryParams();
}
foreach ($data as $key => $value) {
if (method_exists($this->formData, $key)) {
$this->formData->{$key} = $value;
}
}
return $this;
}
@@ -34,7 +61,7 @@ class Validator
public function run(): bool
{
foreach ($this->rules as $name => $rule) {
if (!$rule->dispatch($name)) {
if (!$rule->dispatch($this->formData, $name)) {
return false;
}
}
+35 -1
View File
@@ -2,7 +2,41 @@
namespace Kiri\Router\Validator;
class ValidatorMiddleware
use Kiri\Di\Context;
use Kiri\Router\Constrict\Stream;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class ValidatorMiddleware implements MiddlewareInterface
{
/**
* @param Validator $validator
*/
public function __construct(readonly public Validator $validator)
{
}
/**
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
$this->validator->bindData($request);
return $handler->handle($request);
} catch (\Throwable $throwable) {
/** @var ResponseInterface $response */
$response = Context::get(ResponseInterface::class);
$response->withStatus(407)->withBody(new Stream($throwable->getMessage()));
return $response;
}
}
}