diff --git a/src/Validator/Inject/Email.php b/src/Validator/Inject/Email.php index e23f640..06923c9 100644 --- a/src/Validator/Inject/Email.php +++ b/src/Validator/Inject/Email.php @@ -3,29 +3,46 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; +use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; +use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class Email implements ValidatorInterface { - /** - * @return string - */ - public function getError(): string - { - return ''; - } + /** + * @var RequestInterface + */ + #[Container(RequestInterface::class)] + public RequestInterface $request; - /** - * @param object $class - * @param string $name - * @return bool - */ - public function dispatch(object $class, string $name): bool - { - return filter_var($class->{$name}, FILTER_VALIDATE_EMAIL); - } + /** + * @return string + */ + public function getError(): string + { + return ''; + } + + + /** + * @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); + } } diff --git a/src/Validator/Inject/In.php b/src/Validator/Inject/In.php index a2dc2d0..39d21df 100644 --- a/src/Validator/Inject/In.php +++ b/src/Validator/Inject/In.php @@ -3,14 +3,23 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; +use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; +use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class In implements ValidatorInterface { - /** + /** + * @var RequestInterface + */ + #[Container(RequestInterface::class)] + public RequestInterface $request; + + + /** * @param 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 { - 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); } } diff --git a/src/Validator/Inject/Length.php b/src/Validator/Inject/Length.php index 7cd2e41..55db98d 100644 --- a/src/Validator/Inject/Length.php +++ b/src/Validator/Inject/Length.php @@ -3,14 +3,22 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; +use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; +use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class Length implements ValidatorInterface { + /** + * @var RequestInterface + */ + #[Container(RequestInterface::class)] + public RequestInterface $request; - /** + + /** * @param 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 { // 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; } } diff --git a/src/Validator/Inject/Max.php b/src/Validator/Inject/Max.php index 533be88..9f1f740 100644 --- a/src/Validator/Inject/Max.php +++ b/src/Validator/Inject/Max.php @@ -3,29 +3,45 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; +use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; +use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class Max implements ValidatorInterface { + /** + * @var RequestInterface + */ + #[Container(RequestInterface::class)] + public RequestInterface $request; + - /** - * @param int $value - */ - public function __construct(readonly public int $value) - { - } + /** + * @param int $value + */ + public function __construct(readonly public int $value) + { + } - /** - * @param object $class - * @param string $name - * @return bool - */ - public function dispatch(object $class, string $name): bool - { - // TODO: Implement dispatch() method. - return $class->{$name} <= $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); + } + if ($data === null) { + return false; + } + return $data <= $this->value; + } } diff --git a/src/Validator/Inject/MaxLength.php b/src/Validator/Inject/MaxLength.php index 8af4b86..2e76f04 100644 --- a/src/Validator/Inject/MaxLength.php +++ b/src/Validator/Inject/MaxLength.php @@ -3,14 +3,24 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; +use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; +use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class MaxLength implements ValidatorInterface { - /** + /** + * @var RequestInterface + */ + #[Container(RequestInterface::class)] + public RequestInterface $request; + + + + /** * @param 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 { // 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; } } diff --git a/src/Validator/Inject/Min.php b/src/Validator/Inject/Min.php index f6b7b98..67b6fa1 100644 --- a/src/Validator/Inject/Min.php +++ b/src/Validator/Inject/Min.php @@ -3,14 +3,23 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; +use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; +use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class Min implements ValidatorInterface { - /** + /** + * @var RequestInterface + */ + #[Container(RequestInterface::class)] + public RequestInterface $request; + + + /** * @param 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 { // 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; } } diff --git a/src/Validator/Inject/MinLength.php b/src/Validator/Inject/MinLength.php index 46973cb..1164846 100644 --- a/src/Validator/Inject/MinLength.php +++ b/src/Validator/Inject/MinLength.php @@ -3,14 +3,23 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; +use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; +use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class MinLength implements ValidatorInterface { - /** + /** + * @var RequestInterface + */ + #[Container(RequestInterface::class)] + public RequestInterface $request; + + + /** * @param 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 { // 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; } } diff --git a/src/Validator/Inject/Must.php b/src/Validator/Inject/Must.php index 7045e5e..e087581 100644 --- a/src/Validator/Inject/Must.php +++ b/src/Validator/Inject/Must.php @@ -4,12 +4,20 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; +use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; +use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class Must implements ValidatorInterface { + /** + * @var RequestInterface + */ + #[Container(RequestInterface::class)] + public RequestInterface $request; + /** * @param mixed $value @@ -27,7 +35,12 @@ class Must implements ValidatorInterface public function dispatch(object $class, string $name): bool { // 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; } } diff --git a/src/Validator/Inject/NotEmpty.php b/src/Validator/Inject/NotEmpty.php index 37cc838..9902aa9 100644 --- a/src/Validator/Inject/NotEmpty.php +++ b/src/Validator/Inject/NotEmpty.php @@ -3,12 +3,22 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; +use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; +use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class NotEmpty implements ValidatorInterface { + + /** + * @var RequestInterface + */ + #[Container(RequestInterface::class)] + public RequestInterface $request; + + /** * @param object $class * @param string $name @@ -17,6 +27,11 @@ class NotEmpty implements ValidatorInterface public function dispatch(object $class, string $name): bool { // 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); } } diff --git a/src/Validator/Inject/NotIn.php b/src/Validator/Inject/NotIn.php index 2216e85..eca8950 100644 --- a/src/Validator/Inject/NotIn.php +++ b/src/Validator/Inject/NotIn.php @@ -3,7 +3,9 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; +use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; +use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] @@ -11,6 +13,14 @@ class NotIn implements ValidatorInterface { + + /** + * @var RequestInterface + */ + #[Container(RequestInterface::class)] + public RequestInterface $request; + + /** * @param array $value */ @@ -27,6 +37,11 @@ class NotIn implements ValidatorInterface public function dispatch(object $class, string $name): bool { // 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); } } diff --git a/src/Validator/Inject/NotNull.php b/src/Validator/Inject/NotNull.php index e9d3de5..cdf5aac 100644 --- a/src/Validator/Inject/NotNull.php +++ b/src/Validator/Inject/NotNull.php @@ -3,13 +3,21 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; +use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; +use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class NotNull implements ValidatorInterface { + /** + * @var RequestInterface + */ + #[Container(RequestInterface::class)] + public RequestInterface $request; + /** * @param object $class * @param string $name @@ -18,6 +26,11 @@ class NotNull implements ValidatorInterface public function dispatch(object $class, string $name): bool { // 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); } } diff --git a/src/Validator/Inject/Phone.php b/src/Validator/Inject/Phone.php index cf8eb85..f2bac58 100644 --- a/src/Validator/Inject/Phone.php +++ b/src/Validator/Inject/Phone.php @@ -3,20 +3,34 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; +use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; +use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class Phone implements ValidatorInterface { - const REG = '/^1[356789]\d{9}$/'; + const REG = '/^1[356789]\d{9}$/'; - /** - * @param object $class - * @param string $name - * @return bool - */ - public function dispatch(object $class, string $name): bool - { - return preg_match(self::REG, $class->{$name}); - } + + /** + * @var RequestInterface + */ + #[Container(RequestInterface::class)] + public RequestInterface $request; + + /** + * @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); + } } diff --git a/src/Validator/Inject/Required.php b/src/Validator/Inject/Required.php index 97893cf..18de417 100644 --- a/src/Validator/Inject/Required.php +++ b/src/Validator/Inject/Required.php @@ -3,22 +3,34 @@ declare(strict_types=1); namespace Kiri\Router\Validator\Inject; +use Kiri\Di\Inject\Container; use Kiri\Router\Interface\ValidatorInterface; +use Psr\Http\Message\RequestInterface; #[\Attribute(\Attribute::TARGET_PROPERTY)] class Required implements ValidatorInterface { - /** - * @param object $class - * @param string $name - * @return bool - */ - public function dispatch(object $class, string $name): bool - { - // TODO: Implement dispatch() method. - return $class->$name === null; - } + /** + * @var RequestInterface + */ + #[Container(RequestInterface::class)] + public RequestInterface $request; + + /** + * @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 !($data === null); + } }