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
Radio
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
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Form\Element\Input;
15
16use Pop\Form\Element;
17
18/**
19 * Form radio element class
20 *
21 * @category   Pop
22 * @package    Pop\Form
23 * @author     Nick Sagona, III <dev@nolainteractive.com>
24 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
25 * @license    http://www.popphp.org/license     New BSD License
26 * @version    4.0.0
27 */
28
29class Radio 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, 'radio', $value, $indent);
43    }
44
45    /**
46     * Set the value of the form input element object
47     *
48     * @param  mixed $value
49     * @return Radio
50     */
51    public function setValue(mixed $value): Radio
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 Radio
65     */
66    public function resetValue(): Radio
67    {
68        $this->uncheck();
69        return $this;
70    }
71
72    /**
73     * Set the checkbox to checked
74     *
75     * @return Radio
76     */
77    public function check(): Radio
78    {
79        $this->setAttribute('checked', 'checked');
80        return $this;
81    }
82
83    /**
84     * Set the checkbox to checked
85     *
86     * @return Radio
87     */
88    public function uncheck(): Radio
89    {
90        $this->removeAttribute('checked');
91        return $this;
92    }
93
94    /**
95     * Determine if the radio value is checked
96     *
97     * @return bool
98     */
99    public function isChecked(): bool
100    {
101        return ($this->getAttribute('checked') == 'checked');
102    }
103
104}