Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.82% covered (warning)
81.82%
27 / 33
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
MetadataConstraint
81.82% covered (warning)
81.82%
27 / 33
0.00% covered (danger)
0.00%
0 / 1
6.22
0.00% covered (danger)
0.00%
0 / 1
 check
81.82% covered (warning)
81.82%
27 / 33
0.00% covered (danger)
0.00%
0 / 1
6.22
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Pdf\Conformance\Constraint;
6
7use Phpdftk\Pdf\Conformance\Inspection\DocumentInspector;
8use Phpdftk\Pdf\Conformance\Profile\ConformanceProfile;
9use Phpdftk\Pdf\Conformance\Result\ConformanceViolation;
10use Phpdftk\Pdf\Conformance\Result\ViolationSeverity;
11
12/**
13 * PDF/A clause 6.7: XMP metadata is required and must contain the
14 * conformance identification schema.
15 */
16final class MetadataConstraint implements ConformanceConstraint
17{
18    public function check(DocumentInspector $inspector, ConformanceProfile $profile): array
19    {
20        $violations = [];
21
22        if (!$inspector->hasXmpMetadata()) {
23            $violations[] = new ConformanceViolation(
24                clause: '6.7.2',
25                message: 'XMP metadata stream is required on the document Catalog',
26                severity: ViolationSeverity::Error,
27            );
28            return $violations;
29        }
30
31        $xmp = $inspector->getXmpBytes();
32        if ($xmp === null || trim($xmp) === '') {
33            $violations[] = new ConformanceViolation(
34                clause: '6.7.2',
35                message: 'XMP metadata stream is empty',
36                severity: ViolationSeverity::Error,
37            );
38            return $violations;
39        }
40
41        // Check for identification schema properties
42        $prefix = $profile->getXmpPrefix();
43        foreach ($profile->getXmpProperties() as $localName => $expectedValue) {
44            $tag = $prefix . ':' . $localName;
45            if (!str_contains($xmp, $tag)) {
46                $violations[] = new ConformanceViolation(
47                    clause: '6.7.11',
48                    message: sprintf(
49                        'XMP metadata must contain <%s>%s</%s> for %s-%s identification',
50                        $tag,
51                        $expectedValue,
52                        $tag,
53                        $profile->getFamily(),
54                        $profile->getLevel(),
55                    ),
56                    severity: ViolationSeverity::Error,
57                );
58            }
59        }
60
61        return $violations;
62    }
63}