Files

44 lines
756 B
PHP
Raw Permalink Normal View History

2021-08-11 15:13:35 +08:00
<?php
declare(strict_types=1);
namespace validator;
/**
* Class EnumValidator
* @package validator
*/
class EnumValidator extends BaseValidator
{
public array $value = [];
/**
* @return bool
*/
public function trigger(): bool
{
2022-01-04 16:53:31 +08:00
if (empty($this->params) || !isset($this->params[$this->field])) {
2021-08-11 15:13:35 +08:00
return $this->addError('The param :attribute is null');
}
2022-01-04 16:53:31 +08:00
if (is_null($this->params[$this->field])) {
2021-08-11 15:13:35 +08:00
return $this->addError('The param :attribute is null');
}
2022-01-04 16:53:31 +08:00
if (!in_array($this->params[$this->field], $this->value)) {
2021-08-11 15:13:35 +08:00
return $this->addError($this->i());
}
return true;
}
/**
* @return string
*/
private function i(): string
{
return 'The param :attribute value only in ' . implode(',', $this->value);
}
}