|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MintyPHP\Form; |
| 4 | + |
| 5 | +use DOMElement; |
| 6 | +use MintyPHP\Form\Validator\Validator; |
| 7 | + |
| 8 | +class SelectOrType implements Control |
| 9 | +{ |
| 10 | + use HtmlElement; |
| 11 | + |
| 12 | + protected string $name = ''; |
| 13 | + /** @var array<string|int,string> $options */ |
| 14 | + protected array $options = []; |
| 15 | + /** @var array<string|int,string> $originalOptions */ |
| 16 | + protected array $originalOptions = []; |
| 17 | + protected string $value = ''; |
| 18 | + protected string $placeholder = ''; |
| 19 | + protected string $typeCaption = 'Type a value ...'; |
| 20 | + |
| 21 | + protected bool $disabled = false; |
| 22 | + protected bool $readonly = false; |
| 23 | + protected bool $required = false; |
| 24 | + protected bool $autofocus = false; |
| 25 | + protected string $autocomplete = ''; |
| 26 | + |
| 27 | + protected bool $inputMode = false; |
| 28 | + |
| 29 | + public function __construct() |
| 30 | + { |
| 31 | + $this->tag('select'); |
| 32 | + } |
| 33 | + |
| 34 | + public function name(string $name): self |
| 35 | + { |
| 36 | + $this->name = $name; |
| 37 | + return $this; |
| 38 | + } |
| 39 | + |
| 40 | + public function getName(): string |
| 41 | + { |
| 42 | + return $this->name; |
| 43 | + } |
| 44 | + |
| 45 | + public function disabled(): self |
| 46 | + { |
| 47 | + $this->disabled = true; |
| 48 | + return $this; |
| 49 | + } |
| 50 | + |
| 51 | + public function readonly(): self |
| 52 | + { |
| 53 | + $this->readonly = true; |
| 54 | + return $this; |
| 55 | + } |
| 56 | + |
| 57 | + public function required(): self |
| 58 | + { |
| 59 | + $this->required = true; |
| 60 | + return $this; |
| 61 | + } |
| 62 | + |
| 63 | + public function autofocus(): self |
| 64 | + { |
| 65 | + $this->autofocus = true; |
| 66 | + return $this; |
| 67 | + } |
| 68 | + |
| 69 | + public function autocomplete(string $value): self |
| 70 | + { |
| 71 | + $this->autocomplete = $value; |
| 72 | + return $this; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param array<string|int,string> $options |
| 77 | + */ |
| 78 | + public function options(array $options): self |
| 79 | + { |
| 80 | + $this->options = $options; |
| 81 | + if (!isset($this->options[''])) { |
| 82 | + $this->options = ['' => '...'] + $this->options; // Add empty option if not present |
| 83 | + } |
| 84 | + $this->originalOptions = $this->options; |
| 85 | + $this->options = $this->options + ['!type!' => $this->typeCaption]; |
| 86 | + return $this; |
| 87 | + } |
| 88 | + |
| 89 | + public function value(string $value): self |
| 90 | + { |
| 91 | + $this->value = $value; |
| 92 | + $this->options = $this->originalOptions; |
| 93 | + if ($this->value && !in_array($this->value, $this->options)) { |
| 94 | + $this->options[$this->value] = $this->value; |
| 95 | + } |
| 96 | + $this->options = $this->options + ['!type!' => $this->typeCaption]; |
| 97 | + return $this; |
| 98 | + } |
| 99 | + |
| 100 | + public function placeholder(string $placeholder): self |
| 101 | + { |
| 102 | + $this->placeholder = $placeholder; |
| 103 | + return $this; |
| 104 | + } |
| 105 | + |
| 106 | + public function typeCaption(string $typeCaption): self |
| 107 | + { |
| 108 | + $this->typeCaption = $typeCaption; |
| 109 | + $this->options['!type!'] = $this->typeCaption; |
| 110 | + return $this; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @param array<string, string|null> $data |
| 115 | + */ |
| 116 | + public function fill(array $data): void |
| 117 | + { |
| 118 | + $this->value($data[$this->name] ?? ''); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @return array<string, string|string[]|null> $data |
| 123 | + */ |
| 124 | + public function extract(bool $withNulls = false): array |
| 125 | + { |
| 126 | + if (!$this->name) { |
| 127 | + return []; |
| 128 | + } |
| 129 | + if ($this->disabled) { |
| 130 | + return []; |
| 131 | + } |
| 132 | + $value = trim($this->values[0] ?? ''); |
| 133 | + if ($withNulls) { |
| 134 | + $value = strlen($value) ? $value : null; |
| 135 | + } |
| 136 | + return [$this->name => $value]; |
| 137 | + } |
| 138 | + |
| 139 | + public function validate(Validator $validator): string |
| 140 | + { |
| 141 | + if (!$this->required && !$this->value) { |
| 142 | + return ''; |
| 143 | + } |
| 144 | + if (!in_array($this->value, $this->options)) { |
| 145 | + return 'Invalid option value'; |
| 146 | + } |
| 147 | + return $validator->validate($this->value); |
| 148 | + } |
| 149 | + |
| 150 | + public function setError(string $message): void {} |
| 151 | + |
| 152 | + public function renderDom(\DOMDocument $doc): \DOMElement |
| 153 | + { |
| 154 | + $select = $this->renderElement($doc); |
| 155 | + $select->setAttribute('name', $this->name); |
| 156 | + if ($this->required) { |
| 157 | + $select->setAttribute('required', 'required'); |
| 158 | + } |
| 159 | + $i = 0; |
| 160 | + foreach ($this->options as $key => $value) { |
| 161 | + $option = $doc->createElement('option', $value); |
| 162 | + $option->setAttribute('value', strval($key)); |
| 163 | + if ($key == $this->value) { |
| 164 | + $option->setAttribute('selected', 'selected'); |
| 165 | + } |
| 166 | + $select->appendChild($option); |
| 167 | + $i += 1; |
| 168 | + if ($i == count($this->originalOptions)) { |
| 169 | + $hr = $doc->createElement('hr'); |
| 170 | + $select->appendChild($hr); |
| 171 | + } |
| 172 | + } |
| 173 | + $select->setAttribute('onchange', "var last = this.options[this.options.length - 1]; var hasPrevious = last.previousSibling.nodeName == 'HR'; if (this.options.length - 1 == this.selectedIndex) { var str = prompt(last.text,last.previousSibling.text); if (str) { if (hasPrevious) { opt = document.createElement('option'); this.insertBefore(opt, last); } else { opt = last.previousSibling; } opt.value = opt.text = str; this.selectedIndex -= 1; } else { this.selectedIndex = this.dataset.lastIndex; } } this.dataset.lastIndex = this.selectedIndex;"); |
| 174 | + return $select; |
| 175 | + } |
| 176 | +} |
0 commit comments