Element
2 days ago
Element.php
2 days ago
Nonce.php
2 days ago
NonceFactory.php
2 days ago
PreviewNonce.php
2 days ago
Element.php
213 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Form; |
| 6 | |
| 7 | use AC\Helper; |
| 8 | use AC\Renderable; |
| 9 | |
| 10 | abstract class Element implements Renderable |
| 11 | { |
| 12 | protected array $attributes = []; |
| 13 | |
| 14 | /** |
| 15 | * Options for element like select |
| 16 | */ |
| 17 | protected array $options = []; |
| 18 | |
| 19 | /** |
| 20 | * The elements value |
| 21 | * @var mixed |
| 22 | */ |
| 23 | protected $value = null; |
| 24 | |
| 25 | protected string $label = ''; |
| 26 | |
| 27 | protected string $description = ''; |
| 28 | |
| 29 | /** |
| 30 | * Setup element with base name and id |
| 31 | */ |
| 32 | public function __construct(string $name, array $options = []) |
| 33 | { |
| 34 | $this->set_name($name); |
| 35 | $this->set_id($name); |
| 36 | $this->set_options($options); |
| 37 | } |
| 38 | |
| 39 | protected function render_description(): ?string |
| 40 | { |
| 41 | if (! $this->get_description()) { |
| 42 | return null; |
| 43 | } |
| 44 | |
| 45 | $template = '<p class="help-msg">%s</p>'; |
| 46 | |
| 47 | return sprintf($template, $this->get_description()); |
| 48 | } |
| 49 | |
| 50 | abstract public function render(): string; |
| 51 | |
| 52 | public function get_attribute(string $key): ?string |
| 53 | { |
| 54 | if (! isset($this->attributes[$key])) { |
| 55 | return null; |
| 56 | } |
| 57 | |
| 58 | return trim((string)$this->attributes[$key]); |
| 59 | } |
| 60 | |
| 61 | public function set_attribute(string $key, string $value): self |
| 62 | { |
| 63 | if ('value' === $key) { |
| 64 | $this->set_value($value); |
| 65 | |
| 66 | return $this; |
| 67 | } |
| 68 | |
| 69 | $this->attributes[$key] = $value; |
| 70 | |
| 71 | return $this; |
| 72 | } |
| 73 | |
| 74 | public function get_attributes(): array |
| 75 | { |
| 76 | return $this->attributes; |
| 77 | } |
| 78 | |
| 79 | public function set_attributes(array $attributes): self |
| 80 | { |
| 81 | foreach ($attributes as $key => $value) { |
| 82 | $this->set_attribute((string)$key, (string)$value); |
| 83 | } |
| 84 | |
| 85 | return $this; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get attributes as string |
| 90 | */ |
| 91 | protected function get_attributes_as_string(array $attributes): string |
| 92 | { |
| 93 | $output = []; |
| 94 | |
| 95 | foreach ($attributes as $key => $value) { |
| 96 | $output[] = $this->get_attribute_as_string((string)$key, (string)$value); |
| 97 | } |
| 98 | |
| 99 | return implode(' ', $output); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Render an attribute |
| 104 | */ |
| 105 | protected function get_attribute_as_string(string $key, ?string $value = null): string |
| 106 | { |
| 107 | if (null === $value) { |
| 108 | $value = $this->get_attribute($key); |
| 109 | } |
| 110 | |
| 111 | return Helper\Html::create()->get_attribute_as_string($key, $value); |
| 112 | } |
| 113 | |
| 114 | public function get_name(): ?string |
| 115 | { |
| 116 | return $this->get_attribute('name'); |
| 117 | } |
| 118 | |
| 119 | public function set_name(string $name): self |
| 120 | { |
| 121 | return $this->set_attribute('name', $name); |
| 122 | } |
| 123 | |
| 124 | public function get_id(): ?string |
| 125 | { |
| 126 | return $this->get_attribute('id'); |
| 127 | } |
| 128 | |
| 129 | public function set_id(string $id): self |
| 130 | { |
| 131 | return $this->set_attribute('id', $id); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @return mixed |
| 136 | */ |
| 137 | public function get_value() |
| 138 | { |
| 139 | return $this->value; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @param mixed $value |
| 144 | * |
| 145 | * @return $this |
| 146 | */ |
| 147 | public function set_value($value): self |
| 148 | { |
| 149 | $this->value = $value; |
| 150 | |
| 151 | return $this; |
| 152 | } |
| 153 | |
| 154 | public function set_class(string $class): self |
| 155 | { |
| 156 | $this->set_attribute('class', $class); |
| 157 | |
| 158 | return $this; |
| 159 | } |
| 160 | |
| 161 | public function add_class(string $class): self |
| 162 | { |
| 163 | $parts = explode(' ', (string)$this->get_attribute('class')); |
| 164 | $parts[] = $class; |
| 165 | |
| 166 | $this->set_class(implode(' ', $parts)); |
| 167 | |
| 168 | return $this; |
| 169 | } |
| 170 | |
| 171 | public function get_label(): string |
| 172 | { |
| 173 | return $this->label; |
| 174 | } |
| 175 | |
| 176 | public function set_label(string $label): self |
| 177 | { |
| 178 | $this->label = $label; |
| 179 | |
| 180 | return $this; |
| 181 | } |
| 182 | |
| 183 | public function set_options(array $options): self |
| 184 | { |
| 185 | $this->options = $options; |
| 186 | |
| 187 | return $this; |
| 188 | } |
| 189 | |
| 190 | public function get_options(): array |
| 191 | { |
| 192 | return $this->options; |
| 193 | } |
| 194 | |
| 195 | public function get_description(): string |
| 196 | { |
| 197 | return $this->description; |
| 198 | } |
| 199 | |
| 200 | public function set_description(string $description): self |
| 201 | { |
| 202 | $this->description = $description; |
| 203 | |
| 204 | return $this; |
| 205 | } |
| 206 | |
| 207 | public function __toString(): string |
| 208 | { |
| 209 | return $this->render(); |
| 210 | } |
| 211 | |
| 212 | } |
| 213 |