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