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