22
33namespace Laravel \Nova \Fields ;
44
5+ use Closure ;
56use Illuminate \Support \Arr ;
67use Laravel \Nova \Contracts \FilterableField ;
78use Laravel \Nova \Fields \Filters \BooleanGroupFilter ;
1011use Laravel \Nova \Util ;
1112use Stringable ;
1213
14+ /**
15+ * @phpstan-type TOptionLabel \Stringable|string
16+ * @phpstan-type TOptionValue string
17+ * @phpstan-type TOption iterable<TOptionValue|int, TOptionLabel>
18+ */
1319class BooleanGroup extends Field implements FilterableField
1420{
1521 use FieldFilterable;
@@ -37,11 +43,13 @@ class BooleanGroup extends Field implements FilterableField
3743 public $ noValueText = 'No Data ' ;
3844
3945 /**
40- * The options for the field .
46+ * The field's options callback .
4147 *
42- * @var array|null
48+ * @var iterable<string|int, string>|callable|null
49+ *
50+ * @phpstan-var TOption|(callable(): (TOption))|null
4351 */
44- public $ options = null ;
52+ public $ optionsCallback ;
4553
4654 /**
4755 * Determine false values should be hidden.
@@ -65,11 +73,26 @@ class BooleanGroup extends Field implements FilterableField
6573 */
6674 public function options (callable |iterable $ options )
6775 {
68- if (Util::isSafeCallable ($ options )) {
69- $ options = \call_user_func ($ options );
70- }
76+ $ this ->optionsCallback = $ options ;
7177
72- $ this ->options = with (collect ($ options ), static function ($ options ) {
78+ return $ this ;
79+ }
80+
81+ /**
82+ * Serialize options for the field.
83+ *
84+ * @return array<int, array<string, mixed>>
85+ *
86+ * @phpstan-return array<int, array{label: string, value: string}>
87+ */
88+ protected function serializeOptions (): array
89+ {
90+ /** @var TOption $options */
91+ $ options = ! Util::isSafeCallable ($ this ->optionsCallback ) && ! $ this ->optionsCallback instanceof Closure
92+ ? value ($ this ->optionsCallback )
93+ : \call_user_func ($ this ->optionsCallback );
94+
95+ return with (collect ($ options ), static function ($ options ) {
7396 $ isList = array_is_list ($ options ->all ());
7497
7598 return $ options ->map (static function ($ label , $ name ) use ($ isList ) {
@@ -78,8 +101,6 @@ public function options(callable|iterable $options)
78101 : ['label ' => $ label , 'name ' => $ label ];
79102 })->values ()->all ();
80103 });
81-
82- return $ this ;
83104 }
84105
85106 /**
@@ -188,7 +209,7 @@ public function jsonSerialize(): array
188209 return array_merge (parent ::jsonSerialize (), [
189210 'hideTrueValues ' => $ this ->hideTrueValues ,
190211 'hideFalseValues ' => $ this ->hideFalseValues ,
191- 'options ' => $ this ->options ,
212+ 'options ' => $ this ->serializeOptions () ,
192213 'noValueText ' => Nova::__ ($ this ->noValueText ),
193214 ]);
194215 }
0 commit comments