|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\Exception\LogicException; |
| 16 | +use Symfony\Component\Form\Extension\Core\DataTransformer\WeekToArrayTransformer; |
| 17 | +use Symfony\Component\Form\FormBuilderInterface; |
| 18 | +use Symfony\Component\Form\FormInterface; |
| 19 | +use Symfony\Component\Form\FormView; |
| 20 | +use Symfony\Component\Form\ReversedTransformer; |
| 21 | +use Symfony\Component\OptionsResolver\Options; |
| 22 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 23 | + |
| 24 | +class WeekType extends AbstractType |
| 25 | +{ |
| 26 | + private static $widgets = [ |
| 27 | + 'text' => IntegerType::class, |
| 28 | + 'choice' => ChoiceType::class, |
| 29 | + ]; |
| 30 | + |
| 31 | + /** |
| 32 | + * {@inheritdoc} |
| 33 | + */ |
| 34 | + public function buildForm(FormBuilderInterface $builder, array $options) |
| 35 | + { |
| 36 | + if ('string' === $options['input']) { |
| 37 | + $builder->addModelTransformer(new WeekToArrayTransformer()); |
| 38 | + } |
| 39 | + |
| 40 | + if ('single_text' === $options['widget']) { |
| 41 | + $builder->addViewTransformer(new ReversedTransformer(new WeekToArrayTransformer())); |
| 42 | + } else { |
| 43 | + $yearOptions = $weekOptions = [ |
| 44 | + 'error_bubbling' => true, |
| 45 | + 'empty_data' => '', |
| 46 | + ]; |
| 47 | + // when the form is compound the entries of the array are ignored in favor of children data |
| 48 | + // so we need to handle the cascade setting here |
| 49 | + $emptyData = $builder->getEmptyData() ?: []; |
| 50 | + |
| 51 | + $yearOptions['empty_data'] = $emptyData['year'] ?? ''; |
| 52 | + $weekOptions['empty_data'] = $emptyData['week'] ?? ''; |
| 53 | + |
| 54 | + if (isset($options['invalid_message'])) { |
| 55 | + $yearOptions['invalid_message'] = $options['invalid_message']; |
| 56 | + $weekOptions['invalid_message'] = $options['invalid_message']; |
| 57 | + } |
| 58 | + |
| 59 | + if (isset($options['invalid_message_parameters'])) { |
| 60 | + $yearOptions['invalid_message_parameters'] = $options['invalid_message_parameters']; |
| 61 | + $weekOptions['invalid_message_parameters'] = $options['invalid_message_parameters']; |
| 62 | + } |
| 63 | + |
| 64 | + if ('choice' === $options['widget']) { |
| 65 | + // Only pass a subset of the options to children |
| 66 | + $yearOptions['choices'] = array_combine($options['years'], $options['years']); |
| 67 | + $yearOptions['placeholder'] = $options['placeholder']['year']; |
| 68 | + $yearOptions['choice_translation_domain'] = $options['choice_translation_domain']['year']; |
| 69 | + |
| 70 | + $weekOptions['choices'] = array_combine($options['weeks'], $options['weeks']); |
| 71 | + $weekOptions['placeholder'] = $options['placeholder']['week']; |
| 72 | + $weekOptions['choice_translation_domain'] = $options['choice_translation_domain']['week']; |
| 73 | + |
| 74 | + // Append generic carry-along options |
| 75 | + foreach (['required', 'translation_domain'] as $passOpt) { |
| 76 | + $yearOptions[$passOpt] = $options[$passOpt]; |
| 77 | + $weekOptions[$passOpt] = $options[$passOpt]; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + $builder->add('year', self::$widgets[$options['widget']], $yearOptions); |
| 82 | + $builder->add('week', self::$widgets[$options['widget']], $weekOptions); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * {@inheritdoc} |
| 88 | + */ |
| 89 | + public function buildView(FormView $view, FormInterface $form, array $options) |
| 90 | + { |
| 91 | + $view->vars['widget'] = $options['widget']; |
| 92 | + |
| 93 | + if ($options['html5']) { |
| 94 | + $view->vars['type'] = 'week'; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * {@inheritdoc} |
| 100 | + */ |
| 101 | + public function configureOptions(OptionsResolver $resolver) |
| 102 | + { |
| 103 | + $compound = function (Options $options) { |
| 104 | + return 'single_text' !== $options['widget']; |
| 105 | + }; |
| 106 | + |
| 107 | + $placeholderDefault = function (Options $options) { |
| 108 | + return $options['required'] ? null : ''; |
| 109 | + }; |
| 110 | + |
| 111 | + $placeholderNormalizer = function (Options $options, $placeholder) use ($placeholderDefault) { |
| 112 | + if (\is_array($placeholder)) { |
| 113 | + $default = $placeholderDefault($options); |
| 114 | + |
| 115 | + return array_merge( |
| 116 | + ['year' => $default, 'week' => $default], |
| 117 | + $placeholder |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + return [ |
| 122 | + 'year' => $placeholder, |
| 123 | + 'week' => $placeholder, |
| 124 | + ]; |
| 125 | + }; |
| 126 | + |
| 127 | + $choiceTranslationDomainNormalizer = function (Options $options, $choiceTranslationDomain) { |
| 128 | + if (\is_array($choiceTranslationDomain)) { |
| 129 | + $default = false; |
| 130 | + |
| 131 | + return array_replace( |
| 132 | + ['year' => $default, 'week' => $default], |
| 133 | + $choiceTranslationDomain |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + return [ |
| 138 | + 'year' => $choiceTranslationDomain, |
| 139 | + 'week' => $choiceTranslationDomain, |
| 140 | + ]; |
| 141 | + }; |
| 142 | + |
| 143 | + $resolver->setDefaults([ |
| 144 | + 'years' => range(date('Y') - 10, date('Y') + 10), |
| 145 | + 'weeks' => array_combine(range(1, 53), range(1, 53)), |
| 146 | + 'widget' => 'single_text', |
| 147 | + 'input' => 'array', |
| 148 | + 'placeholder' => $placeholderDefault, |
| 149 | + 'html5' => static function (Options $options) { |
| 150 | + return 'single_text' === $options['widget']; |
| 151 | + }, |
| 152 | + 'error_bubbling' => false, |
| 153 | + 'empty_data' => function (Options $options) { |
| 154 | + return $options['compound'] ? [] : ''; |
| 155 | + }, |
| 156 | + 'compound' => $compound, |
| 157 | + 'choice_translation_domain' => false, |
| 158 | + ]); |
| 159 | + |
| 160 | + $resolver->setNormalizer('placeholder', $placeholderNormalizer); |
| 161 | + $resolver->setNormalizer('choice_translation_domain', $choiceTranslationDomainNormalizer); |
| 162 | + $resolver->setNormalizer('html5', function (Options $options, $html5) { |
| 163 | + if ($html5 && 'single_text' !== $options['widget']) { |
| 164 | + throw new LogicException(sprintf('The "widget" option of %s must be set to "single_text" when the "html5" option is enabled.', self::class)); |
| 165 | + } |
| 166 | + |
| 167 | + return $html5; |
| 168 | + }); |
| 169 | + |
| 170 | + $resolver->setAllowedValues('input', [ |
| 171 | + 'string', |
| 172 | + 'array', |
| 173 | + ]); |
| 174 | + |
| 175 | + $resolver->setAllowedValues('widget', [ |
| 176 | + 'single_text', |
| 177 | + 'text', |
| 178 | + 'choice', |
| 179 | + ]); |
| 180 | + |
| 181 | + $resolver->setAllowedTypes('years', 'int[]'); |
| 182 | + $resolver->setAllowedTypes('weeks', 'int[]'); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * {@inheritdoc} |
| 187 | + */ |
| 188 | + public function getBlockPrefix() |
| 189 | + { |
| 190 | + return 'week'; |
| 191 | + } |
| 192 | +} |
0 commit comments