4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Extension\Core\ChoiceList
;
14 use Symfony\Component\Form\Exception\InvalidArgumentException
;
17 * A choice list that is loaded lazily
19 * This list loads itself as soon as any of the getters is accessed for the
20 * first time. You should implement loadChoiceList() in your child classes,
21 * which should return a ChoiceListInterface instance.
23 * @author Bernhard Schussek <bschussek@gmail.com>
25 abstract class LazyChoiceList
implements ChoiceListInterface
28 * The loaded choice list
30 * @var ChoiceListInterface
37 public function getChoices()
39 if (!$this->choiceList
) {
43 return $this->choiceList
->getChoices();
49 public function getValues()
51 if (!$this->choiceList
) {
55 return $this->choiceList
->getValues();
61 public function getPreferredViews()
63 if (!$this->choiceList
) {
67 return $this->choiceList
->getPreferredViews();
73 public function getRemainingViews()
75 if (!$this->choiceList
) {
79 return $this->choiceList
->getRemainingViews();
85 public function getChoicesForValues(array $values)
87 if (!$this->choiceList
) {
91 return $this->choiceList
->getChoicesForValues($values);
97 public function getValuesForChoices(array $choices)
99 if (!$this->choiceList
) {
103 return $this->choiceList
->getValuesForChoices($choices);
109 public function getIndicesForChoices(array $choices)
111 if (!$this->choiceList
) {
115 return $this->choiceList
->getIndicesForChoices($choices);
121 public function getIndicesForValues(array $values)
123 if (!$this->choiceList
) {
127 return $this->choiceList
->getIndicesForValues($values);
131 * Loads the choice list
133 * Should be implemented by child classes.
135 * @return ChoiceListInterface The loaded choice list
137 abstract protected function loadChoiceList();
139 private function load()
141 $choiceList = $this->loadChoiceList();
143 if (!$choiceList instanceof ChoiceListInterface
) {
144 throw new InvalidArgumentException(sprintf('loadChoiceList() should return a ChoiceListInterface instance. Got %s', gettype($choiceList)));
147 $this->choiceList
= $choiceList;