Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 220 lines (188 sloc) 5.73 kB
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
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\FormException;
15 use Symfony\Component\Form\Exception\UnexpectedTypeException;
16
17 /**
18 * @author Bernhard Schussek <bernhard.schussek@symfony.com>
19 */
20 abstract class AbstractExtension implements FormExtensionInterface
21 {
22 /**
35d9e23 @vicb [Form] phpDoc
vicb authored
23 * The types provided by this extension
24 * @var array An array of FormTypeInterface
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
25 */
26 private $types;
27
28 /**
35d9e23 @vicb [Form] phpDoc
vicb authored
29 * The type extensions provided by this extension
30 * @var array An array of FormTypeExtensionInterface
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
31 */
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
32 private $typeExtensions;
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
33
34 /**
35d9e23 @vicb [Form] phpDoc
vicb authored
35 * The type guesser provided by this extension
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
36 * @var FormTypeGuesserInterface
37 */
38 private $typeGuesser;
39
40 /**
35d9e23 @vicb [Form] phpDoc
vicb authored
41 * Whether the type guesser has been loaded
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
42 * @var Boolean
43 */
44 private $typeGuesserLoaded = false;
45
35d9e23 @vicb [Form] phpDoc
vicb authored
46 /**
47 * Returns a type by name.
48 *
49 * @param string $name The name of the type
50 *
51 * @return FormTypeInterface The type
52 *
53 * @throws FormException if the given type is not supported by this extension
54 */
f7a4b75 @vicb [Form] Order method according to their visibility to respect the CS
vicb authored
55 public function getType($name)
56 {
57 if (null === $this->types) {
58 $this->initTypes();
59 }
60
61 if (!isset($this->types[$name])) {
62 throw new FormException(sprintf('The type "%s" can not be loaded by this extension', $name));
63 }
64
65 return $this->types[$name];
66 }
67
35d9e23 @vicb [Form] phpDoc
vicb authored
68 /**
69 * Returns whether the given type is supported.
70 *
71 * @param string $name The name of the type
72 *
73 * @return Boolean Whether the type is supported by this extension
74 */
f7a4b75 @vicb [Form] Order method according to their visibility to respect the CS
vicb authored
75 public function hasType($name)
76 {
77 if (null === $this->types) {
78 $this->initTypes();
79 }
80
81 return isset($this->types[$name]);
82 }
83
35d9e23 @vicb [Form] phpDoc
vicb authored
84 /**
85 * Returns the extensions for the given type.
86 *
87 * @param string $name The name of the type
88 *
89 * @return array An array of extensions as FormTypeExtensionInterface instances
90 */
f7a4b75 @vicb [Form] Order method according to their visibility to respect the CS
vicb authored
91 public function getTypeExtensions($name)
92 {
93 if (null === $this->typeExtensions) {
94 $this->initTypeExtensions();
95 }
96
97 return isset($this->typeExtensions[$name])
98 ? $this->typeExtensions[$name]
99 : array();
100 }
101
35d9e23 @vicb [Form] phpDoc
vicb authored
102 /**
103 * Returns whether this extension provides type extensions for the given type.
104 *
105 * @param string $name The name of the type
106 *
107 * @return Boolean Whether the given type has extensions
108 */
f7a4b75 @vicb [Form] Order method according to their visibility to respect the CS
vicb authored
109 public function hasTypeExtensions($name)
110 {
111 if (null === $this->typeExtensions) {
112 $this->initTypeExtensions();
113 }
114
115 return isset($this->typeExtensions[$name]) && count($this->typeExtensions[$name]) > 0;
116 }
117
35d9e23 @vicb [Form] phpDoc
vicb authored
118 /**
119 * Returns the type guesser provided by this extension.
120 *
121 * @return FormTypeGuesserInterface|null The type guesser
122 */
f7a4b75 @vicb [Form] Order method according to their visibility to respect the CS
vicb authored
123 public function getTypeGuesser()
124 {
125 if (!$this->typeGuesserLoaded) {
126 $this->initTypeGuesser();
127 }
128
129 return $this->typeGuesser;
130 }
131
35d9e23 @vicb [Form] phpDoc
vicb authored
132 /**
133 * Registers the types.
134 *
135 * @return array An array of FormTypeInterface instances
136 */
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
137 protected function loadTypes()
138 {
139 return array();
140 }
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
141
35d9e23 @vicb [Form] phpDoc
vicb authored
142 /**
143 * Registers the type extensions.
144 *
145 * @return array An array of FormTypeExtensionInterface instances
146 */
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
147 protected function loadTypeExtensions()
148 {
149 return array();
150 }
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
151
35d9e23 @vicb [Form] phpDoc
vicb authored
152 /**
153 * Registers the type guesser.
154 *
155 * @return FormTypeGuesserInterface|null A type guesser
156 */
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
157 protected function loadTypeGuesser()
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
158 {
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
159 return null;
160 }
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
161
35d9e23 @vicb [Form] phpDoc
vicb authored
162 /**
163 * Initializes the types.
164 *
165 * @throws UnexpectedTypeException if any registered type is not an instance of FormTypeInterface
166 */
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
167 private function initTypes()
168 {
35d9e23 @vicb [Form] phpDoc
vicb authored
169 $this->types = array();
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
170
35d9e23 @vicb [Form] phpDoc
vicb authored
171 foreach ($this->loadTypes() as $type) {
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
172 if (!$type instanceof FormTypeInterface) {
173 throw new UnexpectedTypeException($type, 'Symfony\Component\Form\FormTypeInterface');
174 }
175
35d9e23 @vicb [Form] phpDoc
vicb authored
176 $this->types[$type->getName()] = $type;
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
177 }
178 }
179
35d9e23 @vicb [Form] phpDoc
vicb authored
180 /**
181 * Initializes the type extensions.
182 *
183 * @throws UnexpectedTypeException if any registered type extension is not
184 * an instance of FormTypeExtensionInterface
185 */
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
186 private function initTypeExtensions()
187 {
35d9e23 @vicb [Form] phpDoc
vicb authored
188 $this->typeExtensions = array();
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
189
35d9e23 @vicb [Form] phpDoc
vicb authored
190 foreach ($this->loadTypeExtensions() as $extension) {
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
191 if (!$extension instanceof FormTypeExtensionInterface) {
192 throw new UnexpectedTypeException($extension, 'Symfony\Component\Form\FormTypeExtensionInterface');
193 }
194
195 $type = $extension->getExtendedType();
196
35d9e23 @vicb [Form] phpDoc
vicb authored
197 if (!isset($this->typeExtensions[$type])) {
198 $this->typeExtensions[$type] = array();
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
199 }
200
35d9e23 @vicb [Form] phpDoc
vicb authored
201 $this->typeExtensions[$type][] = $extension;
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
202 }
203 }
204
35d9e23 @vicb [Form] phpDoc
vicb authored
205 /**
206 * Initializes the type guesser.
207 *
208 * @throws UnexpectedTypeException if the type guesser is not an instance of FormTypeGuesserInterface
209 */
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
210 private function initTypeGuesser()
211 {
212 $this->typeGuesserLoaded = true;
213
9bb0eb8 @fabpot [Form] fixed typo (patch from mgatto)
fabpot authored
214 $this->typeGuesser = $this->loadTypeGuesser();
215 if (!$this->typeGuesser instanceof FormTypeGuesserInterface) {
216 throw new UnexpectedTypeException($this->typeGuesser, 'Symfony\Component\Form\FormTypeGuesserInterface');
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
217 }
218 }
66d0f1b @fabpot fixed CS
fabpot authored
219 }
Something went wrong with that request. Please try again.