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
Checkbox.php
137 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Form\Element; |
| 6 | |
| 7 | use AC\Form\Element; |
| 8 | |
| 9 | class Checkbox extends Element |
| 10 | { |
| 11 | protected ?bool $vertical = null; |
| 12 | |
| 13 | protected ?bool $multiple = null; |
| 14 | |
| 15 | protected ?bool $disabled = null; |
| 16 | |
| 17 | protected function get_type(): string |
| 18 | { |
| 19 | return 'checkbox'; |
| 20 | } |
| 21 | |
| 22 | protected function get_classes(): array |
| 23 | { |
| 24 | $classes = [ |
| 25 | $this->get_type() . '-labels', |
| 26 | ]; |
| 27 | |
| 28 | if ($this->is_vertical()) { |
| 29 | $classes[] = 'vertical'; |
| 30 | } |
| 31 | |
| 32 | return $classes; |
| 33 | } |
| 34 | |
| 35 | public function render(): string |
| 36 | { |
| 37 | $elements = $this->get_elements(); |
| 38 | |
| 39 | if (! $elements) { |
| 40 | return ''; |
| 41 | } |
| 42 | |
| 43 | $template = '<div class="%s">%s</div>'; |
| 44 | |
| 45 | return sprintf($template, implode(' ', $this->get_classes()), implode("\n", $elements)); |
| 46 | } |
| 47 | |
| 48 | private function get_elements(): ?array |
| 49 | { |
| 50 | if ($this->is_multiple()) { |
| 51 | $this->set_name($this->get_name() . '[]'); |
| 52 | } |
| 53 | |
| 54 | $options = $this->get_options(); |
| 55 | |
| 56 | if (empty($options)) { |
| 57 | return null; |
| 58 | } |
| 59 | |
| 60 | $elements = []; |
| 61 | |
| 62 | $value = (array)$this->get_value(); |
| 63 | |
| 64 | foreach ($options as $key => $label) { |
| 65 | $input = new Input((string)$this->get_name()); |
| 66 | |
| 67 | $input |
| 68 | ->set_value($key) |
| 69 | ->set_type($this->get_type()) |
| 70 | ->set_id($this->get_id() . '-' . $key); |
| 71 | |
| 72 | if (in_array($key, $value, true)) { |
| 73 | $input->set_attribute('checked', 'checked'); |
| 74 | } |
| 75 | |
| 76 | if ($this->disabled) { |
| 77 | $input->set_attribute('disabled', 'disabled'); |
| 78 | } |
| 79 | |
| 80 | $attributes = $this->get_attributes(); |
| 81 | |
| 82 | $elements[] = sprintf( |
| 83 | '<label %s>%s%s</label>', |
| 84 | $this->get_attributes_as_string($attributes), |
| 85 | $input->render(), |
| 86 | esc_html($label) |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | if ($description = $this->render_description()) { |
| 91 | $elements[] = $description; |
| 92 | } |
| 93 | |
| 94 | return $elements; |
| 95 | } |
| 96 | |
| 97 | public function set_multiple(bool $multiple): self |
| 98 | { |
| 99 | $this->multiple = $multiple; |
| 100 | |
| 101 | return $this; |
| 102 | } |
| 103 | |
| 104 | public function is_multiple(): bool |
| 105 | { |
| 106 | if (empty($this->multiple)) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | return $this->multiple; |
| 111 | } |
| 112 | |
| 113 | public function set_vertical(bool $vertical): self |
| 114 | { |
| 115 | $this->vertical = $vertical; |
| 116 | |
| 117 | return $this; |
| 118 | } |
| 119 | |
| 120 | public function is_vertical(): bool |
| 121 | { |
| 122 | if (empty($this->vertical)) { |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | return $this->vertical; |
| 127 | } |
| 128 | |
| 129 | public function set_disabled(bool $disabled): self |
| 130 | { |
| 131 | $this->disabled = $disabled; |
| 132 | |
| 133 | return $this; |
| 134 | } |
| 135 | |
| 136 | } |
| 137 |