|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Form\Element; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Constant\Html5; |
|
8
|
|
|
use AbterPhp\Framework\Form\Component\Option; |
|
9
|
|
|
use AbterPhp\Framework\Html\Attribute; |
|
10
|
|
|
use AbterPhp\Framework\Html\Attributes; |
|
11
|
|
|
use AbterPhp\Framework\Html\Component; |
|
12
|
|
|
|
|
13
|
|
|
class Select extends Component implements IElement |
|
14
|
|
|
{ |
|
15
|
|
|
protected const DEFAULT_TAG = Html5::TAG_SELECT; |
|
16
|
|
|
|
|
17
|
|
|
/** @var Option[] */ |
|
18
|
|
|
protected array $nodes = []; |
|
19
|
|
|
|
|
20
|
|
|
protected string $nodeClass = Option::class; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Select constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $inputId |
|
26
|
|
|
* @param string $name |
|
27
|
|
|
* @param string[] $intents |
|
28
|
|
|
* @param Attributes|null $attributes |
|
29
|
|
|
* @param string|null $tag |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct( |
|
32
|
|
|
string $inputId, |
|
33
|
|
|
string $name, |
|
34
|
|
|
array $intents = [], |
|
35
|
|
|
?Attributes $attributes = null, |
|
36
|
|
|
?string $tag = null |
|
37
|
|
|
) { |
|
38
|
|
|
$attributes = $attributes ?? new Attributes(); |
|
39
|
|
|
if ($inputId) { |
|
40
|
|
|
$attributes->replaceItem(new Attribute(Html5::ATTR_ID, $inputId)); |
|
41
|
|
|
} |
|
42
|
|
|
$attributes->replaceItem(new Attribute(Html5::ATTR_NAME, $name)); |
|
43
|
|
|
|
|
44
|
|
|
parent::__construct(null, $intents, $attributes, $tag); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string|string[] $value |
|
49
|
|
|
* |
|
50
|
|
|
* @return $this |
|
51
|
|
|
*/ |
|
52
|
|
|
public function setValue($value): IElement |
|
53
|
|
|
{ |
|
54
|
|
|
if (!is_string($value)) { |
|
55
|
|
|
throw new \InvalidArgumentException(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $this->setValueInner([$value]); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return string[] |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getValue() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->getAttribute(Html5::ATTR_VALUE)->getValues(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string[] $values |
|
71
|
|
|
* |
|
72
|
|
|
* @return $this |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function setValueInner(array $values): IElement |
|
75
|
|
|
{ |
|
76
|
|
|
foreach ($this->nodes as $option) { |
|
77
|
|
|
if (in_array($option->getValue(), $values, true)) { |
|
78
|
|
|
$option->getAttributes()->replaceItem(new Attribute(Html5::ATTR_SELECTED)); |
|
79
|
|
|
} elseif ($option->getAttributes()->hasItem(Html5::ATTR_SELECTED)) { |
|
80
|
|
|
$option->getAttributes()->remove(Html5::ATTR_SELECTED); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getName(): string |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->forceGetAttribute(Html5::ATTR_NAME)->getValue(); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|