Files
kiri-core/Validator/EnumValidator.php
T

51 lines
819 B
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;
2020-12-17 14:12:44 +08:00
2020-12-17 14:09:14 +08:00
2020-08-31 12:38:32 +08:00
/**
* Class EnumValidator
* @package validator
*/
class EnumValidator extends BaseValidator
{
2020-10-29 18:17:25 +08:00
public array $value = [];
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
{
$param = $this->getParams();
if (empty($param) || !isset($param[$this->field])) {
return $this->addError('The param :attribute is null');
}
$value = $param[$this->field];
if (is_null($value)) {
return $this->addError('The param :attribute is null');
}
if (!is_array($this->value)) {
return true;
}
if (!in_array($value, $this->value)) {
return $this->addError($this->i());
}
return true;
}
/**
* @return string
*/
2020-12-17 14:12:44 +08:00
private function i(): string
2020-08-31 12:38:32 +08:00
{
return 'The param :attribute value only in ' . implode(',', $this->value);
}
}