Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| ConformanceValidator | |
100.00% |
18 / 18 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| validate | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
4 | |||
| validateAll | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Pdf\Conformance\Validator; |
| 6 | |
| 7 | use Phpdftk\Pdf\Conformance\Inspection\DocumentInspector; |
| 8 | use Phpdftk\Pdf\Conformance\Profile\ConformanceProfile; |
| 9 | use Phpdftk\Pdf\Conformance\Result\ConformanceResult; |
| 10 | use Phpdftk\Pdf\Conformance\Result\ViolationSeverity; |
| 11 | |
| 12 | /** |
| 13 | * Orchestrates conformance validation: runs all applicable constraints |
| 14 | * for a profile against a document inspector and returns a result. |
| 15 | */ |
| 16 | final class ConformanceValidator |
| 17 | { |
| 18 | private ProfileConstraintRegistry $registry; |
| 19 | |
| 20 | public function __construct(?ProfileConstraintRegistry $registry = null) |
| 21 | { |
| 22 | $this->registry = $registry ?? new ProfileConstraintRegistry(); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Validate a document against a single profile. |
| 27 | */ |
| 28 | public function validate(DocumentInspector $inspector, ConformanceProfile $profile): ConformanceResult |
| 29 | { |
| 30 | $violations = []; |
| 31 | |
| 32 | foreach ($this->registry->getConstraints($profile) as $constraint) { |
| 33 | $violations = [...$violations, ...$constraint->check($inspector, $profile)]; |
| 34 | } |
| 35 | |
| 36 | $hasErrors = false; |
| 37 | foreach ($violations as $v) { |
| 38 | if ($v->severity === ViolationSeverity::Error) { |
| 39 | $hasErrors = true; |
| 40 | break; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return new ConformanceResult( |
| 45 | profile: $profile, |
| 46 | isCompliant: !$hasErrors, |
| 47 | violations: $violations, |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Validate a document against multiple profiles. |
| 53 | * |
| 54 | * @param ConformanceProfile[] $profiles |
| 55 | * @return list<ConformanceResult> |
| 56 | */ |
| 57 | public function validateAll(DocumentInspector $inspector, array $profiles): array |
| 58 | { |
| 59 | return array_map( |
| 60 | fn(ConformanceProfile $p) => $this->validate($inspector, $p), |
| 61 | $profiles, |
| 62 | ); |
| 63 | } |
| 64 | } |