| Total Complexity | 6 |
| Total Lines | 52 |
| Duplicated Lines | 0 % |
| Coverage | 40.91% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 12 | class Subset extends Rule |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var iterable |
||
| 16 | */ |
||
| 17 | private iterable $range; |
||
| 18 | /** |
||
| 19 | * @var bool whether the comparison is strict (both type and value must be the same) |
||
| 20 | */ |
||
| 21 | private bool $strict = false; |
||
| 22 | |||
| 23 | private string $iterableMessage = 'Value must be iterable'; |
||
| 24 | |||
| 25 | private string $subsetMessage = 'Value must be subset of...'; |
||
| 26 | |||
| 27 | 4 | public function __construct(iterable $range) |
|
| 28 | { |
||
| 29 | 4 | $this->range = $range; |
|
| 30 | 4 | } |
|
| 31 | |||
| 32 | 3 | protected function validateValue($value, ValidationContext $context = null): Result |
|
| 33 | { |
||
| 34 | 3 | $result = new Result(); |
|
| 35 | |||
| 36 | 3 | if (!is_iterable($value)) { |
|
| 37 | $result->addError($this->formatMessage($this->iterableMessage)); |
||
| 38 | return $result; |
||
| 39 | } |
||
| 40 | |||
| 41 | 3 | if (!ArrayHelper::isSubset($value, $this->range, $this->strict)) { |
|
| 42 | 1 | $result->addError($this->formatMessage($this->subsetMessage)); |
|
| 43 | } |
||
| 44 | |||
| 45 | 3 | return $result; |
|
| 46 | } |
||
| 47 | |||
| 48 | public function strict(): self |
||
| 53 | } |
||
| 54 | |||
| 55 | public function getOptions(): array |
||
| 68 |