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