diff options
Diffstat (limited to 'vendor/symfony/form/Symfony/Component/Form/Extension/Core/Type/TimeType.php')
-rw-r--r-- | vendor/symfony/form/Symfony/Component/Form/Extension/Core/Type/TimeType.php | 225 |
1 files changed, 225 insertions, 0 deletions
diff --git a/vendor/symfony/form/Symfony/Component/Form/Extension/Core/Type/TimeType.php b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/Type/TimeType.php new file mode 100644 index 00000000..d7a2a9ef --- /dev/null +++ b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/Type/TimeType.php | |||
@@ -0,0 +1,225 @@ | |||
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\Type; | ||
13 | |||
14 | use Symfony\Component\Form\AbstractType; | ||
15 | use Symfony\Component\Form\FormInterface; | ||
16 | use Symfony\Component\Form\FormBuilderInterface; | ||
17 | use Symfony\Component\Form\ReversedTransformer; | ||
18 | use Symfony\Component\Form\Exception\InvalidConfigurationException; | ||
19 | use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer; | ||
20 | use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToTimestampTransformer; | ||
21 | use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToArrayTransformer; | ||
22 | use Symfony\Component\Form\FormView; | ||
23 | use Symfony\Component\OptionsResolver\Options; | ||
24 | use Symfony\Component\OptionsResolver\OptionsResolverInterface; | ||
25 | |||
26 | class TimeType extends AbstractType | ||
27 | { | ||
28 | /** | ||
29 | * {@inheritdoc} | ||
30 | */ | ||
31 | public function buildForm(FormBuilderInterface $builder, array $options) | ||
32 | { | ||
33 | $parts = array('hour'); | ||
34 | $format = 'H'; | ||
35 | |||
36 | if ($options['with_seconds'] && !$options['with_minutes']) { | ||
37 | throw new InvalidConfigurationException('You can not disable minutes if you have enabled seconds.'); | ||
38 | } | ||
39 | |||
40 | if ($options['with_minutes']) { | ||
41 | $format .= ':i'; | ||
42 | $parts[] = 'minute'; | ||
43 | } | ||
44 | |||
45 | if ($options['with_seconds']) { | ||
46 | $format .= ':s'; | ||
47 | $parts[] = 'second'; | ||
48 | } | ||
49 | |||
50 | if ('single_text' === $options['widget']) { | ||
51 | $builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format)); | ||
52 | } else { | ||
53 | $hourOptions = $minuteOptions = $secondOptions = array( | ||
54 | 'error_bubbling' => true, | ||
55 | ); | ||
56 | |||
57 | if ('choice' === $options['widget']) { | ||
58 | $hours = $minutes = array(); | ||
59 | |||
60 | foreach ($options['hours'] as $hour) { | ||
61 | $hours[$hour] = str_pad($hour, 2, '0', STR_PAD_LEFT); | ||
62 | } | ||
63 | |||
64 | // Only pass a subset of the options to children | ||
65 | $hourOptions['choices'] = $hours; | ||
66 | $hourOptions['empty_value'] = $options['empty_value']['hour']; | ||
67 | |||
68 | if ($options['with_minutes']) { | ||
69 | foreach ($options['minutes'] as $minute) { | ||
70 | $minutes[$minute] = str_pad($minute, 2, '0', STR_PAD_LEFT); | ||
71 | } | ||
72 | |||
73 | $minuteOptions['choices'] = $minutes; | ||
74 | $minuteOptions['empty_value'] = $options['empty_value']['minute']; | ||
75 | } | ||
76 | |||
77 | if ($options['with_seconds']) { | ||
78 | $seconds = array(); | ||
79 | |||
80 | foreach ($options['seconds'] as $second) { | ||
81 | $seconds[$second] = str_pad($second, 2, '0', STR_PAD_LEFT); | ||
82 | } | ||
83 | |||
84 | $secondOptions['choices'] = $seconds; | ||
85 | $secondOptions['empty_value'] = $options['empty_value']['second']; | ||
86 | } | ||
87 | |||
88 | // Append generic carry-along options | ||
89 | foreach (array('required', 'translation_domain') as $passOpt) { | ||
90 | $hourOptions[$passOpt] = $options[$passOpt]; | ||
91 | |||
92 | if ($options['with_minutes']) { | ||
93 | $minuteOptions[$passOpt] = $options[$passOpt]; | ||
94 | } | ||
95 | |||
96 | if ($options['with_seconds']) { | ||
97 | $secondOptions[$passOpt] = $options[$passOpt]; | ||
98 | } | ||
99 | } | ||
100 | } | ||
101 | |||
102 | $builder->add('hour', $options['widget'], $hourOptions); | ||
103 | |||
104 | if ($options['with_minutes']) { | ||
105 | $builder->add('minute', $options['widget'], $minuteOptions); | ||
106 | } | ||
107 | |||
108 | if ($options['with_seconds']) { | ||
109 | $builder->add('second', $options['widget'], $secondOptions); | ||
110 | } | ||
111 | |||
112 | $builder->addViewTransformer(new DateTimeToArrayTransformer($options['model_timezone'], $options['view_timezone'], $parts, 'text' === $options['widget'])); | ||
113 | } | ||
114 | |||
115 | if ('string' === $options['input']) { | ||
116 | $builder->addModelTransformer(new ReversedTransformer( | ||
117 | new DateTimeToStringTransformer($options['model_timezone'], $options['model_timezone'], 'H:i:s') | ||
118 | )); | ||
119 | } elseif ('timestamp' === $options['input']) { | ||
120 | $builder->addModelTransformer(new ReversedTransformer( | ||
121 | new DateTimeToTimestampTransformer($options['model_timezone'], $options['model_timezone']) | ||
122 | )); | ||
123 | } elseif ('array' === $options['input']) { | ||
124 | $builder->addModelTransformer(new ReversedTransformer( | ||
125 | new DateTimeToArrayTransformer($options['model_timezone'], $options['model_timezone'], $parts) | ||
126 | )); | ||
127 | } | ||
128 | } | ||
129 | |||
130 | /** | ||
131 | * {@inheritdoc} | ||
132 | */ | ||
133 | public function buildView(FormView $view, FormInterface $form, array $options) | ||
134 | { | ||
135 | $view->vars = array_replace($view->vars, array( | ||
136 | 'widget' => $options['widget'], | ||
137 | 'with_minutes' => $options['with_minutes'], | ||
138 | 'with_seconds' => $options['with_seconds'], | ||
139 | )); | ||
140 | |||
141 | if ('single_text' === $options['widget']) { | ||
142 | $view->vars['type'] = 'time'; | ||
143 | } | ||
144 | } | ||
145 | |||
146 | /** | ||
147 | * {@inheritdoc} | ||
148 | */ | ||
149 | public function setDefaultOptions(OptionsResolverInterface $resolver) | ||
150 | { | ||
151 | $compound = function (Options $options) { | ||
152 | return $options['widget'] !== 'single_text'; | ||
153 | }; | ||
154 | |||
155 | $emptyValue = $emptyValueDefault = function (Options $options) { | ||
156 | return $options['required'] ? null : ''; | ||
157 | }; | ||
158 | |||
159 | $emptyValueNormalizer = function (Options $options, $emptyValue) use ($emptyValueDefault) { | ||
160 | if (is_array($emptyValue)) { | ||
161 | $default = $emptyValueDefault($options); | ||
162 | |||
163 | return array_merge( | ||
164 | array('hour' => $default, 'minute' => $default, 'second' => $default), | ||
165 | $emptyValue | ||
166 | ); | ||
167 | } | ||
168 | |||
169 | return array( | ||
170 | 'hour' => $emptyValue, | ||
171 | 'minute' => $emptyValue, | ||
172 | 'second' => $emptyValue | ||
173 | ); | ||
174 | }; | ||
175 | |||
176 | $resolver->setDefaults(array( | ||
177 | 'hours' => range(0, 23), | ||
178 | 'minutes' => range(0, 59), | ||
179 | 'seconds' => range(0, 59), | ||
180 | 'widget' => 'choice', | ||
181 | 'input' => 'datetime', | ||
182 | 'with_minutes' => true, | ||
183 | 'with_seconds' => false, | ||
184 | 'model_timezone' => null, | ||
185 | 'view_timezone' => null, | ||
186 | 'empty_value' => $emptyValue, | ||
187 | // Don't modify \DateTime classes by reference, we treat | ||
188 | // them like immutable value objects | ||
189 | 'by_reference' => false, | ||
190 | 'error_bubbling' => false, | ||
191 | // If initialized with a \DateTime object, FormType initializes | ||
192 | // this option to "\DateTime". Since the internal, normalized | ||
193 | // representation is not \DateTime, but an array, we need to unset | ||
194 | // this option. | ||
195 | 'data_class' => null, | ||
196 | 'compound' => $compound, | ||
197 | )); | ||
198 | |||
199 | $resolver->setNormalizers(array( | ||
200 | 'empty_value' => $emptyValueNormalizer, | ||
201 | )); | ||
202 | |||
203 | $resolver->setAllowedValues(array( | ||
204 | 'input' => array( | ||
205 | 'datetime', | ||
206 | 'string', | ||
207 | 'timestamp', | ||
208 | 'array', | ||
209 | ), | ||
210 | 'widget' => array( | ||
211 | 'single_text', | ||
212 | 'text', | ||
213 | 'choice', | ||
214 | ), | ||
215 | )); | ||
216 | } | ||
217 | |||
218 | /** | ||
219 | * {@inheritdoc} | ||
220 | */ | ||
221 | public function getName() | ||
222 | { | ||
223 | return 'time'; | ||
224 | } | ||
225 | } | ||