minor #30953 [Form] Refactor DefaultChoiceListFactory (vudaltsov)
This PR was merged into the 4.3-dev branch.
Discussion
----------
[Form] Refactor DefaultChoiceListFactory
| Q | A
| ------------- | ---
| Branch? | 3.4
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | n/a
| License | MIT
| Doc PR | n/a
A small optimization (remove unnecessary `foreach` calls) and a naming improvement.
ping @HeahDude , @stof
#EUFOSSA
Commits
-------
77424c8557
Refactor DefaultChoiceListFactory
This commit is contained in:
commit
76906ab6e8
@ -70,7 +70,7 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
|
||||
// If the callable returns null, the choice is not added to any group
|
||||
if (\is_callable($groupBy)) {
|
||||
foreach ($choices as $value => $choice) {
|
||||
self::addChoiceViewGroupedBy(
|
||||
self::addChoiceViewsGroupedByCallable(
|
||||
$groupBy,
|
||||
$choice,
|
||||
(string) $value,
|
||||
@ -83,23 +83,9 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
|
||||
$otherViews
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// Otherwise use the original structure of the choices
|
||||
self::addChoiceViewsGroupedBy(
|
||||
$list->getStructuredValues(),
|
||||
$label,
|
||||
$choices,
|
||||
$keys,
|
||||
$index,
|
||||
$attr,
|
||||
$preferredChoices,
|
||||
$preferredViews,
|
||||
$otherViews
|
||||
);
|
||||
}
|
||||
|
||||
// Remove any empty group view that may have been created by
|
||||
// addChoiceViewGroupedBy()
|
||||
// Remove empty group views that may have been created by
|
||||
// addChoiceViewsGroupedByCallable()
|
||||
foreach ($preferredViews as $key => $view) {
|
||||
if ($view instanceof ChoiceGroupView && 0 === \count($view->choices)) {
|
||||
unset($preferredViews[$key]);
|
||||
@ -111,6 +97,20 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
|
||||
unset($otherViews[$key]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Otherwise use the original structure of the choices
|
||||
self::addChoiceViewsFromStructuredValues(
|
||||
$list->getStructuredValues(),
|
||||
$label,
|
||||
$choices,
|
||||
$keys,
|
||||
$index,
|
||||
$attr,
|
||||
$preferredChoices,
|
||||
$preferredViews,
|
||||
$otherViews
|
||||
);
|
||||
}
|
||||
|
||||
return new ChoiceListView($otherViews, $preferredViews);
|
||||
}
|
||||
@ -150,9 +150,9 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
|
||||
}
|
||||
}
|
||||
|
||||
private static function addChoiceViewsGroupedBy($groupBy, $label, $choices, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
|
||||
private static function addChoiceViewsFromStructuredValues($values, $label, $choices, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
|
||||
{
|
||||
foreach ($groupBy as $key => $value) {
|
||||
foreach ($values as $key => $value) {
|
||||
if (null === $value) {
|
||||
continue;
|
||||
}
|
||||
@ -162,7 +162,7 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
|
||||
$preferredViewsForGroup = [];
|
||||
$otherViewsForGroup = [];
|
||||
|
||||
self::addChoiceViewsGroupedBy(
|
||||
self::addChoiceViewsFromStructuredValues(
|
||||
$value,
|
||||
$label,
|
||||
$choices,
|
||||
@ -200,7 +200,7 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
|
||||
}
|
||||
}
|
||||
|
||||
private static function addChoiceViewGroupedBy($groupBy, $choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
|
||||
private static function addChoiceViewsGroupedByCallable($groupBy, $choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
|
||||
{
|
||||
$groupLabels = $groupBy($choice, $keys[$value], $value);
|
||||
|
||||
|
Reference in New Issue
Block a user