|
| 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\Extension\Core\DataTransformer\DateTimeImmutableToDateTimeTransformer; |
| 16 | +use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToArrayTransformer; |
| 17 | +use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer; |
| 18 | +use Symfony\Component\Form\FormBuilderInterface; |
| 19 | +use Symfony\Component\Form\FormInterface; |
| 20 | +use Symfony\Component\Form\FormView; |
| 21 | +use Symfony\Component\Form\ReversedTransformer; |
| 22 | +use Symfony\Component\OptionsResolver\Options; |
| 23 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 24 | + |
| 25 | +class WeekType extends AbstractType |
| 26 | +{ |
| 27 | + private static $widgets = [ |
| 28 | + 'text' => TextType::class, |
| 29 | + 'choice' => ChoiceType::class, |
| 30 | + ]; |
| 31 | + |
| 32 | + /** |
| 33 | + * {@inheritdoc} |
| 34 | + */ |
| 35 | + public function buildForm(FormBuilderInterface $builder, array $options) |
| 36 | + { |
| 37 | + $parts = ['year', 'week']; |
| 38 | + |
| 39 | + $format = 'Y-\WW'; |
| 40 | + |
| 41 | + if ('single_text' === $options['widget']) { |
| 42 | + $builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format)); |
| 43 | + } else { |
| 44 | + $yearOptions = $weekOptions = [ |
| 45 | + 'error_bubbling' => true, |
| 46 | + 'empty_data' => '', |
| 47 | + ]; |
| 48 | + // when the form is compound the entries of the array are ignored in favor of children data |
| 49 | + // so we need to handle the cascade setting here |
| 50 | + $emptyData = $builder->getEmptyData() ?: []; |
| 51 | + |
| 52 | + if (isset($options['invalid_message'])) { |
| 53 | + $yearOptions['invalid_message'] = $options['invalid_message']; |
| 54 | + $weekOptions['invalid_message'] = $options['invalid_message']; |
| 55 | + } |
| 56 | + |
| 57 | + if (isset($options['invalid_message_parameters'])) { |
| 58 | + $yearOptions['invalid_message_parameters'] = $options['invalid_message_parameters']; |
| 59 | + $weekOptions['invalid_message_parameters'] = $options['invalid_message_parameters']; |
| 60 | + } |
| 61 | + |
| 62 | + if ('choice' === $options['widget']) { |
| 63 | + $years = $weeks = []; |
| 64 | + |
| 65 | + foreach ($options['years'] as $year) { |
| 66 | + $years[str_pad($year, 2, '0', STR_PAD_LEFT)] = $year; |
| 67 | + } |
| 68 | + |
| 69 | + foreach ($options['weeks'] as $week) { |
| 70 | + $weeks[str_pad($week, 2, '0', STR_PAD_LEFT)] = $week; |
| 71 | + } |
| 72 | + |
| 73 | + // Only pass a subset of the options to children |
| 74 | + $yearOptions['choices'] = $years; |
| 75 | + $yearOptions['placeholder'] = $options['placeholder']['year']; |
| 76 | + $yearOptions['choice_translation_domain'] = $options['choice_translation_domain']['year']; |
| 77 | + |
| 78 | + $weekOptions['choices'] = $weeks; |
| 79 | + $weekOptions['placeholder'] = $options['placeholder']['week']; |
| 80 | + $weekOptions['choice_translation_domain'] = $options['choice_translation_domain']['week']; |
| 81 | + |
| 82 | + // Append generic carry-along options |
| 83 | + foreach (['required', 'translation_domain'] as $passOpt) { |
| 84 | + $yearOptions[$passOpt] = $options[$passOpt]; |
| 85 | + |
| 86 | + $weekOptions[$passOpt] = $options[$passOpt]; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + $builder->add('year', self::$widgets[$options['widget']], $yearOptions); |
| 91 | + |
| 92 | + $builder->add('week', self::$widgets[$options['widget']], $weekOptions); |
| 93 | + |
| 94 | + if (isset($emptyData['week'])) { |
| 95 | + $weekOptions['empty_data'] = $emptyData['week']; |
| 96 | + } |
| 97 | + $builder->add('week', self::$widgets[$options['widget']], $weekOptions); |
| 98 | + |
| 99 | + $builder->addViewTransformer(new DateTimeToArrayTransformer($options['model_timezone'], $options['view_timezone'], $parts, 'text' === $options['widget'])); |
| 100 | + } |
| 101 | + |
| 102 | + if ('datetime_immutable' === $options['input']) { |
| 103 | + $builder->addModelTransformer(new DateTimeImmutableToDateTimeTransformer()); |
| 104 | + } elseif ('string' === $options['input']) { |
| 105 | + $builder->addModelTransformer(new ReversedTransformer( |
| 106 | + new DateTimeToStringTransformer($options['model_timezone'], $options['model_timezone'], $format) |
| 107 | + )); |
| 108 | + } elseif ('array' === $options['input']) { |
| 109 | + $builder->addModelTransformer(new ReversedTransformer( |
| 110 | + new DateTimeToArrayTransformer($options['model_timezone'], $options['model_timezone'], $parts) |
| 111 | + )); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * {@inheritdoc} |
| 117 | + */ |
| 118 | + public function buildView(FormView $view, FormInterface $form, array $options) |
| 119 | + { |
| 120 | + $view->vars = array_replace($view->vars, [ |
| 121 | + 'widget' => $options['widget'], |
| 122 | + ]); |
| 123 | + |
| 124 | + if ($options['html5'] && 'single_text' === $options['widget']) { |
| 125 | + $view->vars['type'] = 'week'; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * {@inheritdoc} |
| 131 | + */ |
| 132 | + public function configureOptions(OptionsResolver $resolver) |
| 133 | + { |
| 134 | + $compound = function (Options $options) { |
| 135 | + return 'single_text' !== $options['widget']; |
| 136 | + }; |
| 137 | + |
| 138 | + $placeholderDefault = function (Options $options) { |
| 139 | + return $options['required'] ? null : ''; |
| 140 | + }; |
| 141 | + |
| 142 | + $placeholderNormalizer = function (Options $options, $placeholder) use ($placeholderDefault) { |
| 143 | + if (\is_array($placeholder)) { |
| 144 | + $default = $placeholderDefault($options); |
| 145 | + |
| 146 | + return array_merge( |
| 147 | + ['year' => $default, 'week' => $default], |
| 148 | + $placeholder |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + return [ |
| 153 | + 'year' => $placeholder, |
| 154 | + 'week' => $placeholder, |
| 155 | + ]; |
| 156 | + }; |
| 157 | + |
| 158 | + $choiceTranslationDomainNormalizer = function (Options $options, $choiceTranslationDomain) { |
| 159 | + if (\is_array($choiceTranslationDomain)) { |
| 160 | + $default = false; |
| 161 | + |
| 162 | + return array_replace( |
| 163 | + ['year' => $default, 'week' => $default,], |
| 164 | + $choiceTranslationDomain |
| 165 | + ); |
| 166 | + } |
| 167 | + |
| 168 | + return [ |
| 169 | + 'year' => $choiceTranslationDomain, |
| 170 | + 'week' => $choiceTranslationDomain, |
| 171 | + ]; |
| 172 | + }; |
| 173 | + |
| 174 | + $weeksNumbers = array_map(function ($item) { |
| 175 | + return sprintf('W%02d', $item); |
| 176 | + }, range(1, 53)); |
| 177 | + |
| 178 | + $resolver->setDefaults([ |
| 179 | + 'years' => range(date('Y') - 5, date('Y') + 5), |
| 180 | + 'weeks' => array_combine($weeksNumbers, $weeksNumbers), |
| 181 | + 'widget' => 'choice', |
| 182 | + 'input' => 'datetime', |
| 183 | + 'model_timezone' => null, |
| 184 | + 'view_timezone' => null, |
| 185 | + 'placeholder' => $placeholderDefault, |
| 186 | + 'html5' => true, |
| 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 | + 'empty_data' => function (Options $options) { |
| 197 | + return $options['compound'] ? [] : ''; |
| 198 | + }, |
| 199 | + 'compound' => $compound, |
| 200 | + 'choice_translation_domain' => false, |
| 201 | + ]); |
| 202 | + |
| 203 | + $resolver->setNormalizer('placeholder', $placeholderNormalizer); |
| 204 | + $resolver->setNormalizer('choice_translation_domain', $choiceTranslationDomainNormalizer); |
| 205 | + |
| 206 | + $resolver->setAllowedValues('input', [ |
| 207 | + 'datetime', |
| 208 | + 'datetime_immutable', |
| 209 | + 'string', |
| 210 | + 'array', |
| 211 | + ]); |
| 212 | + |
| 213 | + $resolver->setAllowedValues('widget', [ |
| 214 | + 'single_text', |
| 215 | + 'text', |
| 216 | + 'choice', |
| 217 | + ]); |
| 218 | + |
| 219 | + $resolver->setAllowedTypes('years', 'array'); |
| 220 | + $resolver->setAllowedTypes('weeks', 'array'); |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * {@inheritdoc} |
| 225 | + */ |
| 226 | + public function getBlockPrefix() |
| 227 | + { |
| 228 | + return 'week'; |
| 229 | + } |
| 230 | +} |
0 commit comments