-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Added an AbstractChoiceLoader #30983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\ChoiceList\Loader; | ||
|
||
use Symfony\Component\Form\ChoiceList\ArrayChoiceList; | ||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface; | ||
|
||
/** | ||
* @author Jules Pietri <jules@heahprod.com> | ||
*/ | ||
abstract class AbstractChoiceLoader implements ChoiceLoaderInterface | ||
{ | ||
/** | ||
* The loaded choice list. | ||
* | ||
* @var ChoiceListInterface | ||
*/ | ||
protected $choiceList; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function loadChoiceList($value = null) | ||
{ | ||
if (null !== $this->choiceList) { | ||
return $this->choiceList; | ||
} | ||
|
||
return $this->choiceList = new ArrayChoiceList($this->loadChoices(), $value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function loadChoicesForValues(array $values, $value = null) | ||
{ | ||
// Optimize | ||
if (empty($values)) { | ||
return []; | ||
} | ||
|
||
if ($this->choiceList) { | ||
return $this->choiceList->getChoicesForValues($values, $value); | ||
} | ||
|
||
return $this->doLoadChoicesForValues($values, $value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function loadValuesForChoices(array $choices, $value = null) | ||
{ | ||
// Optimize | ||
if (empty($choices)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some implementations use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It happens that an empty value may be a valid choice for any choice list, but we know it's not the case with intl base choice types, this is why we optimized their loaders specifically. |
||
return []; | ||
} | ||
|
||
if ($value) { | ||
// if a value callback exists, use it | ||
return array_map($value, $choices); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually a new optimisation that will make some good when using |
||
} | ||
|
||
if ($this->choiceList) { | ||
return $this->choiceList->getValuesForChoices($choices); | ||
} | ||
|
||
return $this->doLoadValuesForChoices($choices); | ||
} | ||
|
||
abstract protected function loadChoices(): array; | ||
|
||
protected function doLoadChoicesForValues(array $values, ?callable $value): array | ||
{ | ||
return $this->loadChoiceList($value)->getChoicesForValues($values); | ||
} | ||
|
||
protected function doLoadValuesForChoices(array $choices): array | ||
{ | ||
return $this->loadChoiceList()->getValuesForChoices($choices); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (!$values)
?