Lines 100.00% 6 / 6
Methods 100.00% 1 / 1
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 6 / 6 100.00% 1 / 1 5
10final readonly class ActionField
11{
12    private const string NAME_PATTERN = '/\A[a-z][a-z0-9_]{0,63}\z/';
13
14    /**
15     * @param list<string> $options
16     * @throws ExposureException
17     */
18    public function __construct(
19        public string $name,
20        public ActionFieldType $type = ActionFieldType::Text,
21        public bool $required = false,
22        public ?string $description = null,
23        #[ListOf('string')]
24        public array $options = [],
25    ) {
26        if (preg_match(self::NAME_PATTERN, $name) !== 1) {
27            throw ExposureException::configInvalid(\sprintf('The action field name "%s" must be lower snake case.', $name));
28        }
29
30        if (($type === ActionFieldType::Select) !== ($options !== [])) {
31            throw ExposureException::configInvalid(\sprintf('The action field "%s" takes options exactly when its type is select.', $name));
32        }
33
34        if (\in_array('', $options, true) || \count(array_unique($options)) !== \count($options)) {
35            throw ExposureException::configInvalid(\sprintf('The options of action field "%s" must be distinct and not empty.', $name));
36        }
37    }
38}