first commit
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Inject\Validator\Inject;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Inject\Route\LocalService;
|
||||
use ReflectionException;
|
||||
use Kiri\Inject\Route\Container;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class Email implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getError(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function dispatch(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Inject\Validator\Inject;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class Ignore implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(string $name): bool
|
||||
{
|
||||
// TODO: Implement dispatch() method.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Inject\Validator\Inject;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class In implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param array $value
|
||||
*/
|
||||
public function __construct(readonly public array $value)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(string $name): bool
|
||||
{
|
||||
// TODO: Implement dispatch() method.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Inject\Validator\Inject;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class Length implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param int $value
|
||||
*/
|
||||
public function __construct(readonly public int $value)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(string $name): bool
|
||||
{
|
||||
// TODO: Implement dispatch() method.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Inject\Validator\Inject;
|
||||
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class Max implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param int $value
|
||||
*/
|
||||
public function __construct(readonly public int $value)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(string $name): bool
|
||||
{
|
||||
// TODO: Implement dispatch() method.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Inject\Validator\Inject;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class MaxLength implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param int $value
|
||||
*/
|
||||
public function __construct(readonly public int $value)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(string $name): bool
|
||||
{
|
||||
// TODO: Implement dispatch() method.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Inject\Validator\Inject;
|
||||
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class Min implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param int $value
|
||||
*/
|
||||
public function __construct(readonly public int $value)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(string $name): bool
|
||||
{
|
||||
// TODO: Implement dispatch() method.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Inject\Validator\Inject;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class MinLength implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param int $value
|
||||
*/
|
||||
public function __construct(readonly public int $value)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(string $name): bool
|
||||
{
|
||||
// TODO: Implement dispatch() method.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Inject\Validator\Inject;
|
||||
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class Must implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __construct(readonly public mixed $value)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(string $name): bool
|
||||
{
|
||||
// TODO: Implement dispatch() method.
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Inject\Validator\Inject;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class NotEmpty implements ValidatorInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(string $name): bool
|
||||
{
|
||||
// TODO: Implement dispatch() method.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Inject\Validator\Inject;
|
||||
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class NotIn implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param array $value
|
||||
*/
|
||||
public function __construct(readonly public array $value)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(string $name): bool
|
||||
{
|
||||
// TODO: Implement dispatch() method.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Inject\Validator\Inject;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class NotNull implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(string $name): bool
|
||||
{
|
||||
// TODO: Implement dispatch() method.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Inject\Validator\Inject;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Inject\Route\LocalService;
|
||||
use ReflectionException;
|
||||
use Kiri\Inject\Route\Container;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class Phone implements ValidatorInterface
|
||||
{
|
||||
const REG = '/^1[356789]\d{9}$/';
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function dispatch(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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Inject\Validator\Inject;
|
||||
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class Required implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
public function dispatch(string $name): bool
|
||||
{
|
||||
// TODO: Implement dispatch() method.
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Inject\Validator\Inject;
|
||||
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class Round implements ValidatorInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param int $value
|
||||
*/
|
||||
public function __construct(readonly public int $value)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function dispatch(string $name): bool
|
||||
{
|
||||
// TODO: Implement dispatch() method.
|
||||
return round($name, $this->value) == $name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Inject\Validator;
|
||||
|
||||
use Kiri\Inject\Validator\Inject\ValidatorInterface;
|
||||
|
||||
class Validator
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var ValidatorInterface[]
|
||||
*/
|
||||
private array $rules = [];
|
||||
|
||||
|
||||
private string $message;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param ValidatorInterface $rule
|
||||
* @return void
|
||||
*/
|
||||
public function addRule(string $name, ValidatorInterface $rule): void
|
||||
{
|
||||
$this->rules[$name] = $rule;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function run(): bool
|
||||
{
|
||||
foreach ($this->rules as $name => $rule) {
|
||||
if (!$rule->dispatch($name)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $field
|
||||
* @return bool
|
||||
*/
|
||||
private function addError($field): bool
|
||||
{
|
||||
$this->message = $field . ' error';
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function error(): string
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Router\Validator;
|
||||
|
||||
class ValidatorMiddleware
|
||||
{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user