1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Html\Elements; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Spatie\Html\Selectable; |
7
|
|
|
use Spatie\Html\BaseElement; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
|
10
|
|
|
class Select extends BaseElement |
11
|
|
|
{ |
12
|
|
|
/** @var string */ |
13
|
|
|
protected $tag = 'select'; |
14
|
|
|
|
15
|
|
|
/** @var array */ |
16
|
|
|
protected $options = []; |
17
|
|
|
|
18
|
|
|
/** @var string|iterable */ |
19
|
|
|
protected $value = ''; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return static |
23
|
|
|
*/ |
24
|
|
|
public function multiple() |
25
|
|
|
{ |
26
|
|
|
$element = clone $this; |
27
|
|
|
|
28
|
|
|
$element = $element->attribute('multiple'); |
29
|
|
|
|
30
|
|
|
$name = $element->getAttribute('name'); |
31
|
|
|
|
32
|
|
|
if ($name && ! Str::endsWith($name, '[]')) { |
33
|
|
|
$element = $element->name($name.'[]'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$element->applyValueToOptions(); |
37
|
|
|
|
38
|
|
|
return $element; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string|null $name |
43
|
|
|
* |
44
|
|
|
* @return static |
45
|
|
|
*/ |
46
|
|
|
public function name($name) |
47
|
|
|
{ |
48
|
|
|
return $this->attribute('name', $name); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param iterable $options |
53
|
|
|
* |
54
|
|
|
* @return static |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function options($options) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
return $this->addChildren($options, function ($text, $value) { |
59
|
|
|
if (is_array($text)) { |
60
|
|
|
return $this->optgroup($value, $text); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return Option::create() |
64
|
|
|
->value($value) |
65
|
|
|
->text($text) |
66
|
|
|
->selectedIf($value === $this->value); |
67
|
|
|
}); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $label |
72
|
|
|
* @param iterable $options |
73
|
|
|
* |
74
|
|
|
* @return static |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function optgroup($label, $options) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
return Optgroup::create() |
79
|
|
|
->label($label) |
80
|
|
|
->addChildren($options, function ($text, $value) { |
81
|
|
|
return Option::create() |
82
|
|
|
->value($value) |
83
|
|
|
->text($text) |
84
|
|
|
->selectedIf($value === $this->value); |
85
|
|
|
}); |
86
|
|
|
|
87
|
|
|
return $this->addChild($optgroup); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string|null $text |
92
|
|
|
* |
93
|
|
|
* @return static |
94
|
|
|
*/ |
95
|
|
|
public function placeholder($text) |
96
|
|
|
{ |
97
|
|
|
return $this->prependChild( |
98
|
|
|
Option::create() |
99
|
|
|
->value(null) |
100
|
|
|
->text($text) |
101
|
|
|
->selectedIf(! $this->hasSelection()) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string|null $text |
107
|
|
|
* |
108
|
|
|
* @return static |
109
|
|
|
*/ |
110
|
|
|
public function disabledPlaceholder($text) |
111
|
|
|
{ |
112
|
|
|
return $this->prependChild( |
113
|
|
|
Option::create() |
114
|
|
|
->value(null) |
115
|
|
|
->text($text) |
116
|
|
|
->disabledIf(! $this->hasDisabled()) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return static |
122
|
|
|
*/ |
123
|
|
|
public function required() |
124
|
|
|
{ |
125
|
|
|
return $this->attribute('required'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string|iterable $value |
130
|
|
|
* |
131
|
|
|
* @return static |
132
|
|
|
*/ |
133
|
|
|
public function value($value = null) |
134
|
|
|
{ |
135
|
|
|
$element = clone $this; |
136
|
|
|
|
137
|
|
|
$element->value = $value; |
138
|
|
|
|
139
|
|
|
$element->applyValueToOptions(); |
140
|
|
|
|
141
|
|
|
return $element; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
protected function hasSelection() |
145
|
|
|
{ |
146
|
|
|
return $this->children->contains->hasAttribute('selected'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
protected function hasDisabled() |
150
|
|
|
{ |
151
|
|
|
return $this->children->contains->hasAttribute('disabled'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
protected function applyValueToOptions() |
155
|
|
|
{ |
156
|
|
|
$value = Collection::make($this->value); |
157
|
|
|
|
158
|
|
|
if (! $this->hasAttribute('multiple')) { |
159
|
|
|
$value = $value->take(1); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$this->children = $this->applyValueToElements($value, $this->children); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
protected function applyValueToElements($value, Collection $children) |
166
|
|
|
{ |
167
|
|
|
return $children->map(function ($child) use ($value) { |
168
|
|
|
if ($child instanceof Optgroup) { |
169
|
|
|
return $child->setChildren($this->applyValueToElements($value, $child->children)); |
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if ($child instanceof Selectable) { |
173
|
|
|
return $child->selectedIf($value->contains($child->getAttribute('value'))); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $child; |
177
|
|
|
}); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.