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