@@ -59,12 +59,16 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
5959 $ choices = $ list ->getChoices ();
6060 $ keys = $ list ->getOriginalKeys ();
6161
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+ }
6872 }
6973
7074 // The names are generated from an incrementing integer by default
@@ -82,7 +86,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
8286 self ::addChoiceViewsGroupedByCallable (
8387 $ groupBy ,
8488
8000
$ choice ,
85- ( string ) $ value ,
89+ $ value ,
8690 $ label ,
8791 $ keys ,
8892 $ index ,
@@ -132,7 +136,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
132136 return new ChoiceListView ($ otherViews , $ preferredViews );
133137 }
134138
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 )
136140 {
137141 // $value may be an integer or a string, since it's stored in the array
138142 // keys. We want to guarantee it's a string though.
@@ -143,7 +147,7 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $
143147 if (null === $ label ) {
144148 // If the labels are null, use the original choice key by default
145149 $ label = (string ) $ key ;
146- } elseif ( false !== $ label ) {
150+ } else {
147151
8000
// If "choice_label" is set to false and "expanded" is true, the value false
148152 // should be passed on to the "label" option of the checkboxes/radio buttons
149153 $ dynamicLabel = $ label ($ choice , $ key , $ value );
@@ -160,15 +164,15 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $
160164 );
161165
162166 // $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 )) {
164168 $ preferredViews [$ nextIndex ] = $ view ;
165169 $ preferredViewsOrder [$ nextIndex ] = $ preferredKey ;
166170 }
167171
168172 $ otherViews [$ nextIndex ] = $ view ;
169173 }
170174
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 )
172176 {
173177 foreach ($ values as $ key => $ value ) {
174178 if (null === $ value ) {
@@ -220,7 +224,7 @@ private static function addChoiceViewsFromStructuredValues($values, $label, $cho
220224 }
221225 }
222226
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 )
224228 {
225229 $ groupLabels = $ groupBy ($ choice , $ keys [$ value ], $ value );
226230
0 commit comments