Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.31% covered (success)
92.31%
36 / 39
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Annotation
92.31% covered (success)
92.31%
36 / 39
66.67% covered (warning)
66.67%
2 / 3
19.16
0.00% covered (danger)
0.00%
0 / 1
 getSubtype
n/a
0 / 0
n/a
0 / 0
0
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 buildDictionary
91.89% covered (success)
91.89%
34 / 37
0.00% covered (danger)
0.00%
0 / 1
17.15
 toPdf
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Pdf\Core\Annotation;
6
7use Phpdftk\Pdf\Core\PdfArray;
8use Phpdftk\Pdf\Core\PdfDictionary;
9use Phpdftk\Pdf\Core\PdfName;
10use Phpdftk\Pdf\Core\PdfNumber;
11use Phpdftk\Pdf\Core\PdfObject;
12use Phpdftk\Pdf\Core\PdfReference;
13use Phpdftk\Pdf\Core\PdfString;
14use Phpdftk\Pdf\Core\Serializable;
15
16/**
17 * Abstract base class for all PDF annotation types (/Type /Annot).
18 */
19abstract class Annotation extends PdfObject
20{
21    public const PDF_TYPE = 'Annot';
22
23    /**
24     * Returns the /Subtype value for this annotation.
25     */
26    abstract public function getSubtype(): string;
27
28    public PdfArray $rect;                     // /Rect - required [x1 y1 x2 y2]
29    public ?PdfString $contents = null;        // /Contents
30    public ?PdfReference $p = null;            // /P - page reference
31    public ?PdfString $nm = null;              // /NM - annotation name
32    public ?PdfString $m = null;               // /M - modification date
33    public int $f = 0;                         // /F - flags
34    public PdfDictionary|AppearanceDict|null $ap = null; // /AP - appearance streams
35    public ?PdfName $as = null;                // /AS - appearance state
36    public ?PdfArray $border = null;           // /Border
37    public ?PdfArray $c = null;                // /C - color
38    public ?int $structParent = null;          // /StructParent
39    public ?PdfReference $oc = null;           // /OC
40    public ?Serializable $bs = null;           // /BS - BorderStyle dict
41    public ?PdfArray $af = null;               // /AF - associated files
42    public ?PdfNumber $ca = null;              // /ca - constant opacity
43    public ?PdfName $bm = null;                // /BM - blend mode
44    public ?PdfString $lang = null;            // /Lang - language
45
46    public function __construct(PdfArray $rect)
47    {
48        $this->rect = $rect;
49    }
50
51    /**
52     * Build the common annotation dictionary entries.
53     * Subclasses call this and add their own entries.
54     */
55    protected function buildDictionary(): PdfDictionary
56    {
57        $dict = new PdfDictionary();
58        $dict->set('Type', new PdfName(self::PDF_TYPE));
59        $dict->set('Subtype', new PdfName($this->getSubtype()));
60        $dict->set('Rect', $this->rect);
61
62        if ($this->contents !== null) {
63            $dict->set('Contents', $this->contents);
64        }
65        if ($this->p !== null) {
66            $dict->set('P', $this->p);
67        }
68        if ($this->nm !== null) {
69            $dict->set('NM', $this->nm);
70        }
71        if ($this->m !== null) {
72            $dict->set('M', $this->m);
73        }
74        if ($this->f !== 0) {
75            $dict->set('F', new PdfNumber($this->f));
76        }
77        if ($this->ap !== null) {
78            $dict->set('AP', $this->ap);
79        }
80        if ($this->as !== null) {
81            $dict->set('AS', $this->as);
82        }
83        if ($this->border !== null) {
84            $dict->set('Border', $this->border);
85        }
86        if ($this->c !== null) {
87            $dict->set('C', $this->c);
88        }
89        if ($this->structParent !== null) {
90            $dict->set('StructParent', new PdfNumber($this->structParent));
91        }
92        if ($this->oc !== null) {
93            $dict->set('OC', $this->oc);
94        }
95        if ($this->bs !== null) {
96            $dict->set('BS', $this->bs);
97        }
98        if ($this->af !== null) {
99            $dict->set('AF', $this->af);
100        }
101        if ($this->ca !== null) {
102            $dict->set('ca', $this->ca);
103        }
104        if ($this->bm !== null) {
105            $dict->set('BM', $this->bm);
106        }
107        if ($this->lang !== null) {
108            $dict->set('Lang', $this->lang);
109        }
110
111        return $dict;
112    }
113
114    public function toPdf(): string
115    {
116        return $this->buildDictionary()->toPdf();
117    }
118}