This commit is contained in:
2023-08-18 21:08:58 +08:00
parent 9f83b0ce63
commit ee1d61a7d0
+79 -78
View File
@@ -11,106 +11,107 @@ class Validator
{ {
/** /**
* @var ValidatorInterface[] * @var ValidatorInterface[]
*/ */
private array $rules = []; private array $rules = [];
private string $message; private string $message;
private object $formData; private object $formData;
/** /**
* @param object $formData * @param object $formData
*/ */
public function setFormData(object $formData): void public function setFormData(object $formData): void
{ {
$this->formData = $formData; $this->formData = $formData;
} }
/** /**
* @return object * @return object
*/ */
public function getFormData(): object public function getFormData(): object
{ {
return $this->formData; return $this->formData;
} }
/** /**
* @param string $name * @param string $name
* @param ValidatorInterface $rule * @param ValidatorInterface $rule
* @return void * @return void
*/ */
public function addRule(string $name, ValidatorInterface $rule): void public function addRule(string $name, ValidatorInterface $rule): void
{ {
$this->rules[$name] = $rule; $this->rules[$name] = $rule;
} }
/** /**
* @param ServerRequestInterface|Request $request * @param ServerRequestInterface|Request $request
* @return Validator * @return Validator
*/ */
public function bindData(ServerRequestInterface|Request $request): static public function bindData(ServerRequestInterface|Request $request): static
{ {
if ($request->isPost) { if ($request->isPost) {
$data = $request->getParsedBody(); $data = $request->getParsedBody();
} else { } else {
$data = $request->getQueryParams(); $data = $request->getQueryParams();
} }
foreach ($data as $key => $value) { foreach ($data as $key => $value) {
if (property_exists($this->formData, $key)) { if (property_exists($this->formData, $key)) {
$type = new \ReflectionProperty($this->formData, $key); $type = new \ReflectionProperty($this->formData, $key);
var_dump($type->getType()); if (!($type->getType() instanceof \ReflectionUnionType)) {
$value = match ($type->getType()?->getName()) { $value = match ($type->getType()?->getName()) {
'int' => (int)$value, 'int' => (int)$value,
'float' => (float)$value, 'float' => (float)$value,
default => $value default => $value
}; };
$this->formData->{$key} = $value; }
} $this->formData->{$key} = $value;
} }
return $this; }
} return $this;
}
/** /**
* @param ServerRequestInterface|Request $request * @param ServerRequestInterface|Request $request
* @return bool * @return bool
*/ */
public function run(ServerRequestInterface|Request $request): bool public function run(ServerRequestInterface|Request $request): bool
{ {
foreach ($this->rules as $name => $rule) { foreach ($this->rules as $name => $rule) {
if (!$rule->dispatch($this->formData, $name)) { if (!$rule->dispatch($this->formData, $name)) {
return false; return false;
} }
} }
return true; return true;
} }
/** /**
* @param $field * @param $field
* @return bool * @return bool
*/ */
private function addError($field): bool private function addError($field): bool
{ {
$this->message = $field . ' error'; $this->message = $field . ' error';
return false; return false;
} }
/** /**
* @return string * @return string
*/ */
public function error(): string public function error(): string
{ {
return $this->message; return $this->message;
} }
} }