99
1010class Select implements Stringable
1111{
12+ /**
13+ * @var string[]
14+ */
1215 private array $ wrapperProperties = [];
1316
1417 private bool $ dropdown = true ;
1518
16- private bool $ radio = false ;
17-
1819 private bool $ checkbox = false ;
1920
21+ /**
22+ * @param array<string, string> $options
23+ * @param string|string[] $selected
24+ */
2025 public static function create (
2126 string |Stringable $ label ,
2227 string |Stringable $ name ,
@@ -26,6 +31,10 @@ public static function create(
2631 return new self ($ label , $ name , $ options , $ selected );
2732 }
2833
34+ /**
35+ * @param array<string, string> $options
36+ * @param string|string[] $selected
37+ */
2938 final private function __construct (
3039 private readonly string |Stringable $ label ,
3140 private readonly string |Stringable $ name ,
@@ -43,16 +52,12 @@ public function wrapperProps(string ...$propperties): self
4352 public function dropdown (): self
4453 {
4554 $ this ->dropdown = true ;
46-
47- $ this ->radio = false ;
4855 $ this ->checkbox = false ;
4956 return $ this ;
5057 }
5158
5259 public function radio (): self
5360 {
54- $ this ->radio = true ;
55-
5661 $ this ->dropdown = false ;
5762 $ this ->checkbox = false ;
5863 return $ this ;
@@ -61,23 +66,26 @@ public function radio(): self
6166 public function checkbox (): self
6267 {
6368 $ this ->checkbox = true ;
64-
65- $ this ->radio = false ;
6669 $ this ->dropdown = false ;
6770 return $ this ;
6871 }
6972
7073 private function hasSelected (): bool
7174 {
72- if (is_string ($ this ->selected ) and strlen ($ this ->selected ) > 0 ) {
75+ $ selected = $ this ->selected ();
76+ if (is_string ($ selected ) and strlen ($ selected ) > 0 ) {
7377 return true ;
7478
75- } elseif (count ($ this -> selected ) > 0 ) {
79+ } elseif (is_array ( $ selected ) and count ($ selected ) > 0 ) {
7680 return true ;
81+
7782 }
7883 return false ;
7984 }
8085
86+ /**
87+ * @return string|string[]
88+ */
8189 private function selected (): string |array
8290 {
8391 if ($ this ->checkbox ) {
@@ -87,7 +95,7 @@ private function selected(): string|array
8795 return [$ this ->selected ];
8896 }
8997
90- if (is_array ($ this ->selected )) {
98+ if (is_array ($ this ->selected ) and count ( $ this -> selected ) > 0 ) {
9199 return $ this ->selected [0 ];
92100 }
93101 return $ this ->selected ;
@@ -99,7 +107,7 @@ private function isSelected(string $value): bool
99107 return false ;
100108 }
101109
102- if ($ this ->checkbox ) {
110+ if (is_array ( $ this ->selected ()) ) {
103111 return in_array ($ value , $ this ->selected ());
104112 }
105113 return $ value === $ this ->selected ();
0 commit comments