-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractSelect.php
More file actions
206 lines (185 loc) · 6.43 KB
/
AbstractSelect.php
File metadata and controls
206 lines (185 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
declare(strict_types=1);
namespace PHPForge\Html\FormControl\Base;
use InvalidArgumentException;
use PHPForge\{
Html\Attribute\Aria\HasAriaLabel,
Html\Attribute\CanBeAutofocus,
Html\Attribute\Custom\HasAttributes,
Html\Attribute\Custom\HasLabelCollection,
Html\Attribute\Custom\HasPrefixCollection,
Html\Attribute\Custom\HasSuffixCollection,
Html\Attribute\Field\HasGenerateField,
Html\Attribute\HasClass,
Html\Attribute\HasId,
Html\Attribute\HasStyle,
Html\Attribute\HasTabindex,
Html\Attribute\Input\CanBeDisabled,
Html\Attribute\Input\CanBeMultiple,
Html\Attribute\Input\CanBeRequired,
Html\Attribute\Input\HasName,
Html\Attribute\Input\HasSize,
Html\Attribute\Input\HasValue,
Html\Attribute\Tag\HasGroup,
Html\Attribute\Tag\HasItems,
Html\Attribute\Tag\HasItemsAttributes,
Html\Attribute\Tag\HasPrompt,
Html\FormControl\Label,
Html\Interop\InputInterface,
Html\Interop\RenderInterface,
Html\Interop\RequiredInterface,
Html\Interop\ValueInterface,
Html\Tag,
Widget\Element,
};
use Stringable;
use function array_merge;
use function get_debug_type;
use function implode;
use function in_array;
use function is_array;
use function is_bool;
use function is_object;
/**
* Provides a foundation for creating HTML `select` elements with various attributes and content.
*/
abstract class AbstractSelect extends Element implements
InputInterface,
RenderInterface,
RequiredInterface,
ValueInterface
{
use CanBeAutofocus;
use CanBeDisabled;
use CanBeMultiple;
use CanBeRequired;
use HasAriaLabel;
use HasAttributes;
use HasClass;
use HasGenerateField;
use HasGroup;
use HasId;
use HasItems;
use HasItemsAttributes;
use HasLabelCollection;
use HasName;
use HasPrefixCollection;
use HasPrompt;
use HasSize;
use HasStyle;
use HasSuffixCollection;
use HasTabindex;
use HasValue;
protected array $attributes = [];
protected function run(): string
{
$attributes = $this->attributes;
$multiple = false;
/** @psalm-var array<int, Stringable|scalar>|scalar|object|null $value */
$value = $this->getValue();
$items = match ($this->prompt) {
'' => PHP_EOL . Tag::widget()->content('Select an option')->tagName('option')->render(),
default => PHP_EOL . Tag::widget()
->content($this->prompt)
->tagName('option')
->value($this->promptValue)
->render(),
};
if (is_object($value)) {
throw new InvalidArgumentException('Select::class widget value can not be an object.');
}
if (array_key_exists('multiple', $attributes) && is_bool($attributes['multiple'])) {
$multiple = $attributes['multiple'];
}
if ($multiple === true && is_array($value) === false) {
throw new InvalidArgumentException('Select::class widget value must be an array when multiple is "true".');
}
if ($this->items !== []) {
$items .= PHP_EOL . implode(PHP_EOL, $this->renderItems($value)) . PHP_EOL;
}
unset($attributes['value']);
$selectTag = Tag::widget()
->attributes($attributes)
->content($items)
->id($this->id)
->prefix($this->prefix)
->prefixContainer($this->prefixContainer)
->prefixContainerAttributes($this->prefixContainerAttributes)
->prefixContainerTag($this->prefixContainerTag)
->suffix($this->suffix)
->suffixContainer($this->suffixContainer)
->suffixContainerAttributes($this->suffixContainerAttributes)
->suffixContainerTag($this->suffixContainerTag)
->tagName('select');
if ($this->isLabel === false || $this->label === '') {
return $selectTag->render();
}
return Label::widget()
->attributes($this->labelAttributes)
->content($this->label, PHP_EOL, $selectTag, PHP_EOL)
->for($this->labelFor)
->render();
}
/**
* @psalm-return string[]
*
* @psalm-param array<int, Stringable|scalar>|null|scalar $formValue
*/
private function renderItems(mixed $formValue): array
{
$formValue = match (get_debug_type($formValue)) {
'array' => $formValue,
default => [$formValue],
};
$items = [];
$itemsAttributes = $this->itemsAttributes;
/** @psalm-var string[]|string[][] $values */
$values = $this->items;
foreach ($values as $value => $content) {
if (is_array($content)) {
/** @psalm-var array $groupAttrs */
$groupAttrs = $this->groups[$value] ?? [];
$options = [];
foreach ($content as $v => $c) {
/** @psalm-var array[] $itemsAttributes */
$itemsAttributes[$v] ??= [];
$options[] = Tag::widget()
->attributes(
array_merge(
$itemsAttributes[$v],
[
'selected' => in_array($v, $formValue),
'value' => $v,
],
)
)
->content($c)
->tagName('option')
->render();
}
$items[] = Tag::widget()
->attributes($groupAttrs)
->content(implode(PHP_EOL, $options))
->tagName('optgroup')
->render();
} else {
/** @psalm-var array[] $itemsAttributes */
$itemsAttributes[$value] ??= [];
$items[] = Tag::widget()
->attributes(
array_merge(
$itemsAttributes[$value],
[
'selected' => in_array($value, $formValue),
'value' => $value,
],
)
)
->content($content)
->tagName('option')
->render();
}
}
return $items;
}
}