src/Form/Type/ApartmentsPricingType.php line 20

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-04-29
  5.  * Time: 19:23
  6.  */
  7. namespace App\Form\Type;
  8. use App\Entity\ApartmentsPricing;
  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\NumberType;
  13. use Symfony\Component\Form\FormBuilderInterface;
  14. use Symfony\Component\Form\FormInterface;
  15. use Symfony\Component\OptionsResolver\OptionsResolver;
  16. use Symfony\Component\Validator\Constraints\GreaterThan;
  17. class ApartmentsPricingType extends AbstractType implements DataMapperInterface
  18. {
  19.     /**
  20.      * @inheritDoc
  21.      */
  22.     public function buildForm(FormBuilderInterface $builder, array $options): void
  23.     {
  24.         $builder->add('one_hour_price'NumberType::class, [
  25.             'required' => false,
  26.             'constraints' => new GreaterThan(0),
  27.         ]);
  28.         $builder->add('two_hours_price'NumberType::class, [
  29.             'required' => false,
  30.             'constraints' => new GreaterThan(0),
  31.         ]);
  32.         $builder->add('night_price'NumberType::class, [
  33.             'required' => false,
  34.             'constraints' => new GreaterThan(0),
  35.         ]);
  36.         $builder->setDataMapper($this);
  37.     }
  38.     /**
  39.      * @inheritDoc
  40.      */
  41.     public function configureOptions(OptionsResolver $resolver): void
  42.     {
  43.         $resolver->setDefaults([
  44.             'data_class' => ApartmentsPricing::class,
  45.             'empty_data' => null,
  46.         ]);
  47.     }
  48.     /**
  49.      * @inheritDoc
  50.      */
  51.     public function mapDataToForms($viewData$forms)
  52.     {
  53.         if (null === $viewData) {
  54.             return null;
  55.         }
  56.         if (!$viewData instanceof ApartmentsPricing) {
  57.             throw new Exception\UnexpectedTypeException($viewDataApartmentsPricing::class);
  58.         }
  59.         /** @var FormInterface[] $forms */
  60.         $forms iterator_to_array($forms);
  61.         $forms['one_hour_price']->setData($viewData->getOneHourPrice());
  62.         $forms['two_hours_price']->setData($viewData->getTwoHoursPrice());
  63.         $forms['night_price']->setData($viewData->getNightPrice());
  64.     }
  65.     /**
  66.      * @inheritDoc
  67.      */
  68.     public function mapFormsToData($forms, &$viewData): void
  69.     {
  70.         /** @var FormInterface[] $forms */
  71.         $forms iterator_to_array($forms);
  72.         $viewData = new ApartmentsPricing(
  73.             $forms['one_hour_price']->getData(),
  74.             $forms['two_hours_price']->getData(),
  75.             $forms['night_price']->getData()
  76.         );
  77.     }
  78. }