]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/form/Symfony/Component/Form/AbstractExtension.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / form / Symfony / Component / Form / AbstractExtension.php
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;
13
14 use Symfony\Component\Form\Exception\InvalidArgumentException;
15 use Symfony\Component\Form\Exception\UnexpectedTypeException;
16
17 /**
18 * @author Bernhard Schussek <bschussek@gmail.com>
19 */
20 abstract class AbstractExtension implements FormExtensionInterface
21 {
22 /**
23 * The types provided by this extension
24 * @var FormTypeInterface[] An array of FormTypeInterface
25 */
26 private $types;
27
28 /**
29 * The type extensions provided by this extension
30 * @var FormTypeExtensionInterface[] An array of FormTypeExtensionInterface
31 */
32 private $typeExtensions;
33
34 /**
35 * The type guesser provided by this extension
36 * @var FormTypeGuesserInterface
37 */
38 private $typeGuesser;
39
40 /**
41 * Whether the type guesser has been loaded
42 * @var Boolean
43 */
44 private $typeGuesserLoaded = false;
45
46 /**
47 * {@inheritdoc}
48 */
49 public function getType($name)
50 {
51 if (null === $this->types) {
52 $this->initTypes();
53 }
54
55 if (!isset($this->types[$name])) {
56 throw new InvalidArgumentException(sprintf('The type "%s" can not be loaded by this extension', $name));
57 }
58
59 return $this->types[$name];
60 }
61
62 /**
63 * {@inheritdoc}
64 */
65 public function hasType($name)
66 {
67 if (null === $this->types) {
68 $this->initTypes();
69 }
70
71 return isset($this->types[$name]);
72 }
73
74 /**
75 * {@inheritdoc}
76 */
77 public function getTypeExtensions($name)
78 {
79 if (null === $this->typeExtensions) {
80 $this->initTypeExtensions();
81 }
82
83 return isset($this->typeExtensions[$name])
84 ? $this->typeExtensions[$name]
85 : array();
86 }
87
88 /**
89 * {@inheritdoc}
90 */
91 public function hasTypeExtensions($name)
92 {
93 if (null === $this->typeExtensions) {
94 $this->initTypeExtensions();
95 }
96
97 return isset($this->typeExtensions[$name]) && count($this->typeExtensions[$name]) > 0;
98 }
99
100 /**
101 * {@inheritdoc}
102 */
103 public function getTypeGuesser()
104 {
105 if (!$this->typeGuesserLoaded) {
106 $this->initTypeGuesser();
107 }
108
109 return $this->typeGuesser;
110 }
111
112 /**
113 * Registers the types.
114 *
115 * @return FormTypeInterface[] An array of FormTypeInterface instances
116 */
117 protected function loadTypes()
118 {
119 return array();
120 }
121
122 /**
123 * Registers the type extensions.
124 *
125 * @return FormTypeExtensionInterface[] An array of FormTypeExtensionInterface instances
126 */
127 protected function loadTypeExtensions()
128 {
129 return array();
130 }
131
132 /**
133 * Registers the type guesser.
134 *
135 * @return FormTypeGuesserInterface|null A type guesser
136 */
137 protected function loadTypeGuesser()
138 {
139 return null;
140 }
141
142 /**
143 * Initializes the types.
144 *
145 * @throws UnexpectedTypeException if any registered type is not an instance of FormTypeInterface
146 */
147 private function initTypes()
148 {
149 $this->types = array();
150
151 foreach ($this->loadTypes() as $type) {
152 if (!$type instanceof FormTypeInterface) {
153 throw new UnexpectedTypeException($type, 'Symfony\Component\Form\FormTypeInterface');
154 }
155
156 $this->types[$type->getName()] = $type;
157 }
158 }
159
160 /**
161 * Initializes the type extensions.
162 *
163 * @throws UnexpectedTypeException if any registered type extension is not
164 * an instance of FormTypeExtensionInterface
165 */
166 private function initTypeExtensions()
167 {
168 $this->typeExtensions = array();
169
170 foreach ($this->loadTypeExtensions() as $extension) {
171 if (!$extension instanceof FormTypeExtensionInterface) {
172 throw new UnexpectedTypeException($extension, 'Symfony\Component\Form\FormTypeExtensionInterface');
173 }
174
175 $type = $extension->getExtendedType();
176
177 $this->typeExtensions[$type][] = $extension;
178 }
179 }
180
181 /**
182 * Initializes the type guesser.
183 *
184 * @throws UnexpectedTypeException if the type guesser is not an instance of FormTypeGuesserInterface
185 */
186 private function initTypeGuesser()
187 {
188 $this->typeGuesserLoaded = true;
189
190 $this->typeGuesser = $this->loadTypeGuesser();
191 if (null !== $this->typeGuesser && !$this->typeGuesser instanceof FormTypeGuesserInterface) {
192 throw new UnexpectedTypeException($this->typeGuesser, 'Symfony\Component\Form\FormTypeGuesserInterface');
193 }
194 }
195 }