Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
FilterConstraint
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
8
100.00% covered (success)
100.00%
1 / 1
 check
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
8
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\Profile\PdfAProfile;
10use Phpdftk\Pdf\Conformance\Result\ConformanceViolation;
11use Phpdftk\Pdf\Conformance\Result\ViolationSeverity;
12use Phpdftk\Pdf\Core\PdfStream;
13
14/**
15 * PDF/A-1 clause 6.8: LZWDecode filter is prohibited.
16 */
17final class FilterConstraint implements ConformanceConstraint
18{
19    public function check(DocumentInspector $inspector, ConformanceProfile $profile): array
20    {
21        // LZWDecode is only prohibited in PDF/A-1
22        if ($profile instanceof PdfAProfile && $profile->getPart() > 1) {
23            return [];
24        }
25
26        $violations = [];
27
28        foreach ($inspector->getRegisteredObjects() as $object) {
29            if (!$object instanceof PdfStream) {
30                continue;
31            }
32
33            if (!$object->dictionary->has('Filter')) {
34                continue;
35            }
36
37            $filter = $object->dictionary->get('Filter');
38            $filterStr = $filter instanceof \Phpdftk\Pdf\Core\PdfName ? $filter->value : '';
39
40            if ($filterStr === 'LZWDecode') {
41                $violations[] = new ConformanceViolation(
42                    clause: '6.1.10',
43                    message: 'LZWDecode filter is prohibited in PDF/A-1',
44                    severity: ViolationSeverity::Error,
45                    objectPath: 'Object[' . $object->objectNumber . ']',
46                );
47            }
48        }
49
50        return $violations;
51    }
52}