src/Form/Type/ClientRestrictionsType.php line 20

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-04-30
  5.  * Time: 12:30
  6.  */
  7. namespace App\Form\Type;
  8. use App\Entity\Profile\ClientRestrictions;
  9. use Symfony\Component\Form\AbstractType;
  10. use Symfony\Component\Form\DataMapperInterface;
  11. use Symfony\Component\Form\Exception;
  12. use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  13. use Symfony\Component\Form\FormBuilderInterface;
  14. use Symfony\Component\Form\FormInterface;
  15. use Symfony\Component\OptionsResolver\OptionsResolver;
  16. use Symfony\Component\Validator\Constraints\Range;
  17. class ClientRestrictionsType extends AbstractType implements DataMapperInterface
  18. {
  19.     /**
  20.      * @inheritDoc
  21.      */
  22.     public function buildForm(FormBuilderInterface $builder, array $options): void
  23.     {
  24.         $builder->add('min_age'IntegerType::class, [
  25.             'required' => false,
  26.             'constraints' => new Range(['min' => 18'max' => 100]),
  27.         ]);
  28.         $builder->add('max_age'IntegerType::class, [
  29.             'required' => false,
  30.             'constraints' => new Range(['min' => 18'max' => 100]),
  31.         ]);
  32.         $builder->setDataMapper($this);
  33.     }
  34.     /**
  35.      * @inheritDoc
  36.      */
  37.     public function configureOptions(OptionsResolver $resolver): void
  38.     {
  39.         $resolver->setDefaults([
  40.             'data_class' => ClientRestrictions::class,
  41.             'empty_data' => null,
  42.         ]);
  43.     }
  44.     /**
  45.      * @inheritDoc
  46.      */
  47.     public function mapDataToForms($viewData$forms)
  48.     {
  49.         if (null === $viewData) {
  50.             return null;
  51.         }
  52.         if (!$viewData instanceof ClientRestrictions) {
  53.             throw new Exception\UnexpectedTypeException($viewDataClientRestrictions::class);
  54.         }
  55.         /** @var FormInterface[] $forms */
  56.         $forms iterator_to_array($forms);
  57.         $forms['min_age']->setData($viewData->getMinAge());
  58.         $forms['max_age']->setData($viewData->getMaxAge());
  59.     }
  60.     /**
  61.      * @inheritDoc
  62.      */
  63.     public function mapFormsToData($forms, &$viewData): void
  64.     {
  65.         /** @var FormInterface[] $forms */
  66.         $forms iterator_to_array($forms);
  67.         $viewData = new ClientRestrictions(
  68.             $forms['min_age']->getData(),
  69.             $forms['max_age']->getData()
  70.         );
  71.     }
  72. }