This commit is contained in:
xl
2023-11-09 20:25:33 +08:00
parent 39329b9f02
commit dbe4288de3
13 changed files with 266 additions and 66 deletions
+33 -16
View File
@@ -3,29 +3,46 @@ 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
{ {
/** /**
* @return string * @var RequestInterface
*/ */
public function getError(): string #[Container(RequestInterface::class)]
{ public RequestInterface $request;
return '';
}
/** /**
* @param object $class * @return string
* @param string $name */
* @return bool public function getError(): string
*/ {
public function dispatch(object $class, string $name): bool return '';
{ }
return filter_var($class->{$name}, FILTER_VALIDATE_EMAIL);
}
/**
* @param object $class
* @param string $name
* @return bool
*/
public function dispatch(object $class, string $name): bool
{
if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
if ($data === null) {
return false;
}
return filter_var($data, FILTER_VALIDATE_EMAIL);
}
} }
+19 -2
View File
@@ -3,14 +3,23 @@ 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
*/ */
public function __construct(readonly public array $value) public function __construct(readonly public array $value)
@@ -25,6 +34,14 @@ class In implements ValidatorInterface
*/ */
public function dispatch(object $class, string $name): bool public function dispatch(object $class, string $name): bool
{ {
return in_array($class->{$name}, $this->value); if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
if ($data === null) {
return false;
}
return in_array($data, $this->value);
} }
} }
+18 -2
View File
@@ -3,14 +3,22 @@ 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
*/ */
public function __construct(readonly public int $value) public function __construct(readonly public int $value)
@@ -26,6 +34,14 @@ class Length implements ValidatorInterface
public function dispatch(object $class, string $name): bool public function dispatch(object $class, string $name): bool
{ {
// TODO: Implement dispatch() method. // TODO: Implement dispatch() method.
return mb_strlen((string)$class->{$name}) === $this->value; if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
if ($data === null) {
return false;
}
return mb_strlen((string)$data) === $this->value;
} }
} }
+32 -16
View File
@@ -3,29 +3,45 @@ 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
*/ */
public function __construct(readonly public int $value) public function __construct(readonly public int $value)
{ {
} }
/** /**
* @param object $class * @param object $class
* @param string $name * @param string $name
* @return bool * @return bool
*/ */
public function dispatch(object $class, string $name): bool public function dispatch(object $class, string $name): bool
{ {
// TODO: Implement dispatch() method. // TODO: Implement dispatch() method.
return $class->{$name} <= $this->value; if ($this->request->getIsPost()) {
} $data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
if ($data === null) {
return false;
}
return $data <= $this->value;
}
} }
+20 -2
View File
@@ -3,14 +3,24 @@ 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
*/ */
public function __construct(readonly public int $value) public function __construct(readonly public int $value)
@@ -26,6 +36,14 @@ class MaxLength implements ValidatorInterface
public function dispatch(object $class, string $name): bool public function dispatch(object $class, string $name): bool
{ {
// TODO: Implement dispatch() method. // TODO: Implement dispatch() method.
return mb_strlen((string)$class->{$name}) <= $this->value; if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
if ($data === null) {
return false;
}
return mb_strlen((string)$data) <= $this->value;
} }
} }
+19 -2
View File
@@ -3,14 +3,23 @@ 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
*/ */
public function __construct(readonly public int $value) public function __construct(readonly public int $value)
@@ -26,6 +35,14 @@ class Min implements ValidatorInterface
public function dispatch(object $class, string $name): bool public function dispatch(object $class, string $name): bool
{ {
// TODO: Implement dispatch() method. // TODO: Implement dispatch() method.
return $class->{$name} >= $this->value; if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
if ($data === null) {
return false;
}
return $data >= $this->value;
} }
} }
+19 -2
View File
@@ -3,14 +3,23 @@ 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
*/ */
public function __construct(readonly public int $value) public function __construct(readonly public int $value)
@@ -26,6 +35,14 @@ class MinLength implements ValidatorInterface
public function dispatch(object $class, string $name): bool public function dispatch(object $class, string $name): bool
{ {
// TODO: Implement dispatch() method. // TODO: Implement dispatch() method.
return mb_strlen((string)$class->{$name}) <= $this->value; if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
if ($data === null) {
return false;
}
return mb_strlen((string)$data) <= $this->value;
} }
} }
+14 -1
View File
@@ -4,12 +4,20 @@ 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
@@ -27,7 +35,12 @@ class Must implements ValidatorInterface
public function dispatch(object $class, string $name): bool public function dispatch(object $class, string $name): bool
{ {
// TODO: Implement dispatch() method. // TODO: Implement dispatch() method.
return $class->{$name} === $this->value; if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
return $data === $this->value;
} }
} }
+16 -1
View File
@@ -3,12 +3,22 @@ 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
{ {
/**
* @var RequestInterface
*/
#[Container(RequestInterface::class)]
public RequestInterface $request;
/** /**
* @param object $class * @param object $class
* @param string $name * @param string $name
@@ -17,6 +27,11 @@ class NotEmpty implements ValidatorInterface
public function dispatch(object $class, string $name): bool public function dispatch(object $class, string $name): bool
{ {
// TODO: Implement dispatch() method. // TODO: Implement dispatch() method.
return !empty($class->{$name}); if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
return !empty($data);
} }
} }
+16 -1
View File
@@ -3,7 +3,9 @@ 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)]
@@ -11,6 +13,14 @@ class NotIn implements ValidatorInterface
{ {
/**
* @var RequestInterface
*/
#[Container(RequestInterface::class)]
public RequestInterface $request;
/** /**
* @param array $value * @param array $value
*/ */
@@ -27,6 +37,11 @@ class NotIn implements ValidatorInterface
public function dispatch(object $class, string $name): bool public function dispatch(object $class, string $name): bool
{ {
// TODO: Implement dispatch() method. // TODO: Implement dispatch() method.
return !in_array($class->{$name}, $this->value); if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
return !in_array($data, $this->value);
} }
} }
+14 -1
View File
@@ -3,13 +3,21 @@ 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
{ {
/**
* @var RequestInterface
*/
#[Container(RequestInterface::class)]
public RequestInterface $request;
/** /**
* @param object $class * @param object $class
* @param string $name * @param string $name
@@ -18,6 +26,11 @@ class NotNull implements ValidatorInterface
public function dispatch(object $class, string $name): bool public function dispatch(object $class, string $name): bool
{ {
// TODO: Implement dispatch() method. // TODO: Implement dispatch() method.
return $class->{$name} !== null; if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
return !($data === null);
} }
} }
+24 -10
View File
@@ -3,20 +3,34 @@ 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
{ {
const REG = '/^1[356789]\d{9}$/'; const REG = '/^1[356789]\d{9}$/';
/**
* @param object $class /**
* @param string $name * @var RequestInterface
* @return bool */
*/ #[Container(RequestInterface::class)]
public function dispatch(object $class, string $name): bool public RequestInterface $request;
{
return preg_match(self::REG, $class->{$name}); /**
} * @param object $class
* @param string $name
* @return bool
*/
public function dispatch(object $class, string $name): bool
{
if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
return preg_match(self::REG, $data);
}
} }
+22 -10
View File
@@ -3,22 +3,34 @@ 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
{ {
/** /**
* @param object $class * @var RequestInterface
* @param string $name */
* @return bool #[Container(RequestInterface::class)]
*/ public RequestInterface $request;
public function dispatch(object $class, string $name): bool
{ /**
// TODO: Implement dispatch() method. * @param object $class
return $class->$name === null; * @param string $name
} * @return bool
*/
public function dispatch(object $class, string $name): bool
{
if ($this->request->getIsPost()) {
$data = $this->request->post($name, null);
} else {
$data = $this->request->query($name, null);
}
return !($data === null);
}
} }