| Total Complexity | 5 |
| Total Lines | 38 |
| Duplicated Lines | 0 % |
| Coverage | 72.21% |
| Changes | 0 | ||
| 1 | <?php |
||
| 10 | final class Validator implements ValidatorInterface |
||
| 11 | { |
||
| 12 | private ?FormatterInterface $formatter; |
||
| 13 | |||
| 14 | 1 | public function __construct(?FormatterInterface $formatter = null) |
|
| 17 | 1 | } |
|
| 18 | |||
| 19 | /** |
||
| 20 | * @param DataSetInterface $dataSet |
||
| 21 | * @param Rule[][] $rules |
||
| 22 | * @psalm-param iterable<string, Rule[]> $rules |
||
| 23 | * |
||
| 24 | * @return ResultSet |
||
| 25 | */ |
||
| 26 | 1 | public function validate(DataSetInterface $dataSet, iterable $rules): ResultSet |
|
| 27 | { |
||
| 28 | 1 | $context = new ValidationContext($dataSet); |
|
| 29 | 1 | $results = new ResultSet(); |
|
| 30 | 1 | foreach ($rules as $attribute => $attributeRules) { |
|
| 31 | 1 | $aggregateRule = new Rules($attributeRules); |
|
| 32 | 1 | if ($this->formatter !== null) { |
|
| 33 | $aggregateRule = $aggregateRule->withFormatter($this->formatter); |
||
| 34 | } |
||
| 35 | 1 | $results->addResult( |
|
| 36 | 1 | $attribute, |
|
| 37 | 1 | $aggregateRule->validate($dataSet->getAttributeValue($attribute), $context->withAttribute($attribute)) |
|
| 38 | ); |
||
| 39 | } |
||
| 40 | 1 | return $results; |
|
| 41 | } |
||
| 42 | |||
| 43 | public function withFormatter(?FormatterInterface $formatter): self |
||
| 48 | } |
||
| 49 | } |
||
| 50 |