src/Form/Type/PhoneCallRestrictionsType.php line 36

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-04-30
  5.  * Time: 12:50
  6.  */
  7. namespace App\Form\Type;
  8. use App\Entity\PhoneCallRestrictions;
  9. use App\Service\Features;
  10. use Symfony\Component\Form\AbstractType;
  11. use Symfony\Component\Form\DataMapperInterface;
  12. use Symfony\Component\Form\Exception;
  13. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  14. use Symfony\Component\Form\FormBuilderInterface;
  15. use Symfony\Component\Form\FormInterface;
  16. use Symfony\Component\OptionsResolver\OptionsResolver;
  17. class PhoneCallRestrictionsType extends AbstractType implements DataMapperInterface
  18. {
  19.     public function __construct(
  20.         private Features $features
  21.     ) {}
  22.     /**
  23.      * @inheritDoc
  24.      */
  25.     public function buildForm(FormBuilderInterface $builder, array $options): void
  26.     {
  27.         $hourChoices range(024);
  28.         $builder->add('time_from'ChoiceType::class, [
  29.             'empty_data' => 0,
  30.             'choices' => $hourChoices,
  31.             'choice_label' => function ($value): string {
  32.                 return sprintf('%02d'$value);
  33.             },
  34.         ]);
  35.         $builder->add('time_to'ChoiceType::class, [
  36.             'empty_data' => 24,
  37.             'choices' => $hourChoices,
  38.             'choice_label' => function ($value): string {
  39.                 return sprintf('%02d'$value);
  40.             }
  41.         ]);
  42.         $answeringToChoices PhoneCallRestrictions::getList();
  43.         if(false == $this->features->extended_profile_form()) {
  44.             unset($answeringToChoices['ANSWERING_MESSENGER']);
  45.         }
  46.         $builder->add('answering_to'ChoiceType::class, [
  47.             'multiple' => true,
  48.             'expanded' => true,
  49. //            'empty_data' => PhoneCallRestrictions::ANSWERING_CALLS_AND_SMS,
  50.             'choices' => $answeringToChoices,
  51.             'choice_translation_domain' => 'phone_call_restrictions',
  52.         ]);
  53.         $builder->setDataMapper($this);
  54.     }
  55.     /**
  56.      * @inheritDoc
  57.      */
  58.     public function configureOptions(OptionsResolver $resolver): void
  59.     {
  60.         $resolver->setDefaults([
  61.             'data_class' => PhoneCallRestrictions::class,
  62.             'empty_data' => null,
  63.         ]);
  64.     }
  65.     /**
  66.      * @inheritDoc
  67.      */
  68.     public function mapDataToForms($viewData$forms)
  69.     {
  70.         if (null === $viewData) {
  71.             return null;
  72.         }
  73.         if (!$viewData instanceof PhoneCallRestrictions) {
  74.             throw new Exception\UnexpectedTypeException($viewDataPhoneCallRestrictions::class);
  75.         }
  76.         /** @var FormInterface[] $forms */
  77.         $forms iterator_to_array($forms);
  78.         $forms['time_from']->setData($viewData->getTimeFrom());
  79.         $forms['time_to']->setData($viewData->getTimeTo());
  80.         $forms['answering_to']->setData($viewData->getAnsweringTo());
  81.     }
  82.     /**
  83.      * @inheritDoc
  84.      */
  85.     public function mapFormsToData($forms, &$viewData): void
  86.     {
  87.         /** @var FormInterface[] $forms */
  88.         $forms iterator_to_array($forms);
  89.         $viewData = new PhoneCallRestrictions(
  90.             $forms['time_from']->getData(),
  91.             $forms['time_to']->getData(),
  92.             $forms['answering_to']->getData()
  93.         );
  94.     }
  95. }