|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebTheory\Saveyour\Field\Type; |
|
4
|
|
|
|
|
5
|
|
|
use WebTheory\Saveyour\Contracts\Field\FormFieldInterface; |
|
6
|
|
|
use WebTheory\Saveyour\Field\Abstracts\MultiValueSelectionTrait; |
|
7
|
|
|
use WebTheory\Saveyour\Field\Abstracts\RendersOptionsTrait; |
|
8
|
|
|
use WebTheory\Saveyour\Field\Element\OptGroup; |
|
9
|
|
|
use WebTheory\Saveyour\Field\Element\Option; |
|
10
|
|
|
use WebTheory\Saveyour\Field\Type\Abstracts\AbstractStandardFormControl; |
|
11
|
|
|
|
|
12
|
|
|
class Select extends AbstractStandardFormControl implements FormFieldInterface |
|
13
|
|
|
{ |
|
14
|
|
|
use MultiValueSelectionTrait; |
|
15
|
|
|
use RendersOptionsTrait { |
|
16
|
|
|
renderSelection as renderSelectionFromProvider; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
protected $value = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var OptGroup[] |
|
23
|
|
|
*/ |
|
24
|
|
|
protected array $groups = []; |
|
25
|
|
|
|
|
26
|
|
|
protected bool $multiple = false; |
|
27
|
|
|
|
|
28
|
|
|
protected ?int $size = null; |
|
29
|
|
|
|
|
30
|
|
|
public function getGroups() |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->groups; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function setGroups(OptGroup ...$optGroups) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->groups = $optGroups; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function addGroup(OptGroup $optGroup) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->groups[] = $optGroup; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get the value of multiple |
|
47
|
|
|
* |
|
48
|
|
|
* @return bool |
|
49
|
|
|
*/ |
|
50
|
|
|
public function isMultiple(): bool |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->multiple; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Set the value of multiple |
|
57
|
|
|
* |
|
58
|
|
|
* @param bool $multiple |
|
59
|
|
|
* |
|
60
|
|
|
* @return $this |
|
61
|
|
|
*/ |
|
62
|
|
|
public function setMultiple(bool $multiple): Select |
|
63
|
|
|
{ |
|
64
|
|
|
$this->multiple = $multiple; |
|
65
|
|
|
|
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get the value of size |
|
71
|
|
|
* |
|
72
|
|
|
* @return int |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getSize(): int |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->size; |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Set the value of size |
|
81
|
|
|
* |
|
82
|
|
|
* @param int $size |
|
83
|
|
|
* |
|
84
|
|
|
* @return $this |
|
85
|
|
|
*/ |
|
86
|
|
|
public function setSize(int $size): Select |
|
87
|
|
|
{ |
|
88
|
|
|
$this->size = $size; |
|
89
|
|
|
|
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return $this |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function resolveAttributes(): Select |
|
97
|
|
|
{ |
|
98
|
|
|
parent::resolveAttributes() |
|
99
|
|
|
->addAttribute('name', $this->resolveNameAttribute()) |
|
100
|
|
|
->addAttribute('multiple', $this->multiple) |
|
101
|
|
|
->addAttribute('size', $this->size); |
|
102
|
|
|
|
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
protected function resolveNameAttribute() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->name . ($this->multiple ? '[]' : ''); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
protected function renderHtmlMarkup(): string |
|
112
|
|
|
{ |
|
113
|
|
|
$html = ''; |
|
114
|
|
|
|
|
115
|
|
|
if ($this->multiple) { |
|
116
|
|
|
$html .= $this->createClearControlField() |
|
117
|
|
|
->setName($this->resolveNameAttribute()) |
|
118
|
|
|
->setValue(''); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$html .= $this->tag('select', $this->attributes, $this->renderOptions()); |
|
122
|
|
|
|
|
123
|
|
|
return $html; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
protected function renderOptions() |
|
127
|
|
|
{ |
|
128
|
|
|
$html = ''; |
|
129
|
|
|
|
|
130
|
|
|
if (!empty($this->placeholder)) { |
|
131
|
|
|
$html .= $this->createPlaceholder(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$html .= $this->renderSelection(); |
|
135
|
|
|
|
|
136
|
|
|
return $html; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
protected function createPlaceholder(): Option |
|
140
|
|
|
{ |
|
141
|
|
|
return new Option($this->placeholder, ''); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
protected function renderSelection(): string |
|
145
|
|
|
{ |
|
146
|
|
|
return empty($this->groups) |
|
147
|
|
|
? $this->renderSelectionFromProvider() |
|
148
|
|
|
: $this->renderSelectionFromOptGroups(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
protected function renderSelectionFromOptGroups(): string |
|
152
|
|
|
{ |
|
153
|
|
|
$html = ''; |
|
154
|
|
|
|
|
155
|
|
|
foreach ($this->groups as $optgroup) { |
|
156
|
|
|
$html .= $optgroup->setValue($this->value); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $html; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|