-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SelectTag.php
177 lines (154 loc) · 4.28 KB
/
SelectTag.php
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
<?php
declare(strict_types=1);
/**
* This file is part of the EaseHtml package
*
* https://github.com/VitexSoftware/php-ease-html
*
* (c) Vítězslav Dvořák <http://vitexsoftware.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Ease\Html;
/**
* @author Vítězslav Dvořák <info@vitexsoftware.cz>, Jana Viktorie Borbina <jana@borbina.com>
*
* Html Select.
*/
class SelectTag extends PairTag implements Input
{
/**
* Default item #.
*/
public string $defaultValue;
/**
* Automatic setting of the element name.
*/
public bool $setName = true;
/**
* @var array field values to use in select
*/
public array $items = [];
/**
* Html select box.
*
* @param string $name name
* @param array<string, string> $items items
* @param string $defaultValue default item id
* @param array<string, string> $properties select tag properties
*/
public function __construct(string $name, array $items = [], string $defaultValue = '', array $properties = [])
{
parent::__construct('select', $properties);
$this->defaultValue = $defaultValue;
$this->setTagName($name);
if ($items) {
$this->addItems($items);
}
}
/**
* Bulk insert items.
*
* @param array $items Select Items to add
*/
public function addItems(array $items): array
{
$added = [];
foreach ($items as $itemName => $itemValue) {
$added[$itemName] = $this->addItem(new OptionTag((string) $itemValue, (string) $itemName));
if ($this->defaultValue === (string) $itemName) {
$this->lastItem()->setDefault();
}
}
return $added;
}
/**
* Mockup of loading items.
*
* @return array
*/
public function loadItems()
{
return [];
}
/**
* Value setting.
*
* @param string $value the set value
*/
public function setValue(string $value): void
{
if (empty(trim($value)) === false) {
foreach ($this->pageParts as $option) {
if ($option->getValue() === $value) {
$option->setDefault();
} else {
$pos = array_search('selected', $option->tagProperties, true);
if (($pos !== false) && is_numeric($pos)) {
unset($option->tagProperties[$pos]);
}
}
}
} else {
if (empty($this->pageParts) === true) {
reset($this->pageParts);
$firstItem = &$this->pageParts[array_keys($this->pageParts)[0]];
$firstItem->setDefault();
}
}
}
/**
* Insert loaded items.
*/
#[\Override]
public function finalize(): void
{
if (!\count($this->pageParts)) {
// Uninitialised Select - so we load items
$this->addItems($this->loadItems());
}
parent::finalize();
}
/**
* Removes an item from the menu.
*
* @param string $itemID value key for removing from the list
*/
public function delItem($itemID): void
{
foreach ($this->pageParts as $optionId => $option) {
if ($option->getValue() === $itemID) {
unset($this->pageParts[$optionId]);
}
}
}
/**
* Disable menu item.
*
* @param int $itemID
*/
public function disableItem($itemID): void
{
foreach ($this->pageParts as $optionId => $option) {
if ($option->getValue() === $itemID) {
$this->pageParts[$optionId]->setTagProperties(['disabled']);
}
}
}
/**
* Get value of selected item.
*/
#[\Override]
public function getValue(): ?string
{
$value = null;
foreach ($this->pageParts as $option) {
$pos = array_search('selected', $option->tagProperties, true);
if (($pos !== false) && is_numeric($pos)) {
$value = $option->getValue();
}
}
return $value;
}
}