Files
kiri-router/src/Validator/Inject/In.php
T

48 lines
938 B
PHP
Raw Normal View History

2023-04-15 23:29:27 +08:00
<?php
2023-04-16 01:24:30 +08:00
declare(strict_types=1);
2023-04-15 23:29:27 +08:00
2023-04-15 23:31:16 +08:00
namespace Kiri\Router\Validator\Inject;
2023-11-09 20:25:33 +08:00
use Kiri\Di\Inject\Container;
2023-04-15 23:31:16 +08:00
use Kiri\Router\Interface\ValidatorInterface;
2023-11-09 20:25:33 +08:00
use Psr\Http\Message\RequestInterface;
2023-04-15 23:29:27 +08:00
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class In implements ValidatorInterface
{
2023-11-09 20:25:33 +08:00
/**
* @var RequestInterface
*/
#[Container(RequestInterface::class)]
public RequestInterface $request;
/**
2023-04-15 23:29:27 +08:00
* @param array $value
*/
public function __construct(readonly public array $value)
{
}
/**
2023-04-15 23:31:16 +08:00
* @param object $class
2023-04-15 23:29:27 +08:00
* @param string $name
* @return bool
*/
2023-04-15 23:31:16 +08:00
public function dispatch(object $class, string $name): bool
2023-04-15 23:29:27 +08:00
{
2023-11-09 20:25:33 +08:00
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);
2023-04-15 23:29:27 +08:00
}
}