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
|
|
|
* @param bool $strict |
23
|
|
|
* @return static |
24
|
|
|
*/ |
25
|
|
|
public function multiple($strict = false) |
26
|
|
|
{ |
27
|
|
|
$element = clone $this; |
28
|
|
|
|
29
|
|
|
$element = $element->attribute('multiple'); |
30
|
|
|
|
31
|
|
|
$name = $element->getAttribute('name'); |
32
|
|
|
|
33
|
|
|
if ($name && ! Str::endsWith($name, '[]')) { |
34
|
|
|
$element = $element->name($name.'[]'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$element->applyValueToOptions($strict); |
38
|
|
|
|
39
|
|
|
return $element; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string|null $name |
44
|
|
|
* |
45
|
|
|
* @return static |
46
|
|
|
*/ |
47
|
|
|
public function name($name) |
48
|
|
|
{ |
49
|
|
|
return $this->attribute('name', $name); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param iterable $options |
54
|
|
|
* @param bool $strict |
55
|
|
|
* @return static |
56
|
|
|
*/ |
57
|
|
|
public function options($options, $strict = false) |
58
|
|
|
{ |
59
|
|
|
return $this->addChildren($options, function ($text, $value) use ($strict) { |
60
|
|
|
if (is_array($text)) { |
61
|
|
|
return $this->optgroup($value, $text, $strict); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->createOption($text, $value, $strict); |
65
|
|
|
}); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $label |
70
|
|
|
* @param iterable $options |
71
|
|
|
* @param bool $strict |
72
|
|
|
* |
73
|
|
|
* @return static |
74
|
|
|
*/ |
75
|
|
|
public function optgroup($label, $options, $strict = false) |
76
|
|
|
{ |
77
|
|
|
return Optgroup::create() |
78
|
|
|
->label($label) |
79
|
|
|
->addChildren($options, function ($text, $value) use ($strict) { |
80
|
|
|
return $this->createOption($text, $value, $strict); |
81
|
|
|
}); |
82
|
|
|
|
83
|
|
|
return $this->addChild($optgroup); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string|null $text |
88
|
|
|
* |
89
|
|
|
* @return static |
90
|
|
|
*/ |
91
|
|
|
public function placeholder($text) |
92
|
|
|
{ |
93
|
|
|
return $this->prependChild( |
94
|
|
|
$this->createOption($text, null) |
95
|
|
|
->selectedIf(! $this->hasSelection()) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return static |
101
|
|
|
*/ |
102
|
|
|
public function required() |
103
|
|
|
{ |
104
|
|
|
return $this->attribute('required'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string|iterable $value |
109
|
|
|
* @param bool $strict |
110
|
|
|
* |
111
|
|
|
* @return static |
112
|
|
|
*/ |
113
|
|
|
public function value($value = null, $strict = false) |
114
|
|
|
{ |
115
|
|
|
$element = clone $this; |
116
|
|
|
|
117
|
|
|
$element->value = $value; |
118
|
|
|
|
119
|
|
|
$element->applyValueToOptions($strict); |
120
|
|
|
|
121
|
|
|
return $element; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
protected function hasSelection() |
125
|
|
|
{ |
126
|
|
|
return $this->children->contains->hasAttribute('selected'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function createOption($text, $value = null, $strict = false) |
130
|
|
|
{ |
131
|
|
|
$selected = false; |
132
|
|
|
if (null !== $value) { |
133
|
|
|
$selected = $strict ? |
134
|
|
|
$value === $this->value : |
135
|
|
|
$value == $this->value; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return Option::create() |
139
|
|
|
->value($value) |
140
|
|
|
->text($text) |
141
|
|
|
->selectedIf($selected); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
protected function applyValueToOptions($strict = false) |
145
|
|
|
{ |
146
|
|
|
$value = Collection::make($this->value); |
147
|
|
|
|
148
|
|
|
if (! $this->hasAttribute('multiple')) { |
149
|
|
|
$value = $value->take(1); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$this->children = $this->applyValueToElements($value, $this->children, $strict); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
protected function applyValueToElements($value, Collection $children, $strict = false) |
156
|
|
|
{ |
157
|
|
|
return $children->map(function ($child) use ($strict, $value) { |
158
|
|
|
if ($child instanceof Optgroup) { |
159
|
|
|
return $child->setChildren($this->applyValueToElements($value, $child->children, $strict)); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if ($child instanceof Selectable) { |
163
|
|
|
$attribute = $child->getAttribute('value'); |
164
|
|
|
$contains = $strict ? |
165
|
|
|
$value->containsStrict($attribute) : |
166
|
|
|
$value->contains($attribute); |
167
|
|
|
|
168
|
|
|
return $child->selectedIf($contains); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $child; |
172
|
|
|
}); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: