Files
kiri-core/Validator/BaseValidator.php
T

135 lines
2.0 KiB
PHP
Raw Normal View History

2020-08-31 12:38:32 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 12:38:32 +08:00
namespace validator;
use Database\ActiveRecord;
2020-12-17 14:09:14 +08:00
use Exception;
2020-08-31 12:38:32 +08:00
abstract class BaseValidator
{
2020-10-30 10:38:31 +08:00
public string $field = '';
2020-08-31 12:38:32 +08:00
2020-10-30 10:38:31 +08:00
public array $rules = [];
2020-08-31 12:38:32 +08:00
2020-10-29 18:17:25 +08:00
public string $method;
2020-08-31 12:38:32 +08:00
2020-10-30 10:38:31 +08:00
protected bool $isFail = TRUE;
2020-08-31 12:38:32 +08:00
2020-10-30 10:38:31 +08:00
protected string $message = '';
2020-08-31 12:38:32 +08:00
2020-10-30 10:38:31 +08:00
protected array $params = [];
2020-08-31 12:38:32 +08:00
2021-03-19 18:16:50 +08:00
protected ?ActiveRecord $model = null;
2020-08-31 12:38:32 +08:00
2020-12-17 14:09:14 +08:00
/**
* @param $model
*/
2020-08-31 12:38:32 +08:00
public function setModel($model)
{
$this->model = $model;
}
/**
2021-03-19 18:16:50 +08:00
* @return ActiveRecord|null
2020-08-31 12:38:32 +08:00
*/
2021-03-19 18:16:50 +08:00
public function getModel(): ?ActiveRecord
2020-08-31 12:38:32 +08:00
{
return $this->model;
}
2020-12-17 14:09:14 +08:00
/**
* BaseValidator constructor.
* @param array $config
*/
2021-07-07 18:36:20 +08:00
public function __construct(array $config = [])
2020-08-31 12:38:32 +08:00
{
$this->regConfig($config);
}
2020-12-17 14:09:14 +08:00
/**
* @param $config
*/
2020-08-31 12:38:32 +08:00
private function regConfig($config)
{
if (empty($config) || !is_array($config)) {
return;
}
foreach ($config as $key => $val) {
$this->$key = $val;
}
}
/**
2020-12-17 14:09:14 +08:00
* @throws Exception
2020-08-31 12:38:32 +08:00
* @return bool
*/
2020-12-17 14:09:14 +08:00
public function trigger(): bool
2020-08-31 12:38:32 +08:00
{
2020-12-17 14:09:14 +08:00
throw new Exception('Child Class must define method of trigger');
2020-08-31 12:38:32 +08:00
}
/**
2020-12-17 14:09:14 +08:00
* @return array
2020-08-31 12:38:32 +08:00
*/
2020-12-17 14:09:14 +08:00
protected function getParams(): array
2020-08-31 12:38:32 +08:00
{
return $this->params;
}
/**
2020-12-17 14:09:14 +08:00
* @param array $data
2020-08-31 12:38:32 +08:00
* @return $this
*/
2020-12-17 14:09:14 +08:00
public function setParams(array $data): static
2020-08-31 12:38:32 +08:00
{
$this->params = $data;
return $this;
}
/**
* @param $message
* @return bool
*/
2020-12-17 14:09:14 +08:00
public function addError($message): bool
2020-08-31 12:38:32 +08:00
{
$this->isFail = FALSE;
$message = str_replace(':attribute', $this->field, $message);
$this->message = $message;
return $this->isFail;
}
/**
* @return string
*/
2020-12-17 14:09:14 +08:00
public function getError(): string
2020-08-31 12:38:32 +08:00
{
return $this->message;
}
/**
* @param $name
* @param $value
2020-12-17 14:09:14 +08:00
* @throws Exception
2020-08-31 12:38:32 +08:00
*/
public function __set($name, $value)
{
$method = 'set' . ucfirst($name);
if (method_exists($this, $method)) {
$this->$method($value);
} else if (property_exists($this, $name)) {
$this->$name = $value;
} else {
2021-04-25 11:27:13 +08:00
throw new Exception('unknown property ' . $name . ' in class ' . static::class);
2020-08-31 12:38:32 +08:00
}
}
}