Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
12 / 12 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| Checkbox | |
100.00% |
12 / 12 |
|
100.00% |
6 / 6 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setValue | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| resetValue | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| check | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| uncheck | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| isChecked | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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\Form\Element\Input; |
| 15 | |
| 16 | use Pop\Form\Element; |
| 17 | |
| 18 | /** |
| 19 | * Form checkbox element class |
| 20 | * |
| 21 | * @category Pop |
| 22 | * @package Pop\Form |
| 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 4.2.6 |
| 27 | */ |
| 28 | |
| 29 | class Checkbox extends Element\Input |
| 30 | { |
| 31 | /** |
| 32 | * Constructor |
| 33 | * |
| 34 | * Instantiate the text input form element |
| 35 | * |
| 36 | * @param string $name |
| 37 | * @param ?string $value |
| 38 | * @param ?string $indent |
| 39 | */ |
| 40 | public function __construct(string $name, ?string $value = null, ?string $indent = null) |
| 41 | { |
| 42 | parent::__construct($name, 'checkbox', $value, $indent); |
| 43 | } |
| 44 | /** |
| 45 | * Set the value of the form input element object |
| 46 | * |
| 47 | * @param mixed $value |
| 48 | * @return Checkbox |
| 49 | */ |
| 50 | public function setValue(mixed $value = null): Checkbox |
| 51 | { |
| 52 | if ($value == $this->getAttribute('value')) { |
| 53 | $this->check(); |
| 54 | } else { |
| 55 | $this->uncheck(); |
| 56 | } |
| 57 | return $this; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Reset the value of the form element |
| 62 | * |
| 63 | * @return Checkbox |
| 64 | */ |
| 65 | public function resetValue(): Checkbox |
| 66 | { |
| 67 | $this->uncheck(); |
| 68 | return $this; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Set the checkbox to checked |
| 73 | * |
| 74 | * @return Checkbox |
| 75 | */ |
| 76 | public function check(): Checkbox |
| 77 | { |
| 78 | $this->setAttribute('checked', 'checked'); |
| 79 | return $this; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Set the checkbox to checked |
| 84 | * |
| 85 | * @return Checkbox |
| 86 | */ |
| 87 | public function uncheck(): Checkbox |
| 88 | { |
| 89 | $this->removeAttribute('checked'); |
| 90 | return $this; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Determine if the checkbox value is checked |
| 95 | * |
| 96 | * @return bool |
| 97 | */ |
| 98 | public function isChecked(): bool |
| 99 | { |
| 100 | return ($this->getAttribute('checked') == 'checked'); |
| 101 | } |
| 102 | |
| 103 | } |