Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Form] Added an AbstractChoiceLoader to simplify implementations and …
…handle global optimizations
- Loading branch information
Showing
8 changed files
with
129 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/Symfony/Component/Form/ChoiceList/Loader/AbstractChoiceLoader.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?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; | ||
|
||
/** | ||
* @author Jules Pietri <jules@heahprod.com> | ||
*/ | ||
abstract class AbstractChoiceLoader implements ChoiceLoaderInterface | ||
{ | ||
/** | ||
* The loaded choice list. | ||
* | ||
* @var ArrayChoiceList | ||
*/ | ||
private $choiceList; | ||
|
||
/** | ||
* @final | ||
* | ||
* {@inheritdoc} | ||
*/ | ||
public function loadChoiceList(callable $value = null) | ||
{ | ||
return $this->choiceList ?? ($this->choiceList = new ArrayChoiceList($this->loadChoices(), $value)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function loadChoicesForValues(array $values, callable $value = null) | ||
{ | ||
// Optimize | ||
if (!$values) { | ||
return []; | ||
} | ||
|
||
if ($this->choiceList) { | ||
return $this->choiceList->getChoicesForValues($values); | ||
} | ||
|
||
return $this->doLoadChoicesForValues($values, $value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function loadValuesForChoices(array $choices, callable $value = null) | ||
{ | ||
// Optimize | ||
if (!$choices) { | ||
return []; | ||
} | ||
|
||
if ($value) { | ||
// if a value callback exists, use it | ||
return array_map($value, $choices); | ||
} | ||
|
||
if ($this->choiceList) { | ||
return $this->choiceList->getValuesForChoices($choices); | ||
} | ||
|
||
return $this->doLoadValuesForChoices($choices); | ||
} | ||
|
||
abstract protected function loadChoices(): iterable; | ||
|
||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.