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 the callable returns null, the choice is not added to any group
|
||||||
if (\is_callable($groupBy)) {
|
if (\is_callable($groupBy)) {
|
||||||
foreach ($choices as $value => $choice) {
|
foreach ($choices as $value => $choice) {
|
||||||
self::addChoiceViewGroupedBy(
|
self::addChoiceViewsGroupedByCallable(
|
||||||
$groupBy,
|
$groupBy,
|
||||||
$choice,
|
$choice,
|
||||||
(string) $value,
|
(string) $value,
|
||||||
@ -83,9 +83,23 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
|
|||||||
$otherViews
|
$otherViews
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($otherViews as $key => $view) {
|
||||||
|
if ($view instanceof ChoiceGroupView && 0 === \count($view->choices)) {
|
||||||
|
unset($otherViews[$key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Otherwise use the original structure of the choices
|
// Otherwise use the original structure of the choices
|
||||||
self::addChoiceViewsGroupedBy(
|
self::addChoiceViewsFromStructuredValues(
|
||||||
$list->getStructuredValues(),
|
$list->getStructuredValues(),
|
||||||
$label,
|
$label,
|
||||||
$choices,
|
$choices,
|
||||||
@ -98,20 +112,6 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove any empty group view that may have been created by
|
|
||||||
// addChoiceViewGroupedBy()
|
|
||||||
foreach ($preferredViews as $key => $view) {
|
|
||||||
if ($view instanceof ChoiceGroupView && 0 === \count($view->choices)) {
|
|
||||||
unset($preferredViews[$key]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($otherViews as $key => $view) {
|
|
||||||
if ($view instanceof ChoiceGroupView && 0 === \count($view->choices)) {
|
|
||||||
unset($otherViews[$key]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ChoiceListView($otherViews, $preferredViews);
|
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) {
|
if (null === $value) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -162,7 +162,7 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
|
|||||||
$preferredViewsForGroup = [];
|
$preferredViewsForGroup = [];
|
||||||
$otherViewsForGroup = [];
|
$otherViewsForGroup = [];
|
||||||
|
|
||||||
self::addChoiceViewsGroupedBy(
|
self::addChoiceViewsFromStructuredValues(
|
||||||
$value,
|
$value,
|
||||||
$label,
|
$label,
|
||||||
$choices,
|
$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);
|
$groupLabels = $groupBy($choice, $keys[$value], $value);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user