33namespace RingleSoft \LaravelSelectable ;
44
55use Closure ;
6- use Exception ;
76use Illuminate \Support \Collection ;
87use ReflectionException ;
98use ReflectionFunction ;
@@ -52,11 +51,11 @@ private function _shouldSelect(mixed $item, int|string|null $index = null): bool
5251 return (bool )call_user_func ($ this ->_selected , $ item , $ index );
5352 }
5453
55- if ($ this ->_value instanceof Closure) {
54+ if ($ this ->_value instanceof Closure) {
5655 $ optionValue = call_user_func ($ this ->_value , $ item , $ index );
5756 } else {
5857 $ optionValue = (is_object ($ item ) ? ($ item ->{$ this ->_value } ?? "" ) : $ item );
59- if (is_array ($ item )) {
58+ if (is_array ($ item )) {
6059 $ optionValue = $ item [$ this ->_value ] ?? reset ($ item );
6160 }
6261 }
@@ -95,15 +94,11 @@ private function _shouldDisable(mixed $item, int|string|null $index = null): boo
9594 }
9695
9796 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 );
10398 } else {
10499 $ 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 );
107102 }
108103 }
109104
@@ -142,12 +137,8 @@ private function _getDataAttributes(mixed $item, int|null $index = null): array
142137 foreach ($ this ->_dataAttributes as $ attributeItem ) {
143138 $ attribute = $ attributeItem ['attribute ' ];
144139 $ 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 } ?? '' );
151142 }
152143 }
153144 return $ dataAttributes ;
@@ -172,15 +163,15 @@ private function _generateOptions(Collection $collection, int $lastIndex = 0): s
172163 $ optionLabel = call_user_func ($ this ->_label , $ item , $ index );
173164 } else {
174165 $ optionLabel = is_object ($ item ) ? ($ item ->{$ this ->_label } ?? "N/A " ) : ($ item );
175- if (is_array ($ item )) {
166+ if (is_array ($ item )) {
176167 $ optionLabel = $ item [$ this ->_label ] ?? array_keys ($ item )[0 ];
177168 }
178169 }
179170 if ($ this ->_value instanceof Closure) {
180171 $ optionValue = call_user_func ($ this ->_value , $ item , $ index );
181172 } else {
182173 $ optionValue = is_object ($ item ) ? ($ item ->{$ this ->_value } ?? "" ) : $ item ;
183- if (is_array ($ item )) {
174+ if (is_array ($ item )) {
184175 $ optionValue = $ item [$ this ->_value ] ?? reset ($ item );
185176 }
186177 if (is_string ($ index ) && is_string ($ item )) {
@@ -189,12 +180,8 @@ private function _generateOptions(Collection $collection, int $lastIndex = 0): s
189180 }
190181 // Prepare Option
191182 $ 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 )) . "\"" ;
198185 }
199186 if ($ this ->_shouldSelect ($ item , $ index )) {
200187 $ html .= " selected " ;
@@ -210,11 +197,7 @@ private function _generateOptions(Collection $collection, int $lastIndex = 0): s
210197 if (count ($ this ->_classes ) > 0 ) {
211198 $ html .= " class= \"" ;
212199 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 ) . " " ;
218201 }
219202 $ html = rtrim ($ html ) . "\"" ;
220203 }
@@ -271,20 +254,12 @@ public function toSelectItems(): Collection
271254 {
272255 return $ this ->_collection ->map (function ($ item , $ index ) {
273256 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 );
279258 } else {
280259 $ optionLabel = is_object ($ item ) ? ($ item ->{$ this ->_label } ?? "N/A " ) : ($ item );
281260 }
282261 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 );
288263 } else {
289264 $ optionValue = is_object ($ item ) ? ($ item ->{$ this ->_value } ?? "" ) : $ item ;
290265 if (is_string ($ index ) && is_string ($ item )) {
@@ -326,7 +301,7 @@ public function withValue(string|Closure $value): self
326301
327302 /**
328303 * Specify the selected values for the selectable items
329- * @param mixed $selected
304+ * @param mixed|Closure(mixed, int|string|null): bool $selected
330305 * @return $this
331306 */
332307 public function withSelected (mixed $ selected ): self
@@ -351,7 +326,7 @@ public function withDisabled(mixed $disabled): self
351326 * @param string|Closure(mixed, int):string $attribute Data attribute name
352327 * * @param string|Closure(mixed, int):mixed $value Data attribute value
353328 * * @return $this
354- */
329+ */
355330 public function withDataAttribute (string |Closure $ attribute , string |Closure $ value ): self
356331 {
357332 $ this ->_dataAttributes [] = ['attribute ' => $ attribute , 'value ' => $ value ];
@@ -421,34 +396,32 @@ public function __call(string $name, array $arguments)
421396
422397
423398 /**
424- * Internal method to validate the callable.
399+ * Experimental (Currently not used)
400+ * Internal method to validate the user provided callable.
425401 * @param callable $callable
426402 * @param string $returnType
427403 * @param null $argumentName
428404 * @return void
429405 * @throws InvalidCallableException
430- * @throws ReflectionException
431406 */
432407 private function validateCallable (callable $ callable , string $ returnType = 'string ' , $ argumentName = null ): void
433- {
408+ {
409+ try {
434410 $ 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+ }
452425 }
453426
454427}
0 commit comments