| 1: | <?php |
| 2: | namespace TIC\FormBundle\Form\Type; |
| 3: | |
| 4: | |
| 5: | use TIC\FormBundle\Base\TICWidgetType as BaseType; |
| 6: | |
| 7: | use Symfony\Component\OptionsResolver\OptionsResolver; |
| 8: | use Symfony\Component\OptionsResolver\Options; |
| 9: | |
| 10: | use Symfony\Component\Intl; |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | class LocaleType extends BaseType |
| 17: | { |
| 18: | protected $form_parent = ChoiceType::class; |
| 19: | protected $default_prefix = '<globe>'; |
| 20: | protected $default_suffix = '<globe>'; |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | public function configureOptions(OptionsResolver $resolver): void |
| 27: | { |
| 28: | parent::configureOptions($resolver); |
| 29: | |
| 30: | $resolver->setDefaults(array( |
| 31: | 'type' => 'locale', |
| 32: | 'prefix' => true, |
| 33: | 'with_js' => true, |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | 'only_choices' => array(), |
| 39: | |
| 40: | 'choices' => function (Options $options) { |
| 41: | switch ($options['type']) { |
| 42: | case 'locale' : return array_flip(Intl\Locales::getNames()); |
| 43: | case 'language' : return array_flip(Intl\Languages::getNames()); |
| 44: | case 'country' : return array_flip(Intl\Countries::getNames()); |
| 45: | case 'currency' : return array_flip(Intl\Currencies::getNames()); |
| 46: | } |
| 47: | }, |
| 48: | 'preferred_choices' => function (Options $options) { |
| 49: | switch ($options['type']) { |
| 50: | case 'locale' : return array('fr_FR'); |
| 51: | case 'language' : return array('fr'); |
| 52: | case 'country' : return array('FR'); |
| 53: | case 'currency' : return array('EUR'); |
| 54: | } |
| 55: | }, |
| 56: | )); |
| 57: | |
| 58: | $resolver->setAllowedTypes('type', array('string')); |
| 59: | $resolver->setAllowedValues('type', array('locale', 'language', 'country', 'currency')); |
| 60: | |
| 61: | $resolver->setNormalizer('choices', function (Options $options, $choices) { |
| 62: | if ($options['only_choices']) $choices = array_intersect($choices, $options['only_choices']); |
| 63: | return array_diff($choices, $options['excluded_choices']); |
| 64: | }); |
| 65: | } |
| 66: | |
| 67: | |
| 68: | } |
| 69: | |