Skip to content

[Form] Add FilterChoiceLoader + choice_filter option #28624

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ CHANGELOG
option is set to `single_text`
* added `block_prefix` option to `BaseType`.
* added `help_html` option to display the `help` text as HTML.
* added `FilterChoiceLoader`
* added `choice_filter` option to `ChoiceType`

4.2.0
-----
Expand Down
100 changes: 100 additions & 0 deletions src/Symfony/Component/Form/ChoiceList/Loader/FilterChoiceLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?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 Roland Franssen <franssen.roland@gmail.com>
*/
class FilterChoiceLoader implements ChoiceLoaderInterface
{
private $loader;
private $filter;
private $choiceList;

public function __construct(ChoiceLoaderInterface $loader, callable $filter)
{
$this->loader = $loader;
$this->filter = $filter;
}

/**
* {@inheritdoc}
*/
public function loadChoiceList($value = null)
{
if (null !== $this->choiceList) {
return $this->choiceList;
}

$choiceList = $this->loader->loadChoiceList($value);
$structured = $choiceList->getStructuredValues();
$choices = $choiceList->getChoices();
$visitor = function (array $list) use ($choices, &$visitor) {
foreach ($list as $k => $v) {
if (\is_array($v)) {
if ($v = $visitor($v)) {
$list[$k] = $v;
} else {
unset($list[$k]);
}
continue;
}

$choice = $choices[$v] ?? $v;
if (!\call_user_func($this->filter, $choice)) {
unset($list[$k]);
continue;
}

$list[$k] = $choice;
}

return $list;
};

return $this->choiceList = new ArrayChoiceList($visitor($structured), $value);
}

/**
* {@inheritdoc}
*/
public function loadChoicesForValues(array $values, $value = null)
{
// Optimize
if (empty($values)) {
return [];
}
if (null !== $this->choiceList) {
return $this->choiceList->getChoicesForValues($values);
}

return array_filter($this->loader->loadChoicesForValues($values, $value), $this->filter);
}

/**
* {@inheritdoc}
*/
public function loadValuesForChoices(array $choices, $value = null)
{
// Optimize
if (empty($choices)) {
return [];
}
if (null !== $this->choiceList) {
return $this->choiceList->getValuesForChoices($choices);
}

return $this->loader->loadValuesForChoices(array_filter($choices, $this->filter), $value);
}
}
14 changes: 13 additions & 1 deletion src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface;
use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory;
use Symfony\Component\Form\ChoiceList\Factory\PropertyAccessDecorator;
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
use Symfony\Component\Form\ChoiceList\Loader\FilterChoiceLoader;
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
use Symfony\Component\Form\ChoiceList\View\ChoiceListView;
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
Expand Down Expand Up @@ -305,6 +307,7 @@ public function configureOptions(OptionsResolver $resolver)
'choice_name' => null,
'choice_value' => null,
'choice_attr' => null,
'choice_filter' => null,
'preferred_choices' => [],
'group_by' => null,
'empty_data' => $emptyData,
Expand All @@ -329,6 +332,7 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setAllowedTypes('choice_name', ['null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath']);
$resolver->setAllowedTypes('choice_value', ['null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath']);
$resolver->setAllowedTypes('choice_attr', ['null', 'array', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath']);
$resolver->setAllowedTypes('choice_filter', ['null', 'callable']);
$resolver->setAllowedTypes('preferred_choices', ['array', '\Traversable', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath']);
$resolver->setAllowedTypes('group_by', ['null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath']);
}
Expand Down Expand Up @@ -391,14 +395,22 @@ private function createChoiceList(array $options)
{
if (null !== $options['choice_loader']) {
return $this->choiceListFactory->createListFromLoader(
$options['choice_loader'],
null === $options['choice_filter'] ? $options['choice_loader'] : new FilterChoiceLoader($options['choice_loader'], $options['choice_filter']),
$options['choice_value']
);
}

// Harden against NULL values (like in EntityType and ModelType)
$choices = null !== $options['choices'] ? $options['choices'] : [];

if (null !== $options['choice_filter']) {
$loader = new FilterChoiceLoader(new CallbackChoiceLoader(function () use ($choices) {
return $choices;
}), $options['choice_filter']);

return $this->choiceListFactory->createListFromLoader($loader, $options['choice_value']);
}

return $this->choiceListFactory->createListFromChoices($choices, $options['choice_value']);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?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\Tests\ChoiceList\Loader;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\ChoiceList\LazyChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
use Symfony\Component\Form\ChoiceList\Loader\FilterChoiceLoader;

/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
class FilterChoiceLoaderTest extends TestCase
{
/**
* @var callable
*/
private static $value;

/**
* @var array
*/
private static $choices;

/**
* @var string[]
*/
private static $choiceValues;

public static function setUpBeforeClass()
{
self::$value = function (\stdClass $choice) {
return $choice->value;
};
self::$choices = [
(object) ['value' => 'choice_one'],
(object) ['value' => 'choice_two'],
(object) ['value' => 'choice_three'],
(object) ['value' => 'choice_four'],
];
self::$choiceValues = ['choice_one', 'choice_two', 'choice_three', 'choice_four'];
}

public function testLoadChoiceList()
{
$loader = $this->createLoader();

$this->assertInstanceOf(ChoiceListInterface::class, $loader->loadChoiceList(self::$value));
}

public function testLoadChoiceListOnlyOnce()
{
$loader = $this->createLoader();
$loadedChoiceList = $loader->loadChoiceList(self::$value);

$this->assertSame($loadedChoiceList, $loader->loadChoiceList(self::$value));
}

public function testLoadChoicesForValuesLoadsChoiceListOnFirstCall()
{
$loader = $this->createLoader();
$lazyList = new LazyChoiceList($loader, self::$value);

$this->assertSame(
$loader->loadChoicesForValues(self::$choiceValues, self::$value),
$lazyList->getChoicesForValues(self::$choiceValues),
'Choice list should not be reloaded.'
);
}

public function testLoadValuesForChoicesLoadsChoiceListOnFirstCall()
{
$loader = $this->createLoader();
$lazyList = new LazyChoiceList($loader, self::$value);

$this->assertSame(
$loader->loadValuesForChoices(self::$choices, self::$value),
$lazyList->getValuesForChoices(self::$choices),
'Choice list should not be reloaded.'
);
}

public function testLoadChoiceListFilters()
{
$choiceList = $this->createLoader()->loadChoiceList();

$this->assertSame([self::$choices[2], self::$choices[3]], $choiceList->getChoices());
$this->assertSame(['0', '1'], $choiceList->getValues());
$this->assertSame([1, 'key'], $choiceList->getOriginalKeys());
$this->assertSame([1 => '0', 'Group' => ['key' => '1']], $choiceList->getStructuredValues());
$this->assertSame([self::$choices[3]], $choiceList->getChoicesForValues(['1', '2']));
$this->assertSame([], $choiceList->getChoicesForValues(['foo']));
$this->assertSame([1 => '0'], $choiceList->getValuesForChoices([self::$choices[1], self::$choices[2]]));
$this->assertSame([], $choiceList->getValuesForChoices(['foo']));
}

public function testLoadChoicesForValuesFilters()
{
$loader = $this->createLoader();
$choices = $loader->loadChoicesForValues(['choice_one', 'choice_three'], self::$value);

$this->assertSame([1 => self::$choices[2]], $choices);
}

public function testLoadValuesForChoicesFilters()
{
$loader = $this->createLoader();
$values = $loader->loadValuesForChoices([
self::$choices[0],
self::$choices[2],
], self::$value);

$this->assertSame([1 => 'choice_three'], $values);
}

public static function tearDownAfterClass()
{
self::$value = null;
self::$choices = [];
self::$choiceValues = [];
}

private function createLoader(): FilterChoiceLoader
{
return new FilterChoiceLoader(new CallbackChoiceLoader(function () {
return [
self::$choices[0],
self::$choices[2],
'Group' => [
self::$choices[1],
'key' => self::$choices[3],
],
];
}), function (\stdClass $choice) {
return \in_array($choice->value, ['choice_three', 'choice_four'], true);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Form\Tests\Extension\Core\Type;

use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
use Symfony\Component\Form\ChoiceList\View\ChoiceView;

Expand Down Expand Up @@ -2053,4 +2054,54 @@ public function provideTrimCases()
'Multiple expanded' => [true, true],
];
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testChoiceFilterOptionExpectsCallable()
{
$this->factory->create(static::TESTED_TYPE, null, [
'choice_filter' => new \stdClass(),
]);
}

public function testClosureChoiceFilterOptionWithChoiceLoaderOption()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
// defined by superclass
'choice_loader' => new CallbackChoiceLoader(function () {
return $this->choices;
}),
// defined by subclass or userland
'choice_filter' => function ($choice) {
return \in_array($choice, ['b', 'c'], true);
},
]);

$options = [];
foreach ($form->createView()->vars['choices'] as $choiceView) {
$options[$choiceView->label] = $choiceView->value;
}

$this->assertSame(['Fabien' => 'b', 'Kris' => 'c'], $options);
}

public function testChoiceFilterOptionWithChoicesOption()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
// defined by superclass
'choices' => $this->choices,
// defined by subclass or userland
'choice_filter' => function ($choice) {
return \in_array($choice, ['b', 'c'], true);
},
]);

$options = [];
foreach ($form->createView()->vars['choices'] as $choiceView) {
$options[$choiceView->label] = $choiceView->value;
}

$this->assertSame(['Fabien' => 'b', 'Kris' => 'c'], $options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"options": {
"own": [
"choice_attr",
"choice_filter",
"choice_label",
"choice_loader",
"choice_name",
Expand Down
Loading