Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #18332 [Form] added
CallbackChoiceLoader
and refactored Cho…
…iceType's children (HeahDude) This PR was merged into the 3.2-dev branch. Discussion ---------- [Form] added `CallbackChoiceLoader` and refactored ChoiceType's children | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | ~ | License | MIT | Doc PR | todo Todo ==== - [ ] Address a doc PR - [x] Update CHANGELOG Changes ======= - 39e937f added `CallbackChoiceLoader` to lazy load choices with a simple callable. - 995dc56 refactored `CountryType`, `CurrencyType`, `LanguageType`, `LocaleType` and `TimezoneType` for better performance by implementing `ChoiceLoaderInterface` for lazy loading. Usage ===== ```php use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; $builder->add('constants', ChoiceType::class, array( 'choice_loader' => new CallbackChoiceLoader(function() { return StaticClass::getConstants(); }, )); ``` Commits ------- 8a4e164 [Form] implemented ChoiceLoaderInterface in children of ChoiceType afd7bf8 [Form] added `CallbackChoiceLoader`
- Loading branch information
Showing
8 changed files
with
511 additions
and
38 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
77 changes: 77 additions & 0 deletions
77
src/Symfony/Component/Form/ChoiceList/Loader/CallbackChoiceLoader.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,77 @@ | ||
<?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; | ||
|
||
/** | ||
* Loads an {@link ArrayChoiceList} instance from a callable returning an array of choices. | ||
* | ||
* @author Jules Pietri <jules@heahprod.com> | ||
*/ | ||
class CallbackChoiceLoader implements ChoiceLoaderInterface | ||
{ | ||
private $callback; | ||
|
||
/** | ||
* The loaded choice list. | ||
* | ||
* @var ArrayChoiceList | ||
*/ | ||
private $choiceList; | ||
|
||
/** | ||
* @param callable $callback The callable returning an array of choices | ||
*/ | ||
public function __construct(callable $callback) | ||
{ | ||
$this->callback = $callback; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function loadChoiceList($value = null) | ||
{ | ||
if (null !== $this->choiceList) { | ||
return $this->choiceList; | ||
} | ||
|
||
return $this->choiceList = new ArrayChoiceList(call_user_func($this->callback), $value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function loadChoicesForValues(array $values, $value = null) | ||
{ | ||
// Optimize | ||
if (empty($values)) { | ||
return array(); | ||
} | ||
|
||
return $this->loadChoiceList($value)->getChoicesForValues($values); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function loadValuesForChoices(array $choices, $value = null) | ||
{ | ||
// Optimize | ||
if (empty($choices)) { | ||
return array(); | ||
} | ||
|
||
return $this->loadChoiceList($value)->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
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
Oops, something went wrong.