Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
83.33% covered (warning)
83.33%
5 / 6
CRAP
99.22% covered (success)
99.22%
128 / 129
DefaultChoiceListFactory
0.00% covered (danger)
0.00%
0 / 1
83.33% covered (warning)
83.33%
5 / 6
42
99.22% covered (success)
99.22%
128 / 129
 createListFromChoices
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
7 / 7
 createListFromLoader
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
4 / 4
 createView
0.00% covered (danger)
0.00%
0 / 1
15
97.22% covered (success)
97.22%
35 / 36
 addChoiceView
100.00% covered (success)
100.00%
1 / 1
9
100.00% covered (success)
100.00%
15 / 15
 addChoiceViewsFromStructuredValues
100.00% covered (success)
100.00%
1 / 1
6
100.00% covered (success)
100.00%
34 / 34
 addChoiceViewsGroupedByCallable
100.00% covered (success)
100.00%
1 / 1
6
100.00% covered (success)
100.00%
33 / 33
1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Form\ChoiceList\Factory;
13
14use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
15use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
16use Symfony\Component\Form\ChoiceList\LazyChoiceList;
17use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
18use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
19use Symfony\Component\Form\ChoiceList\Loader\FilterChoiceLoaderDecorator;
20use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
21use Symfony\Component\Form\ChoiceList\View\ChoiceListView;
22use Symfony\Component\Form\ChoiceList\View\ChoiceView;
23
24/**
25 * Default implementation of {@link ChoiceListFactoryInterface}.
26 *
27 * @author Bernhard Schussek <bschussek@gmail.com>
28 * @author Jules Pietri <jules@heahprod.com>
29 */
30class DefaultChoiceListFactory implements ChoiceListFactoryInterface
31{
32    /**
33     * {@inheritdoc}
34     *
35     * @param callable|null $filter
36     */
37    public function createListFromChoices(iterable $choices, callable $value = null/*, callable $filter = null*/)
38    {
39        $filter = \func_num_args() > 2 ? func_get_arg(2) : null;
40
41        if ($filter) {
42            // filter the choice list lazily
43            return $this->createListFromLoader(new FilterChoiceLoaderDecorator(
44                new CallbackChoiceLoader(static function () use ($choices) {
45                    return $choices;
46                }
47            ), $filter), $value);
48        }
49
50        return new ArrayChoiceList($choices, $value);
51    }
52
53    /**
54     * {@inheritdoc}
55     *
56     * @param callable|null $filter
57     */
58    public function createListFromLoader(ChoiceLoaderInterface $loader, callable $value = null/*, callable $filter = null*/)
59    {
60        $filter = \func_num_args() > 2 ? func_get_arg(2) : null;
61
62        if ($filter) {
63            $loader = new FilterChoiceLoaderDecorator($loader, $filter);
64        }
65
66        return new LazyChoiceList($loader, $value);
67    }
68
69    /**
70     * {@inheritdoc}
71     */
72    public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, callable $index = null, callable $groupBy = null, $attr = null)
73    {
74        $preferredViews = [];
75        $preferredViewsOrder = [];
76        $otherViews = [];