Lines
100.00%
6 / 6
Functions and Methods
100.00%
1 / 1
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| ActionField | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 5 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 5 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Contracts\Exposure\Action; | |
| 6 | ||
| 7 | use Stewart\Contracts\Exception\ExposureException; | |
| 8 | use Stewart\Contracts\Wire\ListOf; | |
| 9 | ||
| 10 | final 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 | } |