@@ -35,31 +35,36 @@ public function __construct(Collection $collection, string|Closure|null $label =
35
35
/**
36
36
* Check if the item should be selected
37
37
* @param object $item
38
- * @param int|null $index
38
+ * @param int|string| null $index
39
39
* @return bool
40
40
*/
41
- private function _shouldSelect (object $ item , int |null $ index = null ): bool
41
+ private function _shouldSelect (mixed $ item , int | string |null $ index = null ): bool
42
42
{
43
- $ optionValue = ($ this ->_value instanceof Closure) ? call_user_func ($ this ->_value , $ item , $ index ) : $ item ->{$ this ->_value } ?? "" ;
44
43
if ($ this ->_selected instanceof Closure) {
45
- if (call_user_func ($ this ->_selected , $ item ) === true ) {
46
- return true ;
47
- }
48
- } else if (is_object ($ this ->_selected )) {
49
- if ((string )$ this ->_selected ->{$ this ->_value } === (string )$ optionValue ) {
50
- return true ;
44
+ return (bool )call_user_func ($ this ->_selected , $ item , $ index );
45
+ }
46
+
47
+ if ($ this ->_value instanceof Closure) {
48
+ $ optionValue = call_user_func ($ this ->_value , $ item , $ index );
49
+ } else {
50
+ $ optionValue = (is_object ($ item ) ? ($ item ->{$ this ->_value } ?? "" ) : $ item );
51
+ if (is_array ($ item )) {
52
+ $ optionValue = $ item [$ this ->_value ] ?? reset ($ item );
51
53
}
52
- } else if (is_array ($ this ->_selected )) {
54
+ }
55
+ if (is_object ($ this ->_selected )) {
56
+ return ((string )$ this ->_selected ->{$ this ->_value } === (string )$ optionValue );
57
+ }
58
+
59
+ if (is_array ($ this ->_selected )) {
53
60
foreach ($ this ->_selected as $ selectedItem ) {
54
61
if (is_object ($ selectedItem )) {
55
- if ((string )$ selectedItem ->{$ this ->_value } === (string )$ optionValue ) {
56
- return true ;
57
- }
58
- } else if (is_array ($ selectedItem )) {
59
- if (array_key_exists ($ this ->_value , $ selectedItem ) && (string )$ selectedItem [$ this ->_value ] === (string )$ optionValue ) {
60
- return true ;
61
- }
62
- } else if ((string )$ selectedItem === (string )$ optionValue ) {
62
+ return ((string )$ selectedItem ->{$ this ->_value } === (string )$ optionValue );
63
+ }
64
+ if (is_array ($ selectedItem )) {
65
+ return (array_key_exists ($ this ->_value , $ selectedItem ) && (string )$ selectedItem [$ this ->_value ] === (string )$ optionValue );
66
+ }
67
+ if ((string )$ selectedItem === (string )$ optionValue ) {
63
68
return true ;
64
69
}
65
70
}
@@ -71,32 +76,38 @@ private function _shouldSelect(object $item, int|null $index = null): bool
71
76
72
77
/**
73
78
* Check if the item should be selected
74
- * @param object $item
75
- * @param int|null $index
79
+ * @param mixed $item
80
+ * @param int|string| null $index
76
81
* @return bool
77
82
*/
78
- private function _shouldDisable (object $ item , int |null $ index = null ): bool
83
+ private function _shouldDisable (mixed $ item , int | string |null $ index = null ): bool
79
84
{
80
- $ lineValue = $ item ->{$ this ->_value } ?? "" ;
81
- if (is_callable ($ this ->_disabled )) {
82
- if (call_user_func ($ this ->_disabled , $ item ) === true ) {
83
- return true ;
84
- }
85
- } else if (is_object ($ this ->_disabled )) {
86
- if ((string )$ this ->_disabled ->{$ this ->_value } === (string )$ lineValue ) {
87
- return true ;
85
+ if ($ this ->_disabled instanceof Closure) {
86
+ return (bool )call_user_func ($ this ->_disabled , $ item , $ index );
87
+ }
88
+
89
+ if ($ this ->_value instanceof Closure) {
90
+ $ lineValue = call_user_func ($ this ->_value , $ item , $ index );
91
+ } else {
92
+ $ lineValue = (is_object ($ item ) ? ($ item ->{$ this ->_value } ?? "" ) : $ item );
93
+ if (is_array ($ item )){
94
+ $ lineValue = $ item [$ this ->_value ] ??reset ($ item );
88
95
}
89
- } else if (is_array ($ this ->_disabled )) {
96
+ }
97
+
98
+ if (is_object ($ this ->_disabled )) {
99
+ return ((string )$ this ->_disabled ->{$ this ->_value } === (string )$ lineValue );
100
+ }
101
+
102
+ if (is_array ($ this ->_disabled )) {
90
103
foreach ($ this ->_disabled as $ disabledItem ) {
91
104
if (is_object ($ disabledItem )) {
92
- if ((string )$ disabledItem ->{$ this ->_value } === (string )$ lineValue ) {
93
- return true ;
94
- }
95
- } else if (is_array ($ disabledItem )) {
96
- if (array_key_exists ($ this ->_value , $ disabledItem ) && (string )$ disabledItem [$ this ->_value ] === (string )$ lineValue ) {
97
- return true ;
98
- }
99
- } else if ((string )$ disabledItem === (string )$ lineValue ) {
105
+ return ((string )$ disabledItem ->{$ this ->_value } === (string )$ lineValue );
106
+ }
107
+ if (is_array ($ disabledItem )) {
108
+ return (array_key_exists ($ this ->_value , $ disabledItem ) && (string )$ disabledItem [$ this ->_value ] === (string )$ lineValue );
109
+ }
110
+ if ((string )$ disabledItem === (string )$ lineValue ) {
100
111
return true ;
101
112
}
102
113
}
@@ -126,9 +137,10 @@ private function _getDataAttributes(mixed $item, int|null $index = null): array
126
137
/**
127
138
* Generate select options from a Collection instance
128
139
* @param Collection $collection
140
+ * @param int $lastIndex
129
141
* @return string
130
142
*/
131
- private function _generateOptions (Collection $ collection ): string
143
+ private function _generateOptions (Collection $ collection, int $ lastIndex = 0 ): string
132
144
{
133
145
$ html = "" ;
134
146
foreach ($ collection as $ index => $ item ) {
@@ -137,8 +149,25 @@ private function _generateOptions(Collection $collection): string
137
149
$ html .= $ this ->_generateOptions ($ item );
138
150
$ html .= "</optgroup> " ;
139
151
} else {
140
- $ optionLabel = ($ this ->_label instanceof Closure) ? call_user_func ($ this ->_label , $ item , $ index ) : $ item ->{$ this ->_label } ?? "N/A " ;
141
- $ optionValue = ($ this ->_value instanceof Closure) ? call_user_func ($ this ->_value , $ item , $ index ) : $ item ->{$ this ->_value } ?? "" ;
152
+ if ($ this ->_label instanceof Closure) {
153
+ $ optionLabel = call_user_func ($ this ->_label , $ item , $ index );
154
+ } else {
155
+ $ optionLabel = is_object ($ item ) ? ($ item ->{$ this ->_label } ?? "N/A " ) : ($ item );
156
+ if (is_array ($ item )) {
157
+ $ optionLabel = $ item [$ this ->_label ] ?? array_keys ($ item )[0 ];
158
+ }
159
+ }
160
+ if ($ this ->_value instanceof Closure) {
161
+ $ optionValue = call_user_func ($ this ->_value , $ item , $ index );
162
+ } else {
163
+ $ optionValue = is_object ($ item ) ? ($ item ->{$ this ->_value } ?? "" ) : $ item ;
164
+ if (is_array ($ item )) {
165
+ $ optionValue = $ item [$ this ->_value ] ?? reset ($ item );
166
+ }
167
+ if (is_string ($ index ) && is_string ($ item )) {
168
+ $ optionValue = $ index ;
169
+ }
170
+ }
142
171
$ html .= "<option value= \"{$ optionValue }\"" ;
143
172
if ($ this ->_shouldSelect ($ item , $ index )) {
144
173
$ html .= " selected " ;
@@ -206,9 +235,22 @@ public function toSelectOptions(): string
206
235
public function toSelectItems (): Collection
207
236
{
208
237
return $ this ->_collection ->map (function ($ item , $ index ) {
238
+ if ($ this ->_label instanceof Closure) {
239
+ $ optionLabel = call_user_func ($ this ->_label , $ item , $ index );
240
+ } else {
241
+ $ optionLabel = is_object ($ item ) ? ($ item ->{$ this ->_label } ?? "N/A " ) : ($ item );
242
+ }
243
+ if ($ this ->_value instanceof Closure) {
244
+ $ optionValue = call_user_func ($ this ->_value , $ item , $ index );
245
+ } else {
246
+ $ optionValue = is_object ($ item ) ? ($ item ->{$ this ->_value } ?? "" ) : $ item ;
247
+ if (is_string ($ index ) && is_string ($ item )) {
248
+ $ optionValue = $ index ;
249
+ }
250
+ }
209
251
return [
210
- 'value ' => $ item ->{ $ this -> _value } ?? "" ,
211
- 'label ' => $ index ,
252
+ 'value ' => $ optionValue ,
253
+ 'label ' => $ optionLabel ,
212
254
'isSelected ' => $ this ->_shouldSelect ($ item , $ index ),
213
255
'isDisabled ' => $ this ->_shouldDisable ($ item , $ index ),
214
256
'data ' => $ this ->_getDataAttributes ($ item , $ index ),
0 commit comments