Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
82.35% covered (warning)
82.35%
28 / 34
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Field
82.35% covered (warning)
82.35%
28 / 34
0.00% covered (danger)
0.00%
0 / 1
18.59
0.00% covered (danger)
0.00%
0 / 1
 buildFieldDictionary
82.35% covered (warning)
82.35%
28 / 34
0.00% covered (danger)
0.00%
0 / 1
18.59
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Pdf\Core\Interactive\Form;
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;
14
15/**
16 * Base class for all interactive form field types.
17 * Maps to the common entries in the Field dictionary.
18 */
19abstract class Field extends PdfObject
20{
21    public ?PdfName $ft = null;        // /FT - field type (Btn/Tx/Ch/Sig)
22    public ?PdfReference $parent = null; // /Parent
23    /** @var array<int, PdfReference> */
24    public array $kids = [];            // /Kids
25    public ?PdfString $t = null;        // /T - partial name - required
26    public ?PdfString $tu = null;       // /TU - user name
27    public ?PdfString $tm = null;       // /TM - mapping name
28    public int $ff = 0;                 // /Ff - field flags
29    public mixed $v = null;             // /V - value
30    public mixed $dv = null;            // /DV - default value
31    public ?PdfDictionary $aa = null;   // /AA - additional actions
32    public ?PdfString $da = null;       // /DA - default appearance (variable text, ยง12.7.4.3)
33    public ?int $q = null;              // /Q  - quadding / justification (variable text)
34    public ?PdfString $ds = null;       // /DS - default style (rich-text variable text)
35    public ?PdfString $rv = null;       // /RV - rich-text value (variable text)
36
37    /**
38     * Build the common field dictionary entries.
39     */
40    protected function buildFieldDictionary(): PdfDictionary
41    {
42        $dict = new PdfDictionary();
43
44        if ($this->ft !== null) {
45            $dict->set('FT', $this->ft);
46        }
47        if ($this->parent !== null) {
48            $dict->set('Parent', $this->parent);
49        }
50        if (!empty($this->kids)) {
51            $dict->set('Kids', new PdfArray($this->kids));
52        }
53        if ($this->t !== null) {
54            $dict->set('T', $this->t);
55        }
56        if ($this->tu !== null) {
57            $dict->set('TU', $this->tu);
58        }
59        if ($this->tm !== null) {
60            $dict->set('TM', $this->tm);
61        }
62        if ($this->ff !== 0) {
63            $dict->set('Ff', new PdfNumber($this->ff));
64        }
65        if ($this->v !== null) {
66            if ($this->v instanceof \Phpdftk\Pdf\Core\Serializable) {
67                $dict->set('V', $this->v);
68            } else {
69                $dict->set('V', new PdfString((string) $this->v));
70            }
71        }
72        if ($this->dv !== null) {
73            if ($this->dv instanceof \Phpdftk\Pdf\Core\Serializable) {
74                $dict->set('DV', $this->dv);
75            } else {
76                $dict->set('DV', new PdfString((string) $this->dv));
77            }
78        }
79        if ($this->aa !== null) {
80            $dict->set('AA', $this->aa);
81        }
82        if ($this->da !== null) {
83            $dict->set('DA', $this->da);
84        }
85        if ($this->q !== null) {
86            $dict->set('Q', new PdfNumber($this->q));
87        }
88        if ($this->ds !== null) {
89            $dict->set('DS', $this->ds);
90        }
91        if ($this->rv !== null) {
92            $dict->set('RV', $this->rv);
93        }
94
95        return $dict;
96    }
97}