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