Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
10 / 10 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| Option | |
100.00% |
10 / 10 |
|
100.00% |
6 / 6 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setValue | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| select | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| deselect | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| isSelected | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | declare(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 | */ |
| 15 | namespace Pop\Form\Element\Select; |
| 16 | |
| 17 | use Pop\Dom\Child; |
| 18 | |
| 19 | /** |
| 20 | * Form select option element class |
| 21 | * |
| 22 | * @category Pop |
| 23 | * @package Pop\Form |
| 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 5.0.0 |
| 28 | */ |
| 29 | |
| 30 | class Option extends Child |
| 31 | { |
| 32 | |
| 33 | /** |
| 34 | * Constructor |
| 35 | * |
| 36 | * Instantiate the option element object |
| 37 | * |
| 38 | * @param string $value |
| 39 | * @param string $nodeValue |
| 40 | * @param array $options |
| 41 | */ |
| 42 | public function __construct(string $value, string $nodeValue, array $options = []) |
| 43 | { |
| 44 | parent::__construct('option', $nodeValue, $options); |
| 45 | $this->setValue($value); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Set the option value |
| 50 | * |
| 51 | * @param mixed $value |
| 52 | * @return Option |
| 53 | */ |
| 54 | public function setValue(mixed $value = null): Option |
| 55 | { |
| 56 | $this->setAttribute('value', $value); |
| 57 | return $this; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get the option value |
| 62 | * |
| 63 | * @return ?string |
| 64 | */ |
| 65 | public function getValue(): ?string |
| 66 | { |
| 67 | return $this->getAttribute('value'); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Select the option value |
| 72 | * |
| 73 | * @return Option |
| 74 | */ |
| 75 | public function select(): Option |
| 76 | { |
| 77 | $this->setAttribute('selected', 'selected'); |
| 78 | return $this; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Deselect the option value |
| 83 | * |
| 84 | * @return Option |
| 85 | */ |
| 86 | public function deselect(): Option |
| 87 | { |
| 88 | $this->removeAttribute('selected'); |
| 89 | return $this; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Determine if the option value is selected |
| 94 | * |
| 95 | * @return bool |
| 96 | */ |
| 97 | public function isSelected(): bool |
| 98 | { |
| 99 | return ($this->getAttribute('selected') == 'selected'); |
| 100 | } |
| 101 | |
| 102 | } |