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