Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (danger)
50.00%
2 / 4
CRAP
81.82% covered (warning)
81.82%
9 / 11
CallbackChoiceLoader
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (danger)
50.00%
2 / 4
7.29
81.82% covered (warning)
81.82%
9 / 11
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 loadChoiceList
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 loadChoicesForValues
0.00% covered (danger)
0.00%
0 / 1
2.15
66.67% covered (warning)
66.67%
2 / 3
 loadValuesForChoices
0.00% covered (danger)
0.00%
0 / 1
2.15
66.67% covered (warning)
66.67%
2 / 3
<?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(callable $value = null)
    {
        if (null !== $this->choiceList) {
            return $this->choiceList;
        }
        return $this->choiceList = new ArrayChoiceList(($this->callback)(), $value);
    }
    /**
     * {@inheritdoc}
     */
    public function loadChoicesForValues(array $values, callable $value = null)
    {
        // Optimize
        if (empty($values)) {
            return [];
        }
        return $this->loadChoiceList($value)->getChoicesForValues($values);
    }
    /**
     * {@inheritdoc}
     */
    public function loadValuesForChoices(array $choices, callable $value = null)
    {
        // Optimize
        if (empty($choices)) {
            return [];
        }
        return $this->loadChoiceList($value)->getValuesForChoices($choices);
    }
}