-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] add choice_filter
option to ChoiceType
#18334
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
Closed
HeahDude
wants to merge
1
commit into
symfony:master
from
HeahDude:feature-choice_type-choice_filter-option
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
61 changes: 61 additions & 0 deletions
61
src/Symfony/Component/Form/ChoiceList/Factory/FilteredChoiceListFactoryInterface.php
This file contains hidden or 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,61 @@ | ||
<?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\Factory; | ||
|
||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface; | ||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface; | ||
|
||
/** | ||
* Creates filtered {@link ChoiceListInterface} instances. | ||
* | ||
* @author Jules Pietri <jules@heahprod.com> | ||
*/ | ||
interface FilteredChoiceListFactoryInterface extends ChoiceListFactoryInterface | ||
{ | ||
/** | ||
* Creates a filtered choice list for the given choices. | ||
* | ||
* The choices should be passed in the values of the choices array. | ||
* | ||
* The filter callable gets passed each choice and its resolved value | ||
* and should return true to keep the choice and false or null otherwise. | ||
* | ||
* Optionally, a callable can be passed for generating the choice values. | ||
* The callable receives the choice as only argument. | ||
* | ||
* @param array|\Traversable $choices The choices | ||
* @param null|callable $value The callable generating the choice | ||
* values | ||
* @param callable $filter The filter | ||
* | ||
* @return ChoiceListInterface The filtered choice list | ||
*/ | ||
public function createFilteredListFromChoices($choices, $value = null, callable $filter); | ||
|
||
/** | ||
* Creates a filtered choice list that is loaded with the given loader. | ||
* | ||
* The filter callable gets passed each choice and its resolved value | ||
* and should return true to keep the choice and false or null otherwise. | ||
* | ||
* Optionally, a callable can be passed for generating the choice values. | ||
* The callable receives the choice as only argument. | ||
* | ||
* @param ChoiceLoaderInterface $loader The choice loader | ||
* @param null|callable $value The callable generating the choice | ||
* values | ||
* @param callable $filter The filter | ||
* | ||
* @return ChoiceListInterface The filtered choice list | ||
*/ | ||
public function createFilteredListFromLoader(ChoiceLoaderInterface $loader, $value = null, callable $filter); | ||
} |
138 changes: 138 additions & 0 deletions
138
src/Symfony/Component/Form/ChoiceList/Factory/FilteringFactoryDecorator.php
This file contains hidden or 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,138 @@ | ||
<?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\Factory; | ||
|
||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface; | ||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface; | ||
|
||
/** | ||
* Filter the choices before passing them to the decorated factory. | ||
* | ||
* @author Jules Pietri <jules@heahprod.com> | ||
*/ | ||
class FilteringFactoryDecorator implements FilteredChoiceListFactoryInterface | ||
{ | ||
/** | ||
* @var ChoiceListFactoryInterface | ||
*/ | ||
private $decoratedFactory; | ||
|
||
/** | ||
* @var array[] | ||
*/ | ||
private $choicesByValues = array(); | ||
|
||
/** | ||
* Decorates the given factory. | ||
* | ||
* @param ChoiceListFactoryInterface $decoratedFactory The decorated factory | ||
*/ | ||
public function __construct(ChoiceListFactoryInterface $decoratedFactory) | ||
{ | ||
$this->decoratedFactory = $decoratedFactory; | ||
} | ||
|
||
/** | ||
* Returns the decorated factory. | ||
* | ||
* @return ChoiceListFactoryInterface The decorated factory | ||
*/ | ||
public function getDecoratedFactory() | ||
{ | ||
return $this->decoratedFactory; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function createListFromChoices($choices, $value = null) | ||
{ | ||
return $this->decoratedFactory->createListFromChoices($choices, $value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function createListFromLoader(ChoiceLoaderInterface $loader, $value = null) | ||
{ | ||
return $this->decoratedFactory->createListFromLoader($loader, $value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function createFilteredListFromChoices($choices, $value = null, callable $filter) | ||
{ | ||
// We need to apply the filter on a resolved choices array in case | ||
// the same choices are filtered many times. The original choice list | ||
// should be cached by the decorated factory | ||
$choiceList = $this->decoratedFactory->createListFromChoices($choices, $value); | ||
|
||
// The filtered choice list should be cached by the decorated factory | ||
// if the same filter is applied on the same choices by values | ||
|
||
return $this->decoratedFactory->createListFromChoices(self::filterChoices($choiceList->getChoices(), $filter)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function createFilteredListFromLoader(ChoiceLoaderInterface $loader, $value = null, callable $filter) | ||
{ | ||
// Don't hash the filter since the original choices may have been loaded already | ||
// with a different filter if any. | ||
$hash = CachingFactoryDecorator::generateHash(array($loader, $value)); | ||
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. |
||
|
||
if (!isset($this->choicesByValues[$hash])) { | ||
// We need to load the choice list before filtering the choices | ||
$choiceList = $this->decoratedFactory->createListFromLoader($loader, $value); | ||
|
||
// Cache the choices by values, in case they are filtered many times, | ||
// the original choice list should already have been cached by the | ||
// previous call. | ||
$this->choicesByValues[$hash] = $choiceList->getChoices(); | ||
} | ||
|
||
// The filtered choice list should be cached by the decorated factory | ||
// if the same filter is applied on the same choices by values | ||
|
||
return $this->decoratedFactory->createListFromChoices(self::filterChoices($this->choicesByValues[$hash], $filter)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null) | ||
{ | ||
$this->decoratedFactory->createView($list, $preferredChoices, $label, $index, $groupBy, $attr); | ||
} | ||
|
||
/** | ||
* Filters the choices. | ||
* | ||
* @param array $choices The choices by values to filter | ||
* @param callable $filter The filter | ||
* | ||
* @return array The filtered choices | ||
*/ | ||
static private function filterChoices($choices, callable $filter) | ||
{ | ||
foreach ($choices as $value => $choice) { | ||
if (call_user_func($filter, $choice, $value)) { | ||
continue; | ||
} | ||
unset($choices[$value]); | ||
} | ||
|
||
return $choices; | ||
} | ||
} |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm just not sure about the over caching here, but it seems that considering this decorator might not decorate the
CachingFactoryDecorator
, keeping the resolved choices to filter should be the right thing to do.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.
This caching is useless, as the decorated choice list already has caching