diff options
Diffstat (limited to 'vendor/symfony/form/Symfony/Component/Form/Extension/Core/ChoiceList/LazyChoiceList.php')
-rw-r--r-- | vendor/symfony/form/Symfony/Component/Form/Extension/Core/ChoiceList/LazyChoiceList.php | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/vendor/symfony/form/Symfony/Component/Form/Extension/Core/ChoiceList/LazyChoiceList.php b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/ChoiceList/LazyChoiceList.php new file mode 100644 index 00000000..996f900c --- /dev/null +++ b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/ChoiceList/LazyChoiceList.php | |||
@@ -0,0 +1,149 @@ | |||
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 | |||
12 | namespace Symfony\Component\Form\Extension\Core\ChoiceList; | ||
13 | |||
14 | use Symfony\Component\Form\Exception\InvalidArgumentException; | ||
15 | |||
16 | /** | ||
17 | * A choice list that is loaded lazily | ||
18 | * | ||
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. | ||
22 | * | ||
23 | * @author Bernhard Schussek <bschussek@gmail.com> | ||
24 | */ | ||
25 | abstract class LazyChoiceList implements ChoiceListInterface | ||
26 | { | ||
27 | /** | ||
28 | * The loaded choice list | ||
29 | * | ||
30 | * @var ChoiceListInterface | ||
31 | */ | ||
32 | private $choiceList; | ||
33 | |||
34 | /** | ||
35 | * {@inheritdoc} | ||
36 | */ | ||
37 | public function getChoices() | ||
38 | { | ||
39 | if (!$this->choiceList) { | ||
40 | $this->load(); | ||
41 | } | ||
42 | |||
43 | return $this->choiceList->getChoices(); | ||
44 | } | ||
45 | |||
46 | /** | ||
47 | * {@inheritdoc} | ||
48 | */ | ||
49 | public function getValues() | ||
50 | { | ||
51 | if (!$this->choiceList) { | ||
52 | $this->load(); | ||
53 | } | ||
54 | |||
55 | return $this->choiceList->getValues(); | ||
56 | } | ||
57 | |||
58 | /** | ||
59 | * {@inheritdoc} | ||
60 | */ | ||
61 | public function getPreferredViews() | ||
62 | { | ||
63 | if (!$this->choiceList) { | ||
64 | $this->load(); | ||
65 | } | ||
66 | |||
67 | return $this->choiceList->getPreferredViews(); | ||
68 | } | ||
69 | |||
70 | /** | ||
71 | * {@inheritdoc} | ||
72 | */ | ||
73 | public function getRemainingViews() | ||
74 | { | ||
75 | if (!$this->choiceList) { | ||
76 | $this->load(); | ||
77 | } | ||
78 | |||
79 | return $this->choiceList->getRemainingViews(); | ||
80 | } | ||
81 | |||
82 | /** | ||
83 | * {@inheritdoc} | ||
84 | */ | ||
85 | public function getChoicesForValues(array $values) | ||
86 | { | ||
87 | if (!$this->choiceList) { | ||
88 | $this->load(); | ||
89 | } | ||
90 | |||
91 | return $this->choiceList->getChoicesForValues($values); | ||
92 | } | ||
93 | |||
94 | /** | ||
95 | * {@inheritdoc} | ||
96 | */ | ||
97 | public function getValuesForChoices(array $choices) | ||
98 | { | ||
99 | if (!$this->choiceList) { | ||
100 | $this->load(); | ||
101 | } | ||
102 | |||
103 | return $this->choiceList->getValuesForChoices($choices); | ||
104 | } | ||
105 | |||
106 | /** | ||
107 | * {@inheritdoc} | ||
108 | */ | ||
109 | public function getIndicesForChoices(array $choices) | ||
110 | { | ||
111 | if (!$this->choiceList) { | ||
112 | $this->load(); | ||
113 | } | ||
114 | |||
115 | return $this->choiceList->getIndicesForChoices($choices); | ||
116 | } | ||
117 | |||
118 | /** | ||
119 | * {@inheritdoc} | ||
120 | */ | ||
121 | public function getIndicesForValues(array $values) | ||
122 | { | ||
123 | if (!$this->choiceList) { | ||
124 | $this->load(); | ||
125 | } | ||
126 | |||
127 | return $this->choiceList->getIndicesForValues($values); | ||
128 | } | ||
129 | |||
130 | /** | ||
131 | * Loads the choice list | ||
132 | * | ||
133 | * Should be implemented by child classes. | ||
134 | * | ||
135 | * @return ChoiceListInterface The loaded choice list | ||
136 | */ | ||
137 | abstract protected function loadChoiceList(); | ||
138 | |||
139 | private function load() | ||
140 | { | ||
141 | $choiceList = $this->loadChoiceList(); | ||
142 | |||
143 | if (!$choiceList instanceof ChoiceListInterface) { | ||
144 | throw new InvalidArgumentException(sprintf('loadChoiceList() should return a ChoiceListInterface instance. Got %s', gettype($choiceList))); | ||
145 | } | ||
146 | |||
147 | $this->choiceList = $choiceList; | ||
148 | } | ||
149 | } | ||