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