Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
33 / 33 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
Input | |
100.00% |
33 / 33 |
|
100.00% |
9 / 9 |
17 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
setRequired | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
setDisabled | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
setReadonly | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
setValue | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
resetValue | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
validate | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
5 |
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 | */ |
14 | namespace Pop\Form\Element; |
15 | |
16 | /** |
17 | * Form input element class |
18 | * |
19 | * @category Pop |
20 | * @package Pop\Form |
21 | * @author Nick Sagona, III <dev@nolainteractive.com> |
22 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
23 | * @license http://www.popphp.org/license New BSD License |
24 | * @version 4.0.0 |
25 | */ |
26 | |
27 | class Input extends AbstractElement |
28 | { |
29 | |
30 | /** |
31 | * Constructor |
32 | * |
33 | * Instantiate the form input element, defaults to text |
34 | * |
35 | * @param string $name |
36 | * @param string $type |
37 | * @param ?string $value |
38 | * @param ?string $indent |
39 | */ |
40 | public function __construct(string $name, string $type = 'text', ?string $value = null, ?string $indent = null) |
41 | { |
42 | parent::__construct('input'); |
43 | |
44 | $this->setName($name); |
45 | $this->setAttributes([ |
46 | 'type' => $type, |
47 | 'name' => $name, |
48 | 'id' => $name, |
49 | 'value' => $value |
50 | ]); |
51 | |
52 | if ($indent !== null) { |
53 | $this->setIndent($indent); |
54 | } |
55 | } |
56 | |
57 | /** |
58 | * Set whether the form element is required |
59 | * |
60 | * @param bool $required |
61 | * @return Input |
62 | */ |
63 | public function setRequired(bool $required): Input |
64 | { |
65 | if ($required) { |
66 | $this->setAttribute('required', 'required'); |
67 | } else { |
68 | $this->removeAttribute('required'); |
69 | } |
70 | return parent::setRequired($required); |
71 | } |
72 | |
73 | /** |
74 | * Set whether the form element is disabled |
75 | * |
76 | * @param bool $disabled |
77 | * @return Input |
78 | */ |
79 | public function setDisabled(bool $disabled): Input |
80 | { |
81 | if ($disabled) { |
82 | $this->setAttribute('disabled', 'disabled'); |
83 | } else { |
84 | $this->removeAttribute('disabled'); |
85 | } |
86 | return parent::setDisabled($disabled); |
87 | } |
88 | |
89 | /** |
90 | * Set whether the form element is readonly |
91 | * |
92 | * @param bool $readonly |
93 | * @return Input |
94 | */ |
95 | public function setReadonly(bool $readonly): Input |
96 | { |
97 | if ($readonly) { |
98 | $this->setAttribute('readonly', 'readonly'); |
99 | } else { |
100 | $this->removeAttribute('readonly'); |
101 | } |
102 | return parent::setReadonly($readonly); |
103 | } |
104 | |
105 | /** |
106 | * Set the value of the form input element object |
107 | * |
108 | * @param mixed $value |
109 | * @return Input |
110 | */ |
111 | public function setValue(mixed $value): Input |
112 | { |
113 | $this->setAttribute('value', $value); |
114 | return $this; |
115 | } |
116 | |
117 | /** |
118 | * Reset the value of the form element |
119 | * |
120 | * @return Input |
121 | */ |
122 | public function resetValue(): Input |
123 | { |
124 | $this->setAttribute('value', ''); |
125 | return $this; |
126 | } |
127 | |
128 | /** |
129 | * Get the value of the form input element object |
130 | * |
131 | * @return ?string |
132 | */ |
133 | public function getValue(): ?string |
134 | { |
135 | return $this->getAttribute('value'); |
136 | } |
137 | |
138 | /** |
139 | * Get the type of the form input element object |
140 | * |
141 | * @return ?string |
142 | */ |
143 | public function getType(): ?string |
144 | { |
145 | return $this->getAttribute('type'); |
146 | } |
147 | |
148 | /** |
149 | * Validate the form element object |
150 | * |
151 | * @param array $formValues |
152 | * @return bool |
153 | */ |
154 | public function validate(array $formValues = []): bool |
155 | { |
156 | $value = $this->getValue(); |
157 | |
158 | // Check if the element is required |
159 | if (($this->required) && empty($value) && !($this->getType() == 'number' && $value === '0')) { |
160 | $this->errors[] = 'This field is required.'; |
161 | } |
162 | |
163 | $this->validateValue($value, $formValues); |
164 | |
165 | return (count($this->errors) == 0); |
166 | } |
167 | |
168 | } |