Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
21 / 21
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractField
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
21 / 21
27
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 setName
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setValue
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setDefaultValue
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setFont
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getFont
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setSize
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getSize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setFontColor
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getFontColor
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setReadOnly
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setRequired
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setNoExport
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDefaultValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setWidth
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getWidth
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setHeight
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getHeight
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFlags
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Pdf\Document\Page\Field;
16
17use Pop\Color\Color;
18
19/**
20 * Pdf abstract form field class
21 *
22 * @category   Pop
23 * @package    Pop\Pdf
24 * @author     Nick Sagona, III <nick@popphp.org>
25 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
26 * @license    https://www.popphp.org/license     New BSD License
27 * @version    6.0.0
28 */
29abstract class AbstractField implements FieldInterface
30{
31
32    /**
33     * Field name
34     * @var ?string
35     */
36    protected ?string $name = null;
37
38    /**
39     * Text field width
40     * @var ?int
41     */
42    protected ?int $width = null;
43
44    /**
45     * Text field height
46     * @var ?int
47     */
48    protected ?int $height = null;
49
50    /**
51     * Field value
52     * @var ?string
53     */
54    protected ?string $value = null;
55
56    /**
57     * Field default value
58     * @var ?string
59     */
60    protected ?string $defaultValue = null;
61
62    /**
63     * Text field font
64     * @var ?string
65     */
66    protected ?string $font = null;
67
68    /**
69     * Text field font size
70     * @var int
71     */
72    protected int $size = 12;
73
74    /**
75     * Field font color
76     * @var ?Color\ColorInterface
77     */
78    protected ?Color\ColorInterface $fontColor = null;
79
80    /**
81     * Field flag bits
82     * @var array
83     */
84    protected array $flagBits = [];
85
86    /**
87     * Constructor
88     *
89     * Instantiate a PDF text field object.
90     *
91     * @param  string  $name
92     * @param  ?string $font
93     * @param  int     $size
94     */
95    public function __construct(string $name, ?string $font = null, int $size = 12)
96    {
97        $this->setName($name);
98        $this->setSize($size);
99        if ($font !== null) {
100            $this->setFont($font);
101        }
102    }
103
104    /**
105     * Set the field name
106     *
107     * @param  string $name
108     * @return AbstractField
109     */
110    public function setName(string $name): AbstractField
111    {
112        $this->name = $name;
113        return $this;
114    }
115
116    /**
117     * Set the field value
118     *
119     * @param  string $value
120     * @return AbstractField
121     */
122    public function setValue(string $value): AbstractField
123    {
124        $this->value = $value;
125        return $this;
126    }
127
128    /**
129     * Set the field default value
130     *
131     * @param  string $value
132     * @return AbstractField
133     */
134    public function setDefaultValue(string $value): AbstractField
135    {
136        $this->defaultValue = $value;
137        return $this;
138    }
139
140    /**
141     * Set the font
142     *
143     * @param  string $font
144     * @return AbstractField
145     */
146    public function setFont(string $font): AbstractField
147    {
148        $this->font = $font;
149        return $this;
150    }
151
152    /**
153     * Get the font
154     *
155     * @return ?string
156     */
157    public function getFont(): ?string
158    {
159        return $this->font;
160    }
161
162    /**
163     * Set the font size
164     *
165     * @param  int $size
166     * @return AbstractField
167     */
168    public function setSize(int $size): AbstractField
169    {
170        $this->size = $size;
171        return $this;
172    }
173
174    /**
175     * Get the font size
176     *
177     * @return int
178     */
179    public function getSize(): int
180    {
181        return $this->size;
182    }
183
184    /**
185     * Set the font color
186     *
187     * @param  Color\ColorInterface $color
188     * @return AbstractField
189     */
190    public function setFontColor(Color\ColorInterface $color): AbstractField
191    {
192        $this->fontColor = $color;
193        return $this;
194    }
195
196    /**
197     * Get the field font color
198     *
199     * @return ?Color\ColorInterface
200     */
201    public function getFontColor(): ?Color\ColorInterface
202    {
203        return $this->fontColor;
204    }
205
206    /**
207     * Set read-only
208     *
209     * @return AbstractField
210     */
211    public function setReadOnly(): AbstractField
212    {
213        if (!in_array(1, $this->flagBits)) {
214            $this->flagBits[] = 1;
215        }
216        return $this;
217    }
218
219    /**
220     * Set required
221     *
222     * @return AbstractField
223     */
224    public function setRequired(): AbstractField
225    {
226        if (!in_array(2, $this->flagBits)) {
227            $this->flagBits[] = 2;
228        }
229        return $this;
230    }
231
232    /**
233     * Set no export
234     *
235     * @return AbstractField
236     */
237    public function setNoExport(): AbstractField
238    {
239        if (!in_array(3, $this->flagBits)) {
240            $this->flagBits[] = 3;
241        }
242        return $this;
243    }
244
245    /**
246     * Get the field name
247     *
248     * @return ?string
249     */
250    public function getName(): ?string
251    {
252        return $this->name;
253    }
254
255    /**
256     * Get the field value
257     *
258     * @return ?string
259     */
260    public function getValue(): ?string
261    {
262        return $this->value;
263    }
264
265    /**
266     * Get the field default value
267     *
268     * @return ?string
269     */
270    public function getDefaultValue(): ?string
271    {
272        return $this->defaultValue;
273    }
274
275    /**
276     * Set the field width
277     *
278     * @param  int $width
279     * @return AbstractField
280     */
281    public function setWidth(int $width): AbstractField
282    {
283        $this->width = $width;
284        return $this;
285    }
286
287    /**
288     * Get the field width
289     *
290     * @return ?int
291     */
292    public function getWidth(): ?int
293    {
294        return $this->width;
295    }
296
297    /**
298     * Set the field height
299     *
300     * @param  int $height
301     * @return AbstractField
302     */
303    public function setHeight(int $height): AbstractField
304    {
305        $this->height = $height;
306        return $this;
307    }
308
309    /**
310     * Get the field height
311     *
312     * @return ?int
313     */
314    public function getHeight(): ?int
315    {
316        return $this->height;
317    }
318
319    /**
320     * Get the flags value
321     *
322     * @return int
323     */
324    protected function getFlags(): int
325    {
326        $flags = '';
327
328        for ($i = 1; $i <= 32; $i++) {
329            $flags = ((in_array($i, $this->flagBits)) ? '1' : '0') . $flags;
330        }
331
332        return bindec($flags);
333    }
334
335}