2022-01-09 14:01:11 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace validator;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class EnumValidator
|
|
|
|
|
* @package validator
|
|
|
|
|
*/
|
|
|
|
|
class EnumValidator extends BaseValidator
|
|
|
|
|
{
|
|
|
|
|
|
2023-07-07 17:24:03 +08:00
|
|
|
public array $value = [];
|
2022-01-09 14:01:11 +08:00
|
|
|
|
2023-05-09 10:11:21 +08:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws \ReflectionException
|
|
|
|
|
*/
|
2023-07-07 17:24:03 +08:00
|
|
|
public function trigger(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->_validator($this->field, function ($field, $params, $values) {
|
|
|
|
|
$value = $params[$field] ?? null;
|
|
|
|
|
if (in_array($value, $values)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
$message = 'The param :attribute value(' . $value . ') only in ' . implode(',', $values);
|
|
|
|
|
return $this->addError($field, $message);
|
|
|
|
|
|
|
|
|
|
}, $this->params, $this->value);
|
|
|
|
|
}
|
2022-01-09 14:01:11 +08:00
|
|
|
|
|
|
|
|
}
|