src/Form/Type/ExpressPricingType.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Form\Type;
  3. use App\Entity\ApartmentsPricing;
  4. use App\Entity\ExpressPricing;
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\DataMapperInterface;
  7. use Symfony\Component\Form\Exception;
  8. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  9. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  10. use Symfony\Component\Form\FormBuilderInterface;
  11. use Symfony\Component\Form\FormInterface;
  12. use Symfony\Component\OptionsResolver\OptionsResolver;
  13. use Symfony\Component\Validator\Constraints\GreaterThan;
  14. class ExpressPricingType extends AbstractType implements DataMapperInterface
  15. {
  16.     /**
  17.      * @inheritDoc
  18.      */
  19.     public function buildForm(FormBuilderInterface $builder, array $options): void
  20.     {
  21.         $builder->add('provided'CheckboxType::class, [
  22.             'required' => false,
  23.         ]);
  24.         $builder->add('price'NumberType::class, [
  25.             'required' => false,
  26.             'constraints' => new GreaterThan(0),
  27.         ]);
  28.         $builder->setDataMapper($this);
  29.     }
  30.     /**
  31.      * @inheritDoc
  32.      */
  33.     public function configureOptions(OptionsResolver $resolver): void
  34.     {
  35.         $resolver->setDefaults([
  36.             'data_class' => ExpressPricing::class,
  37.             'empty_data' => null,
  38.         ]);
  39.     }
  40.     /**
  41.      * @inheritDoc
  42.      */
  43.     public function mapDataToForms($viewData$forms)
  44.     {
  45.         if (null === $viewData) {
  46.             return null;
  47.         }
  48.         if (!$viewData instanceof ExpressPricing) {
  49.             throw new Exception\UnexpectedTypeException($viewDataExpressPricing::class);
  50.         }
  51.         /** @var FormInterface[] $forms */
  52.         $forms iterator_to_array($forms);
  53.         $forms['provided']->setData($viewData->isProvided());
  54.         $forms['price']->setData($viewData->getPrice());
  55.     }
  56.     /**
  57.      * @inheritDoc
  58.      */
  59.     public function mapFormsToData($forms, &$viewData): void
  60.     {
  61.         /** @var FormInterface[] $forms */
  62.         $forms iterator_to_array($forms);
  63.         $viewData = new ExpressPricing(
  64.             $forms['provided']->getData(),
  65.             $forms['price']->getData()
  66.         );
  67.     }
  68. }