Checkbox.php
2 days ago
Input.php
2 days ago
MultiSelect.php
2 days ago
Radio.php
2 days ago
Select.php
2 days ago
Input.php
59 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Form\Element; |
| 6 | |
| 7 | use AC\Form\Element; |
| 8 | |
| 9 | class Input extends Element |
| 10 | { |
| 11 | protected function is_valid_type(string $type): bool |
| 12 | { |
| 13 | $valid_types = [ |
| 14 | 'hidden', |
| 15 | 'text', |
| 16 | 'number', |
| 17 | 'email', |
| 18 | 'radio', |
| 19 | 'checkbox', |
| 20 | ]; |
| 21 | |
| 22 | return in_array($type, $valid_types); |
| 23 | } |
| 24 | |
| 25 | public function render(): string |
| 26 | { |
| 27 | $template = '<input %s>%s'; |
| 28 | |
| 29 | $attributes = $this->get_attributes(); |
| 30 | $attributes['name'] = $this->get_name(); |
| 31 | $attributes['id'] = $this->get_id(); |
| 32 | $attributes['value'] = $this->get_value(); |
| 33 | $attributes['type'] = $this->get_type(); |
| 34 | |
| 35 | return sprintf($template, $this->get_attributes_as_string($attributes), $this->render_description()); |
| 36 | } |
| 37 | |
| 38 | public function get_type(): string |
| 39 | { |
| 40 | $type = $this->get_attribute('type'); |
| 41 | |
| 42 | if (! $type) { |
| 43 | return 'text'; |
| 44 | } |
| 45 | |
| 46 | return strtolower($type); |
| 47 | } |
| 48 | |
| 49 | public function set_type(string $type): self |
| 50 | { |
| 51 | if ($this->is_valid_type($type)) { |
| 52 | $this->set_attribute('type', $type); |
| 53 | } |
| 54 | |
| 55 | return $this; |
| 56 | } |
| 57 | |
| 58 | } |
| 59 |