3
3
namespace RingleSoft \LaravelSelectable ;
4
4
5
5
use Closure ;
6
- use Exception ;
7
6
use Illuminate \Support \Collection ;
8
7
use ReflectionException ;
9
8
use ReflectionFunction ;
@@ -52,11 +51,11 @@ private function _shouldSelect(mixed $item, int|string|null $index = null): bool
52
51
return (bool )call_user_func ($ this ->_selected , $ item , $ index );
53
52
}
54
53
55
- if ($ this ->_value instanceof Closure) {
54
+ if ($ this ->_value instanceof Closure) {
56
55
$ optionValue = call_user_func ($ this ->_value , $ item , $ index );
57
56
} else {
58
57
$ optionValue = (is_object ($ item ) ? ($ item ->{$ this ->_value } ?? "" ) : $ item );
59
- if (is_array ($ item )) {
58
+ if (is_array ($ item )) {
60
59
$ optionValue = $ item [$ this ->_value ] ?? reset ($ item );
61
60
}
62
61
}
@@ -95,15 +94,11 @@ private function _shouldDisable(mixed $item, int|string|null $index = null): boo
95
94
}
96
95
97
96
if ($ this ->_value instanceof Closure) {
98
- try {
99
- $ lineValue = call_user_func ($ this ->_value , $ item , $ index );
100
- } catch (Exception $ e ) {
101
- Logger::error ($ e ->getMessage ());
102
- }
97
+ $ lineValue = call_user_func ($ this ->_value , $ item , $ index );
103
98
} else {
104
99
$ lineValue = (is_object ($ item ) ? ($ item ->{$ this ->_value } ?? "" ) : $ item );
105
- if (is_array ($ item )){
106
- $ lineValue = $ item [$ this ->_value ] ??reset ($ item );
100
+ if (is_array ($ item )) {
101
+ $ lineValue = $ item [$ this ->_value ] ?? reset ($ item );
107
102
}
108
103
}
109
104
@@ -142,12 +137,8 @@ private function _getDataAttributes(mixed $item, int|null $index = null): array
142
137
foreach ($ this ->_dataAttributes as $ attributeItem ) {
143
138
$ attribute = $ attributeItem ['attribute ' ];
144
139
$ value = $ attributeItem ['value ' ];
145
- try {
146
- $ index = (($ attribute instanceof Closure) ? $ attribute ($ item , $ index ) : $ attribute );
147
- $ dataAttributes [(string )$ index ] = ($ value instanceof Closure) ? $ value ($ item , $ index ) : ($ item ->{$ value } ?? '' );
148
- } catch (Exception $ e ) {
149
- Logger::error ($ e ->getMessage ());
150
- }
140
+ $ index = (($ attribute instanceof Closure) ? $ attribute ($ item , $ index ) : $ attribute );
141
+ $ dataAttributes [(string )$ index ] = ($ value instanceof Closure) ? $ value ($ item , $ index ) : ($ item ->{$ value } ?? '' );
151
142
}
152
143
}
153
144
return $ dataAttributes ;
@@ -172,15 +163,15 @@ private function _generateOptions(Collection $collection, int $lastIndex = 0): s
172
163
$ optionLabel = call_user_func ($ this ->_label , $ item , $ index );
173
164
} else {
174
165
$ optionLabel = is_object ($ item ) ? ($ item ->{$ this ->_label } ?? "N/A " ) : ($ item );
175
- if (is_array ($ item )) {
166
+ if (is_array ($ item )) {
176
167
$ optionLabel = $ item [$ this ->_label ] ?? array_keys ($ item )[0 ];
177
168
}
178
169
}
179
170
if ($ this ->_value instanceof Closure) {
180
171
$ optionValue = call_user_func ($ this ->_value , $ item , $ index );
181
172
} else {
182
173
$ optionValue = is_object ($ item ) ? ($ item ->{$ this ->_value } ?? "" ) : $ item ;
183
- if (is_array ($ item )) {
174
+ if (is_array ($ item )) {
184
175
$ optionValue = $ item [$ this ->_value ] ?? reset ($ item );
185
176
}
186
177
if (is_string ($ index ) && is_string ($ item )) {
@@ -189,12 +180,8 @@ private function _generateOptions(Collection $collection, int $lastIndex = 0): s
189
180
}
190
181
// Prepare Option
191
182
$ html .= "<option value= \"{$ optionValue }\"" ;
192
- if ($ this ->_id instanceof Closure) {
193
- try {
194
- $ html .= " id= \"" . ((string )call_user_func ($ this ->_id , $ item , $ index )) . "\"" ;
195
- } catch (Exception $ e ) {
196
- Logger::error ($ e ->getMessage ());
197
- }
183
+ if ($ this ->_id instanceof Closure) {
184
+ $ html .= " id= \"" . ((string )call_user_func ($ this ->_id , $ item , $ index )) . "\"" ;
198
185
}
199
186
if ($ this ->_shouldSelect ($ item , $ index )) {
200
187
$ html .= " selected " ;
@@ -210,11 +197,7 @@ private function _generateOptions(Collection $collection, int $lastIndex = 0): s
210
197
if (count ($ this ->_classes ) > 0 ) {
211
198
$ html .= " class= \"" ;
212
199
foreach ($ this ->_classes as $ class ) {
213
- try {
214
- $ html .= (($ class instanceof Closure) ? ((string )$ class ($ item , $ index )) : $ class ) . " " ;
215
- } catch (Exception $ e ) {
216
- Logger::error ($ e ->getMessage ());
217
- }
200
+ $ html .= (($ class instanceof Closure) ? ((string )$ class ($ item , $ index )) : $ class ) . " " ;
218
201
}
219
202
$ html = rtrim ($ html ) . "\"" ;
220
203
}
@@ -271,20 +254,12 @@ public function toSelectItems(): Collection
271
254
{
272
255
return $ this ->_collection ->map (function ($ item , $ index ) {
273
256
if ($ this ->_label instanceof Closure) {
274
- try {
275
- $ optionLabel = call_user_func ($ this ->_label , $ item , $ index );
276
- } catch (Exception $ e ) {
277
- Logger::error ($ e ->getMessage ());
278
- }
257
+ $ optionLabel = call_user_func ($ this ->_label , $ item , $ index );
279
258
} else {
280
259
$ optionLabel = is_object ($ item ) ? ($ item ->{$ this ->_label } ?? "N/A " ) : ($ item );
281
260
}
282
261
if ($ this ->_value instanceof Closure) {
283
- try {
284
- $ optionValue = call_user_func ($ this ->_value , $ item , $ index );
285
- } catch (Exception $ e ) {
286
- Logger::error ($ e ->getMessage ());
287
- }
262
+ $ optionValue = call_user_func ($ this ->_value , $ item , $ index );
288
263
} else {
289
264
$ optionValue = is_object ($ item ) ? ($ item ->{$ this ->_value } ?? "" ) : $ item ;
290
265
if (is_string ($ index ) && is_string ($ item )) {
@@ -326,7 +301,7 @@ public function withValue(string|Closure $value): self
326
301
327
302
/**
328
303
* Specify the selected values for the selectable items
329
- * @param mixed $selected
304
+ * @param mixed|Closure(mixed, int|string|null): bool $selected
330
305
* @return $this
331
306
*/
332
307
public function withSelected (mixed $ selected ): self
@@ -351,7 +326,7 @@ public function withDisabled(mixed $disabled): self
351
326
* @param string|Closure(mixed, int):string $attribute Data attribute name
352
327
* * @param string|Closure(mixed, int):mixed $value Data attribute value
353
328
* * @return $this
354
- */
329
+ */
355
330
public function withDataAttribute (string |Closure $ attribute , string |Closure $ value ): self
356
331
{
357
332
$ this ->_dataAttributes [] = ['attribute ' => $ attribute , 'value ' => $ value ];
@@ -421,34 +396,32 @@ public function __call(string $name, array $arguments)
421
396
422
397
423
398
/**
424
- * Internal method to validate the callable.
399
+ * Experimental (Currently not used)
400
+ * Internal method to validate the user provided callable.
425
401
* @param callable $callable
426
402
* @param string $returnType
427
403
* @param null $argumentName
428
404
* @return void
429
405
* @throws InvalidCallableException
430
- * @throws ReflectionException
431
406
*/
432
407
private function validateCallable (callable $ callable , string $ returnType = 'string ' , $ argumentName = null ): void
433
- {
408
+ {
409
+ try {
434
410
$ reflection = new ReflectionFunction ($ callable );
435
- $ parameters = $ reflection ->getParameters ();
436
-
437
- // Ensure max 2 parameters
438
- if (count ($ parameters ) > 2 ) {
439
- throw new InvalidCallableException ("The callable {$ argumentName } must accept maximum of 2 parameters. " );
440
- }
441
-
442
- // Ensure second parameter is `int`
443
- if ($ parameters [1 ] && $ parameters [1 ]->hasType () && $ parameters [1 ]->getType ()?->getName() !== 'int ' ) {
444
- throw new InvalidCallableException ("The second parameter of the callable {$ argumentName } must be of type `int`. " );
445
- }
446
-
447
- // Ensure the return type is `string`
448
- if ($ reflection ->hasReturnType () && $ reflection ->getReturnType ()?->getName() !== $ returnType ) {
449
- throw new InvalidCallableException ("The callable {$ argumentName } must return a string. " );
450
- }
451
-
411
+ } catch (ReflectionException $ e ) {
412
+ Logger::error ($ e ->getMessage ());
413
+ return ;
414
+ }
415
+ $ parameters = $ reflection ->getParameters ();
416
+ if (count ($ parameters ) > 2 ) {
417
+ throw new InvalidCallableException ("The callable {$ argumentName } must accept maximum of 2 parameters. " );
418
+ }
419
+ if (count ($ parameters ) === 2 && $ parameters [1 ]->hasType () && $ parameters [1 ]->getType ()?->getName() !== 'int ' ) {
420
+ throw new InvalidCallableException ("The second parameter of the callable {$ argumentName } must be of type `int`. " );
421
+ }
422
+ if ($ reflection ->hasReturnType () && $ reflection ->getReturnType ()?->getName() !== $ returnType ) {
423
+ throw new InvalidCallableException ("The callable {$ argumentName } must return a string. " );
424
+ }
452
425
}
453
426
454
427
}
0 commit comments