|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_forms\Controls; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class SelectOptgroup |
|
8
|
|
|
* @package kalanis\kw_forms\Controls |
|
9
|
|
|
* Form element for selection - option group |
|
10
|
|
|
*/ |
|
11
|
|
|
class SelectOptgroup extends AControl |
|
12
|
|
|
{ |
|
13
|
|
|
private static int $uniqid = 0; |
|
14
|
|
|
protected string $templateLabel = ''; |
|
15
|
|
|
protected string $templateInput = '<optgroup label="%1$s">%3$s</optgroup>'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Create element Optgroup |
|
19
|
|
|
* @param string $alias |
|
20
|
|
|
* @param string $label |
|
21
|
|
|
* @param iterable<string|int, string|SelectOption> $children |
|
22
|
|
|
* @return $this |
|
23
|
|
|
*/ |
|
24
|
2 |
|
public function set(string $alias, string $label = '', iterable $children = []): self |
|
25
|
|
|
{ |
|
26
|
2 |
|
$this->setEntry(sprintf('%s_%s', $alias, self::$uniqid), '', $label); |
|
27
|
2 |
|
foreach ($children as $childAlias => $child) { |
|
28
|
2 |
|
if ($child instanceof SelectOption) { |
|
29
|
1 |
|
$this->addChild($child, $childAlias); |
|
30
|
|
|
} else { |
|
31
|
2 |
|
$this->addOption(strval($childAlias), $childAlias, strval($child)); |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
2 |
|
self::$uniqid++; |
|
35
|
2 |
|
return $this; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $alias |
|
40
|
|
|
* @param string|int|float|bool|null $value |
|
41
|
|
|
* @param string $label |
|
42
|
|
|
* @return SelectOption |
|
43
|
|
|
*/ |
|
44
|
2 |
|
public function addOption(string $alias, $value, string $label = ''): SelectOption |
|
45
|
|
|
{ |
|
46
|
2 |
|
$option = new SelectOption(); |
|
47
|
2 |
|
$option->setEntry($alias, $value, $label); |
|
48
|
2 |
|
$this->addChild($option, $alias); |
|
49
|
2 |
|
return $option; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
public function setValue($value): void |
|
53
|
|
|
{ |
|
54
|
2 |
|
foreach ($this->children as $child) { |
|
55
|
2 |
|
if ($child instanceof SelectOption) { |
|
56
|
2 |
|
$child->setValue($value); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
2 |
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
public function getValue() |
|
62
|
|
|
{ |
|
63
|
2 |
|
foreach ($this->children as $child) { |
|
64
|
2 |
|
if ($child instanceof SelectOption) { |
|
65
|
2 |
|
if (!empty($child->getValue())) { |
|
66
|
2 |
|
return $child->getValue(); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
2 |
|
return ''; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
public function renderInput($attributes = null): string |
|
74
|
|
|
{ |
|
75
|
2 |
|
return $this->wrapIt(sprintf($this->templateInput, $this->escaped(strval($this->getLabel())), $this->renderAttributes(), $this->renderChildren()), $this->wrappersInput); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
public function renderErrors(array $errors): string |
|
79
|
|
|
{ |
|
80
|
2 |
|
return ''; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|