Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 196 lines (165 sloc) 4.799 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 /**
fae4396 @Tobion [Form] fix PhpDoc
Tobion authored
47 * {@inheritdoc}
35d9e23 @vicb [Form] phpDoc
vicb authored
48 */
f7a4b75 @vicb [Form] Order method according to their visibility to respect the CS
vicb authored
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 FormException(sprintf('The type "%s" can not be loaded by this extension', $name));
57 }
58
59 return $this->types[$name];
60 }
61
35d9e23 @vicb [Form] phpDoc
vicb authored
62 /**
fae4396 @Tobion [Form] fix PhpDoc
Tobion authored
63 * {@inheritdoc}
35d9e23 @vicb [Form] phpDoc
vicb authored
64 */
f7a4b75 @vicb [Form] Order method according to their visibility to respect the CS
vicb authored
65 public function hasType($name)
66 {
67 if (null === $this->types) {
68 $this->initTypes();
69 }
70
71 return isset($this->types[$name]);
72 }
73
35d9e23 @vicb [Form] phpDoc
vicb authored
74 /**
fae4396 @Tobion [Form] fix PhpDoc
Tobion authored
75 * {@inheritdoc}
35d9e23 @vicb [Form] phpDoc
vicb authored
76 */
f7a4b75 @vicb [Form] Order method according to their visibility to respect the CS
vicb authored
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
35d9e23 @vicb [Form] phpDoc
vicb authored
88 /**
fae4396 @Tobion [Form] fix PhpDoc
Tobion authored
89 * {@inheritdoc}
35d9e23 @vicb [Form] phpDoc
vicb authored
90 */
f7a4b75 @vicb [Form] Order method according to their visibility to respect the CS
vicb authored
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
35d9e23 @vicb [Form] phpDoc
vicb authored
100 /**
fae4396 @Tobion [Form] fix PhpDoc
Tobion authored
101 * {@inheritdoc}
35d9e23 @vicb [Form] phpDoc
vicb authored
102 */
f7a4b75 @vicb [Form] Order method according to their visibility to respect the CS
vicb authored
103 public function getTypeGuesser()
104 {
105 if (!$this->typeGuesserLoaded) {
106 $this->initTypeGuesser();
107 }
108
109 return $this->typeGuesser;
110 }
111
35d9e23 @vicb [Form] phpDoc
vicb authored
112 /**
113 * Registers the types.
114 *
115 * @return array An array of FormTypeInterface instances
116 */
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
117 protected function loadTypes()
118 {
119 return array();
120 }
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
121
35d9e23 @vicb [Form] phpDoc
vicb authored
122 /**
123 * Registers the type extensions.
124 *
125 * @return array An array of FormTypeExtensionInterface instances
126 */
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
127 protected function loadTypeExtensions()
128 {
129 return array();
130 }
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
131
35d9e23 @vicb [Form] phpDoc
vicb authored
132 /**
133 * Registers the type guesser.
134 *
135 * @return FormTypeGuesserInterface|null A type guesser
136 */
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
137 protected function loadTypeGuesser()
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
138 {
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
139 return null;
140 }
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
141
35d9e23 @vicb [Form] phpDoc
vicb authored
142 /**
143 * Initializes the types.
144 *
145 * @throws UnexpectedTypeException if any registered type is not an instance of FormTypeInterface
146 */
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
147 private function initTypes()
148 {
35d9e23 @vicb [Form] phpDoc
vicb authored
149 $this->types = array();
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
150
35d9e23 @vicb [Form] phpDoc
vicb authored
151 foreach ($this->loadTypes() as $type) {
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
152 if (!$type instanceof FormTypeInterface) {
153 throw new UnexpectedTypeException($type, 'Symfony\Component\Form\FormTypeInterface');
154 }
155
35d9e23 @vicb [Form] phpDoc
vicb authored
156 $this->types[$type->getName()] = $type;
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
157 }
158 }
159
35d9e23 @vicb [Form] phpDoc
vicb authored
160 /**
161 * Initializes the type extensions.
162 *
163 * @throws UnexpectedTypeException if any registered type extension is not
164 * an instance of FormTypeExtensionInterface
165 */
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
166 private function initTypeExtensions()
167 {
35d9e23 @vicb [Form] phpDoc
vicb authored
168 $this->typeExtensions = array();
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
169
35d9e23 @vicb [Form] phpDoc
vicb authored
170 foreach ($this->loadTypeExtensions() as $extension) {
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
171 if (!$extension instanceof FormTypeExtensionInterface) {
172 throw new UnexpectedTypeException($extension, 'Symfony\Component\Form\FormTypeExtensionInterface');
173 }
174
175 $type = $extension->getExtendedType();
176
35d9e23 @vicb [Form] phpDoc
vicb authored
177 $this->typeExtensions[$type][] = $extension;
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
178 }
179 }
180
35d9e23 @vicb [Form] phpDoc
vicb authored
181 /**
182 * Initializes the type guesser.
183 *
184 * @throws UnexpectedTypeException if the type guesser is not an instance of FormTypeGuesserInterface
185 */
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
186 private function initTypeGuesser()
187 {
188 $this->typeGuesserLoaded = true;
189
9bb0eb8 @fabpot [Form] fixed typo (patch from mgatto)
fabpot authored
190 $this->typeGuesser = $this->loadTypeGuesser();
ae0988b @fabpot [Form] fixed AbstractExtension when an extension does not have a guesser
fabpot authored
191 if (null !== $this->typeGuesser && !$this->typeGuesser instanceof FormTypeGuesserInterface) {
9bb0eb8 @fabpot [Form] fixed typo (patch from mgatto)
fabpot authored
192 throw new UnexpectedTypeException($this->typeGuesser, 'Symfony\Component\Form\FormTypeGuesserInterface');
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
193 }
194 }
66d0f1b @fabpot fixed CS
fabpot authored
195 }
Something went wrong with that request. Please try again.