]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
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 | /** | |
15 | * The default implementation of FormFactoryBuilderInterface. | |
16 | * | |
17 | * @author Bernhard Schussek <bschussek@gmail.com> | |
18 | */ | |
19 | class FormFactoryBuilder implements FormFactoryBuilderInterface | |
20 | { | |
21 | /** | |
22 | * @var ResolvedFormTypeFactoryInterface | |
23 | */ | |
24 | private $resolvedTypeFactory; | |
25 | ||
26 | /** | |
27 | * @var array | |
28 | */ | |
29 | private $extensions = array(); | |
30 | ||
31 | /** | |
32 | * @var array | |
33 | */ | |
34 | private $types = array(); | |
35 | ||
36 | /** | |
37 | * @var array | |
38 | */ | |
39 | private $typeExtensions = array(); | |
40 | ||
41 | /** | |
42 | * @var array | |
43 | */ | |
44 | private $typeGuessers = array(); | |
45 | ||
46 | /** | |
47 | * {@inheritdoc} | |
48 | */ | |
49 | public function setResolvedTypeFactory(ResolvedFormTypeFactoryInterface $resolvedTypeFactory) | |
50 | { | |
51 | $this->resolvedTypeFactory = $resolvedTypeFactory; | |
52 | ||
53 | return $this; | |
54 | } | |
55 | ||
56 | /** | |
57 | * {@inheritdoc} | |
58 | */ | |
59 | public function addExtension(FormExtensionInterface $extension) | |
60 | { | |
61 | $this->extensions[] = $extension; | |
62 | ||
63 | return $this; | |
64 | } | |
65 | ||
66 | /** | |
67 | * {@inheritdoc} | |
68 | */ | |
69 | public function addExtensions(array $extensions) | |
70 | { | |
71 | $this->extensions = array_merge($this->extensions, $extensions); | |
72 | ||
73 | return $this; | |
74 | } | |
75 | ||
76 | /** | |
77 | * {@inheritdoc} | |
78 | */ | |
79 | public function addType(FormTypeInterface $type) | |
80 | { | |
81 | $this->types[$type->getName()] = $type; | |
82 | ||
83 | return $this; | |
84 | } | |
85 | ||
86 | /** | |
87 | * {@inheritdoc} | |
88 | */ | |
89 | public function addTypes(array $types) | |
90 | { | |
91 | foreach ($types as $type) { | |
92 | $this->types[$type->getName()] = $type; | |
93 | } | |
94 | ||
95 | return $this; | |
96 | } | |
97 | ||
98 | /** | |
99 | * {@inheritdoc} | |
100 | */ | |
101 | public function addTypeExtension(FormTypeExtensionInterface $typeExtension) | |
102 | { | |
103 | $this->typeExtensions[$typeExtension->getExtendedType()][] = $typeExtension; | |
104 | ||
105 | return $this; | |
106 | } | |
107 | ||
108 | /** | |
109 | * {@inheritdoc} | |
110 | */ | |
111 | public function addTypeExtensions(array $typeExtensions) | |
112 | { | |
113 | foreach ($typeExtensions as $typeExtension) { | |
114 | $this->typeExtensions[$typeExtension->getExtendedType()][] = $typeExtension; | |
115 | } | |
116 | ||
117 | return $this; | |
118 | } | |
119 | ||
120 | /** | |
121 | * {@inheritdoc} | |
122 | */ | |
123 | public function addTypeGuesser(FormTypeGuesserInterface $typeGuesser) | |
124 | { | |
125 | $this->typeGuessers[] = $typeGuesser; | |
126 | ||
127 | return $this; | |
128 | } | |
129 | ||
130 | /** | |
131 | * {@inheritdoc} | |
132 | */ | |
133 | public function addTypeGuessers(array $typeGuessers) | |
134 | { | |
135 | $this->typeGuessers = array_merge($this->typeGuessers, $typeGuessers); | |
136 | ||
137 | return $this; | |
138 | } | |
139 | ||
140 | /** | |
141 | * {@inheritdoc} | |
142 | */ | |
143 | public function getFormFactory() | |
144 | { | |
145 | $extensions = $this->extensions; | |
146 | ||
147 | if (count($this->types) > 0 || count($this->typeExtensions) > 0 || count($this->typeGuessers) > 0) { | |
148 | if (count($this->typeGuessers) > 1) { | |
149 | $typeGuesser = new FormTypeGuesserChain($this->typeGuessers); | |
150 | } else { | |
151 | $typeGuesser = isset($this->typeGuessers[0]) ? $this->typeGuessers[0] : null; | |
152 | } | |
153 | ||
154 | $extensions[] = new PreloadedExtension($this->types, $this->typeExtensions, $typeGuesser); | |
155 | } | |
156 | ||
157 | $resolvedTypeFactory = $this->resolvedTypeFactory ?: new ResolvedFormTypeFactory(); | |
158 | $registry = new FormRegistry($extensions, $resolvedTypeFactory); | |
159 | ||
160 | return new FormFactory($registry, $resolvedTypeFactory); | |
161 | } | |
162 | } |