vendor/misd/phone-number-bundle/Form/Type/PhoneNumberType.php line 30

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony2 PhoneNumberBundle.
  4.  *
  5.  * (c) University of Cambridge
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Misd\PhoneNumberBundle\Form\Type;
  11. use libphonenumber\PhoneNumberFormat;
  12. use libphonenumber\PhoneNumberUtil;
  13. use Misd\PhoneNumberBundle\Form\DataTransformer\PhoneNumberToArrayTransformer;
  14. use Misd\PhoneNumberBundle\Form\DataTransformer\PhoneNumberToStringTransformer;
  15. use Symfony\Component\Form\AbstractType;
  16. use Symfony\Component\Form\FormBuilderInterface;
  17. use Symfony\Component\Form\FormInterface;
  18. use Symfony\Component\Form\FormView;
  19. use Symfony\Component\Intl\Intl;
  20. use Symfony\Component\OptionsResolver\Options;
  21. use Symfony\Component\OptionsResolver\OptionsResolver;
  22. use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  23. /**
  24.  * Phone number form type.
  25.  */
  26. class PhoneNumberType extends AbstractType
  27. {
  28.     const WIDGET_SINGLE_TEXT 'single_text';
  29.     const WIDGET_COUNTRY_CHOICE 'country_choice';
  30.     /**
  31.      * {@inheritdoc}
  32.      */
  33.     public function buildForm(FormBuilderInterface $builder, array $options)
  34.     {
  35.         if (self::WIDGET_COUNTRY_CHOICE === $options['widget']) {
  36.             $util PhoneNumberUtil::getInstance();
  37.             $countries = array();
  38.             if (is_array($options['country_choices'])) {
  39.                 foreach ($options['country_choices'] as $country) {
  40.                     $code $util->getCountryCodeForRegion($country);
  41.                     if ($code) {
  42.                         $countries[$country] = $code;
  43.                     }
  44.                 }
  45.             }
  46.             if (empty($countries)) {
  47.                 foreach ($util->getSupportedRegions() as $country) {
  48.                     $countries[$country] = $util->getCountryCodeForRegion($country);
  49.                 }
  50.             }
  51.             $countryChoices = array();
  52.             foreach (Intl::getRegionBundle()->getCountryNames() as $region => $name) {
  53.                 if (false === isset($countries[$region])) {
  54.                     continue;
  55.                 }
  56.                 $countryChoices[sprintf('%s (+%s)'$name$countries[$region])] = $region;
  57.             }
  58.             $transformerChoices array_values($countryChoices);
  59.             $countryOptions $numberOptions = array(
  60.                 'error_bubbling' => true,
  61.                 'required' => $options['required'],
  62.                 'disabled' => $options['disabled'],
  63.                 'translation_domain' => $options['translation_domain'],
  64.             );
  65.             if (method_exists('Symfony\\Component\\Form\\AbstractType''getBlockPrefix')) {
  66.                 $choiceType 'Symfony\\Component\\Form\\Extension\\Core\\Type\\ChoiceType';
  67.                 $textType 'Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType';
  68.                 $countryOptions['choice_translation_domain'] = false;
  69.                 // To be removed when dependency on Symfony Form is bumped to 3.1.
  70.                 if (!in_array('Symfony\\Component\\Form\\DataTransformerInterface'class_implements('Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType'))) {
  71.                     $countryOptions['choices_as_values'] = true;
  72.                 }
  73.             } else {
  74.                 // To be removed when dependency on Symfony Form is bumped to 2.7.
  75.                 $choiceType 'choice';
  76.                 $textType 'text';
  77.                 $countryChoices array_flip($countryChoices);
  78.             }
  79.             $countryOptions['required'] = true;
  80.             $countryOptions['choices'] = $countryChoices;
  81.             $countryOptions['preferred_choices'] = $options['preferred_country_choices'];
  82.             if ($options['country_placeholder']) {
  83.                 $countryOptions['placeholder'] = $options['country_placeholder'];
  84.             }
  85.             $builder
  86.                 ->add('country'$choiceType$countryOptions)
  87.                 ->add('number'$textType$numberOptions)
  88.                 ->addViewTransformer(new PhoneNumberToArrayTransformer($transformerChoices));
  89.         } else {
  90.             $builder->addViewTransformer(
  91.                 new PhoneNumberToStringTransformer($options['default_region'], $options['format'])
  92.             );
  93.         }
  94.     }
  95.     /**
  96.      * {@inheritdoc}
  97.      */
  98.     public function buildView(FormView $viewFormInterface $form, array $options)
  99.     {
  100.         $view->vars['type'] = 'tel';
  101.         $view->vars['widget'] = $options['widget'];
  102.     }
  103.     /**
  104.      * {@inheritdoc}
  105.      *
  106.      * @deprecated To be removed when the Symfony Form component compatibility
  107.      *             is bumped to at least 2.7.
  108.      */
  109.     public function setDefaultOptions(OptionsResolverInterface $resolver)
  110.     {
  111.         $this->configureOptions($resolver);
  112.     }
  113.     /**
  114.      * {@inheritdoc}
  115.      */
  116.     public function configureOptions(OptionsResolver $resolver)
  117.     {
  118.         $resolver->setDefaults(
  119.             array(
  120.                 'widget' => self::WIDGET_SINGLE_TEXT,
  121.                 'compound' => function (Options $options) {
  122.                     return PhoneNumberType::WIDGET_SINGLE_TEXT !== $options['widget'];
  123.                 },
  124.                 'default_region' => PhoneNumberUtil::UNKNOWN_REGION,
  125.                 'format' => PhoneNumberFormat::INTERNATIONAL,
  126.                 'invalid_message' => 'This value is not a valid phone number.',
  127.                 'by_reference' => false,
  128.                 'error_bubbling' => false,
  129.                 'country_choices' => array(),
  130.                 'country_placeholder' => false,
  131.                 'preferred_country_choices' => array(),
  132.             )
  133.         );
  134.         if (method_exists($resolver'setDefault')) {
  135.             $resolver->setAllowedValues(
  136.                 'widget',
  137.                 array(
  138.                     self::WIDGET_SINGLE_TEXT,
  139.                     self::WIDGET_COUNTRY_CHOICE,
  140.                 )
  141.             );
  142.         } else {
  143.             // To be removed when dependency on Symfony OptionsResolver is bumped to 2.6.
  144.             $resolver->setAllowedValues(
  145.                 array(
  146.                     'widget' => array(
  147.                         self::WIDGET_SINGLE_TEXT,
  148.                         self::WIDGET_COUNTRY_CHOICE,
  149.                     ),
  150.                 )
  151.             );
  152.         }
  153.     }
  154.     /**
  155.      * {@inheritdoc}
  156.      */
  157.     public function getName()
  158.     {
  159.         return $this->getBlockPrefix();
  160.     }
  161.     /**
  162.      * {@inheritdoc}
  163.      */
  164.     public function getBlockPrefix()
  165.     {
  166.         return 'tel';
  167.     }
  168. }