@@ -59,12 +59,16 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
59
59
$ choices = $ list ->getChoices ();
60
60
$ keys = $ list ->getOriginalKeys ();
61
61
62
- if (!\is_callable ($ preferredChoices ) && !empty ($ preferredChoices )) {
63
- // make sure we have keys that reflect order
64
- $ preferredChoices = array_values ($ preferredChoices );
65
- $ preferredChoices = static function ($ choice ) use ($ preferredChoices ) {
66
- return array_search ($ choice , $ preferredChoices , true );
67
- };
62
+ if (!\is_callable ($ preferredChoices )) {
63
+ if (empty ($ preferredChoices )) {
64
+ $ preferredChoices = null ;
65
+ } else {
66
+ // make sure we have keys that reflect order
67
+ $ preferredChoices = array_values ($ preferredChoices );
68
+ $ preferredChoices = static function ($ choice ) use ($ preferredChoices ) {
69
+ return array_search ($ choice , $ preferredChoices , true );
70
+ };
71
+ }
68
72
}
69
73
70
74
// The names are generated from an incrementing integer by default
@@ -82,7 +86,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
82
86
self ::addChoiceViewsGroupedByCallable (
83
87
$ groupBy ,
84
88
$ choice ,
85
- ( string ) $ value ,
89
+ $ value ,
86
90
$ label ,
87
91
$ keys ,
88
92
$ index ,
@@ -132,7 +136,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
132
136
return new ChoiceListView ($ otherViews , $ preferredViews );
133
137
}
134
138
135
- private static function addChoiceView ($ choice , $ value , $ label , $ keys , &$ index , $ attr , $ isPreferred , &$ preferredViews , &$ preferredViewsOrder , &$ otherViews )
139
+ private static function addChoiceView ($ choice , string $ value , ? callable $ label , array $ keys , &$ index , $ attr , ? callable $ isPreferred , array &$ preferredViews , array &$ preferredViewsOrder , array &$ otherViews )
136
140
{
137
141
// $value may be an integer or a string, since it's stored in the array
138
142
// keys. We want to guarantee it's a string though.
@@ -143,7 +147,7 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $
143
147
if (null === $ label ) {
144
148
// If the labels are null, use the original choice key by default
145
149
$ label = (string ) $ key ;
146
- } elseif ( false !== $ label ) {
150
+ } else {
147
151
// If "choice_label" is set to false and "expanded" is true, the value false
148
152
// should be passed on to the "label" option of the checkboxes/radio buttons
149
153
$ dynamicLabel = $ label ($ choice , $ key , $ value );
@@ -160,15 +164,15 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $
160
164
);
161
165
162
166
// $isPreferred may be null if no choices are preferred
163
- if ($ isPreferred && false !== $ preferredKey = $ isPreferred ($ choice , $ key , $ value )) {
167
+ if (null !== $ isPreferred && false !== $ preferredKey = $ isPreferred ($ choice , $ key , $ value )) {
164
168
$ preferredViews [$ nextIndex ] = $ view ;
165
169
$ preferredViewsOrder [$ nextIndex ] = $ preferredKey ;
166
170
}
167
171
168
172
$ otherViews [$ nextIndex ] = $ view ;
169
173
}
170
174
171
- private static function addChoiceViewsFromStructuredValues ($ values , $ label , $ choices , $ keys , &$ index , $ attr , $ isPreferred , &$ preferredViews , &$ preferredViewsOrder , &$ otherViews )
175
+ private static function addChoiceViewsFromStructuredValues (array $ values , ? callable $ label , array $ choices , array $ keys , &$ index , $ attr , ? callable $ isPreferred , array &$ preferredViews , array &$ preferredViewsOrder , array &$ otherViews )
172
176
{
173
177
foreach ($ values as $ key => $ value ) {
174
178
if (null === $ value ) {
@@ -220,7 +224,7 @@ private static function addChoiceViewsFromStructuredValues($values, $label, $cho
220
224
}
221
225
}
222
226
223
- private static function addChoiceViewsGroupedByCallable ($ groupBy , $ choice , $ value , $ label , $ keys , &$ index , $ attr , $ isPreferred , &$ preferredViews , &$ preferredViewsOrder , &$ otherViews )
227
+ private static function addChoiceViewsGroupedByCallable (callable $ groupBy , $ choice , string $ value , ? callable $ label , array $ keys , &$ index , $ attr , ? callable $ isPreferred , array &$ preferredViews , array &$ preferredViewsOrder , array &$ otherViews )
224
228
{
225
229
$ groupLabels = $ groupBy ($ choice , $ keys [$ value ], $ value );
226
230
0 commit comments