diff --git a/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php b/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php index 9b3c50307ce61..4351b66a3c41c 100644 --- a/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php +++ b/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php @@ -22,6 +22,7 @@ use Symfony\Component\Form\ChoiceList\Factory\CachingFactoryDecorator; use Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface; use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory; +use Symfony\Component\Form\ChoiceList\Factory\FilteringFactoryDecorator; use Symfony\Component\Form\ChoiceList\Factory\PropertyAccessDecorator; use Symfony\Component\Form\Exception\RuntimeException; use Symfony\Component\Form\FormBuilderInterface; @@ -111,10 +112,12 @@ public function getQueryBuilderPartsForCachingHash($queryBuilder) public function __construct(ManagerRegistry $registry, PropertyAccessorInterface $propertyAccessor = null, ChoiceListFactoryInterface $choiceListFactory = null) { $this->registry = $registry; - $this->choiceListFactory = $choiceListFactory ?: new CachingFactoryDecorator( - new PropertyAccessDecorator( - new DefaultChoiceListFactory(), - $propertyAccessor + $this->choiceListFactory = $choiceListFactory ?: new FilteringFactoryDecorator( + new CachingFactoryDecorator( + new PropertyAccessDecorator( + new DefaultChoiceListFactory(), + $propertyAccessor + ) ) ); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml index edbd0d64b88e1..03e159d40fe6a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml @@ -59,7 +59,11 @@ - + + + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/composer.json b/src/Symfony/Bundle/FrameworkBundle/composer.json index 86752accf1f6a..00cb7e228ed9b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/composer.json +++ b/src/Symfony/Bundle/FrameworkBundle/composer.json @@ -43,7 +43,7 @@ "symfony/dom-crawler": "~2.8|~3.0", "symfony/polyfill-intl-icu": "~1.0", "symfony/security": "~2.8|~3.0", - "symfony/form": "~2.8|~3.0", + "symfony/form": "~2.8|~3.1", "symfony/expression-language": "~2.8|~3.0", "symfony/process": "~2.8|~3.0", "symfony/serializer": "~2.8|^3.0", diff --git a/src/Symfony/Component/Form/ChoiceList/Factory/FilteredChoiceListFactoryInterface.php b/src/Symfony/Component/Form/ChoiceList/Factory/FilteredChoiceListFactoryInterface.php new file mode 100644 index 0000000000000..ea0cc342e98fc --- /dev/null +++ b/src/Symfony/Component/Form/ChoiceList/Factory/FilteredChoiceListFactoryInterface.php @@ -0,0 +1,61 @@ + + * + * 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 + */ +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); +} diff --git a/src/Symfony/Component/Form/ChoiceList/Factory/FilteringFactoryDecorator.php b/src/Symfony/Component/Form/ChoiceList/Factory/FilteringFactoryDecorator.php new file mode 100644 index 0000000000000..c07a0dfd306fd --- /dev/null +++ b/src/Symfony/Component/Form/ChoiceList/Factory/FilteringFactoryDecorator.php @@ -0,0 +1,138 @@ + + * + * 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 + */ +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)); + + 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; + } +} diff --git a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php index ca1e5b737b9ab..461aca6cccb92 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php @@ -13,6 +13,8 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\ChoiceList\Factory\CachingFactoryDecorator; +use Symfony\Component\Form\ChoiceList\Factory\FilteredChoiceListFactoryInterface; +use Symfony\Component\Form\ChoiceList\Factory\FilteringFactoryDecorator; use Symfony\Component\Form\ChoiceList\Factory\PropertyAccessDecorator; use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView; use Symfony\Component\Form\ChoiceList\ChoiceListInterface; @@ -45,9 +47,11 @@ class ChoiceType extends AbstractType public function __construct(ChoiceListFactoryInterface $choiceListFactory = null) { - $this->choiceListFactory = $choiceListFactory ?: new CachingFactoryDecorator( - new PropertyAccessDecorator( - new DefaultChoiceListFactory() + $this->choiceListFactory = $choiceListFactory ?: new FilteringFactoryDecorator( + new CachingFactoryDecorator( + new PropertyAccessDecorator( + new DefaultChoiceListFactory() + ) ) ); } @@ -315,6 +319,7 @@ public function configureOptions(OptionsResolver $resolver) 'expanded' => false, 'choices' => array(), 'choices_as_values' => null, // deprecated since 3.1 + 'choice_filter' => null, 'choice_loader' => null, 'choice_label' => null, 'choice_name' => null, @@ -339,6 +344,7 @@ public function configureOptions(OptionsResolver $resolver) $resolver->setAllowedTypes('choices', array('null', 'array', '\Traversable')); $resolver->setAllowedTypes('choice_translation_domain', array('null', 'bool', 'string')); + $resolver->setAllowedTypes('choice_filter', array('null', 'callable')); $resolver->setAllowedTypes('choice_loader', array('null', 'Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')); $resolver->setAllowedTypes('choice_label', array('null', 'bool', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath')); $resolver->setAllowedTypes('choice_name', array('null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath')); @@ -414,15 +420,28 @@ private function addSubForm(FormBuilderInterface $builder, $name, ChoiceView $ch private function createChoiceList(array $options) { if (null !== $options['choice_loader']) { - return $this->choiceListFactory->createListFromLoader( - $options['choice_loader'], - $options['choice_value'] - ); + if (is_callable($options['choice_filter']) && $this->choiceListFactory instanceof FilteredChoiceListFactoryInterface) { + return $this->choiceListFactory->createFilteredListFromLoader( + $options['choice_loader'], + $options['choice_value'], + $options['choice_filter'] + ); + } + + return $this->choiceListFactory->createListFromLoader($options['choice_loader'], $options['choice_value']); } // Harden against NULL values (like in EntityType and ModelType) $choices = null !== $options['choices'] ? $options['choices'] : array(); + if (!empty($choices) && is_callable($options['choice_filter']) && $this->choiceListFactory instanceof FilteredChoiceListFactoryInterface) { + return $this->choiceListFactory->createFilteredListFromChoices( + $choices, + $options['choice_value'], + $options['choice_filter'] + ); + } + return $this->choiceListFactory->createListFromChoices($choices, $options['choice_value']); } diff --git a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/FilteringFactoryDecoratorTest.php b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/FilteringFactoryDecoratorTest.php new file mode 100644 index 0000000000000..2081f73d0e086 --- /dev/null +++ b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/FilteringFactoryDecoratorTest.php @@ -0,0 +1,135 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Tests\ChoiceList\Factory; + +use Symfony\Component\Form\ChoiceList\ArrayChoiceList; +use Symfony\Component\Form\ChoiceList\Factory\FilteringFactoryDecorator; +use Symfony\Component\Form\ChoiceList\LazyChoiceList; + +/** + * @author Jules Pietri + */ +class FilteringFactoryDecoratorTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $decoratedFactory; + + /** + * @var FilteringFactoryDecorator + */ + private $factory; + + protected function setUp() + { + $this->decoratedFactory = $this->getMock('Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface'); + $this->factory = new FilteringFactoryDecorator($this->decoratedFactory); + } + + /** + * @dataProvider provideChoicesAndFilter + */ + public function testCreateFilteredChoiceListFromChoices($choices, $choiceList, $filter, $filteredChoices) + { + $this->decoratedFactory->expects($this->at(0)) + ->method('createListFromChoices') + ->with($choices) + ->willReturn($choiceList); + $this->decoratedFactory->expects($this->at(1)) + ->method('createListFromChoices') + ->with($filteredChoices) + ->willReturn('RESULT'); + + $this->assertSame('RESULT', $this->factory->createFilteredListFromChoices($choices, null, $filter)); + } + + /** + * @dataProvider provideChoicesAndFilter + */ + public function testCreateFilteredChoiceListFromLoader($choices, $choiceList, $filter, $filteredChoices) + { + $choiceLoader = $this->getMock('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface'); + $lazyChoiceList = new LazyChoiceList($choiceLoader); + + $choiceLoader->expects($this->once()) + ->method('loadChoiceList') + ->willReturn($choiceList); + + $this->decoratedFactory->expects($this->at(0)) + ->method('createListFromLoader') + ->with($choiceLoader) + ->willReturn($lazyChoiceList); + + $this->decoratedFactory->expects($this->at(1)) + ->method('createListFromChoices') + ->with($filteredChoices) + ->willReturn('RESULT'); + + $this->assertSame('RESULT', $this->factory->createFilteredListFromLoader($choiceLoader, null, $filter)); + } + + /** + * @dataProvider provideChoicesAndFilter + */ + public function testCreateFilteredChoiceListFromLoaderLoadsOriginalListOnFirstCall($choices, $choiceList, $filter, $filteredChoices) + { + $choiceLoader = $this->getMock('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface'); + $lazyChoiceList = new LazyChoiceList($choiceLoader); + + $choiceLoader->expects($this->once()) + ->method('loadChoiceList') + ->willReturn($choiceList); + + $this->decoratedFactory->expects($this->once()) + ->method('createListFromLoader') + ->with($choiceLoader) + ->willReturn($lazyChoiceList); + + $this->decoratedFactory->expects($this->at(1)) + ->method('createListFromChoices') + ->with($filteredChoices) + ->willReturn('RESULT'); + + $this->decoratedFactory->expects($this->at(2)) + ->method('createListFromChoices') + ->with($filteredChoices) + ->willReturn('RESULT'); + + $this->assertSame('RESULT', $this->factory->createFilteredListFromLoader($choiceLoader, null, $filter)); + $this->assertSame('RESULT', $this->factory->createFilteredListFromLoader($choiceLoader, null, $filter)); + } + + public function provideChoicesAndFilter() + { + return array( + 'Filtered by choices' => array( + array(range(1, 10)), + // Choice list from choices by values as string + new ArrayChoiceList(array_combine(range('1', '10'), range(1, 10))), + function ($choice) { + return 5 < $choice; + }, + // Filtered choices by values + array_combine(range('6', '10'), range(6, 10)), + ), + 'Filtered by values' => array( + array(range(1, 10)), + new ArrayChoiceList(array_combine(range('1', '10'), range(1, 10))), + function ($choice, $value) { + return '5' > $value; + }, + array_combine(range('1', '4'), range(1, 4)), + ), + ); + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy