Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
48 / 48
100.00% covered (success)
100.00%
11 / 11
CRAP
100.00% covered (success)
100.00%
1 / 1
Choice
100.00% covered (success)
100.00%
48 / 48
100.00% covered (success)
100.00%
11 / 11
28
100.00% covered (success)
100.00%
1 / 1
 addOption
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 hasOptions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCombo
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setEdit
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setSort
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setMultiSelect
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setDoNotSpellCheck
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setCommitOnSelChange
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 isCombo
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isMultiSelect
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getStream
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
12
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 page button 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 */
29class Choice extends AbstractField
30{
31
32    /**
33     * Field options
34     * @var array
35     */
36    protected array $options = [];
37
38    /**
39     * Add an option
40     *
41     * @param  string $option
42     * @return Choice
43     */
44    public function addOption(string $option): Choice
45    {
46        $this->options[] = $option;
47        return $this;
48    }
49
50    /**
51     * Has options
52     *
53     * @return bool
54     */
55    public function hasOptions(): bool
56    {
57        return (count($this->options) > 0);
58    }
59
60    /**
61     * Set combo
62     *
63     * @return Choice
64     */
65    public function setCombo(): Choice
66    {
67        if (!in_array(18, $this->flagBits)) {
68            $this->flagBits[] = 18;
69        }
70        return $this;
71    }
72
73    /**
74     * Set edit
75     *
76     * @return Choice
77     */
78    public function setEdit(): Choice
79    {
80        if (!in_array(19, $this->flagBits)) {
81            $this->flagBits[] = 19;
82        }
83        return $this;
84    }
85
86    /**
87     * Set sort
88     *
89     * @return Choice
90     */
91    public function setSort(): Choice
92    {
93        if (!in_array(20, $this->flagBits)) {
94            $this->flagBits[] = 20;
95        }
96        return $this;
97    }
98
99    /**
100     * Set multi-select
101     *
102     * @return Choice
103     */
104    public function setMultiSelect(): Choice
105    {
106        if (!in_array(22, $this->flagBits)) {
107            $this->flagBits[] = 22;
108        }
109        return $this;
110    }
111
112    /**
113     * Set do not spell check
114     *
115     * @return Choice
116     */
117    public function setDoNotSpellCheck(): Choice
118    {
119        if (!in_array(23, $this->flagBits)) {
120            $this->flagBits[] = 23;
121        }
122        return $this;
123    }
124
125    /**
126     * Set commit on select change
127     *
128     * @return Choice
129     */
130    public function setCommitOnSelChange(): Choice
131    {
132        if (!in_array(27, $this->flagBits)) {
133            $this->flagBits[] = 27;
134        }
135        return $this;
136    }
137
138    /**
139     * Is combo
140     *
141     * @return bool
142     */
143    public function isCombo(): bool
144    {
145        return in_array(18, $this->flagBits);
146    }
147
148    /**
149     * Is multi-select
150     *
151     * @return bool
152     */
153    public function isMultiSelect(): bool
154    {
155        return in_array(22, $this->flagBits);
156    }
157
158    /**
159     * Get the field stream
160     *
161     * @param  int     $i
162     * @param  int     $pageIndex
163     * @param  ?string $fontReference
164     * @param  int     $x
165     * @param  int     $y
166     * @return string
167     */
168    public function getStream(int $i, int $pageIndex, ?string $fontReference, int $x, int $y): string
169    {
170        $text    = null;
171        $options = null;
172        $color   = '0 g';
173
174        if ($this->fontColor !== null) {
175            if ($this->fontColor instanceof Color\Rgb) {
176                $color = $this->fontColor->render(Color\Rgb::PERCENT) . " rg";
177            } else if ($this->fontColor instanceof Color\Cmyk) {
178                $color = $this->fontColor->render(Color\Cmyk::PERCENT) . " k";
179            } else if ($this->fontColor instanceof Color\Grayscale) {
180                $color = $this->fontColor->render(Color\Grayscale::PERCENT) . " g";
181            }
182        }
183
184        if ($fontReference !== null) {
185            $fontReference = substr($fontReference, 0, strpos($fontReference, ' '));
186            $text          = '    /DA(' . $fontReference . ' ' . $this->size . ' Tf ' . $color . ')';
187        }
188
189        $name    = ($this->name !== null) ? '    /T(' . $this->name . ')/TU(' . $this->name . ')/TM(' . $this->name . ')' : '';
190        $flags   = (count($this->flagBits) > 0) ? "\n    /Ff " . $this->getFlags() . "\n" : null;
191        $value   = ($this->value !== null) ? "\n    /V " . $this->value . "\n" : null;
192        $default = ($this->defaultValue !== null) ? "\n    /DV " . $this->defaultValue . "\n" : null;
193
194        if (count($this->options) > 0) {
195            $options = "    /Opt [ ";
196            foreach ($this->options as $option) {
197                $options .= '(' . $option . ') ';
198            }
199            $options .= "]\n";
200        }
201
202        // Return the stream
203        return "{$i} 0 obj\n<<\n    /Type /Annot\n    /Subtype /Widget\n    /FT /Ch\n    /Rect [{$x} {$y} " .
204            ($this->width + $x) . " " . ($this->height + $y) . "]{$value}{$default}\n    /P {$pageIndex} 0 R\n" .
205            "    \n{$text}\n{$name}\n{$flags}\n{$options}>>\nendobj\n\n";
206    }
207
208}