src/Form/Type/TakeOutPricingType.php line 25

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-04-29
  5.  * Time: 19:26
  6.  */
  7. namespace App\Form\Type;
  8. use App\Entity\Profile\Profile;
  9. use App\Entity\TakeOutLocations;
  10. use App\Entity\TakeOutPricing;
  11. use Symfony\Component\Form\AbstractType;
  12. use Symfony\Component\Form\DataMapperInterface;
  13. use Symfony\Component\Form\Exception;
  14. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  15. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  16. use Symfony\Component\Form\FormBuilderInterface;
  17. use Symfony\Component\Form\FormInterface;
  18. use Symfony\Component\OptionsResolver\OptionsResolver;
  19. use Symfony\Component\Validator\Constraints\Callback;
  20. use Symfony\Component\Validator\Constraints\GreaterThan;
  21. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  22. class TakeOutPricingType extends AbstractType implements DataMapperInterface
  23. {
  24.     /**
  25.      * @inheritDoc
  26.      */
  27.     public function buildForm(FormBuilderInterface $builder, array $options): void
  28.     {
  29.         $builder->add('one_hour_price'NumberType::class, [
  30.             'required' => false,
  31.             'constraints' => new GreaterThan(0),
  32.         ]);
  33.         $builder->add('two_hours_price'NumberType::class, [
  34.             'required' => false,
  35.             'constraints' => new GreaterThan(0),
  36.         ]);
  37.         $builder->add('night_price'NumberType::class, [
  38.             'required' => false,
  39.             'constraints' => new GreaterThan(0),
  40.         ]);
  41.         $builder->add('locations'ChoiceType::class, [
  42.             'required' => false,
  43.             'multiple' => true,
  44.             'expanded' => true,
  45.             'choices' => TakeOutLocations::getList(),
  46.             'constraints' => new Callback([$this'validateLocations']),
  47.         ]);
  48.         $builder->setDataMapper($this);
  49.     }
  50.     /**
  51.      * @inheritDoc
  52.      */
  53.     public function configureOptions(OptionsResolver $resolver): void
  54.     {
  55.         $resolver->setDefaults([
  56.             'data_class' => TakeOutPricing::class,
  57.             'empty_data' => null,
  58.         ]);
  59.     }
  60.     public function validateLocations($valueExecutionContextInterface $context): void
  61.     {
  62.         $form $context->getRoot();
  63.         /** @var Profile $profile */
  64.         $profile $form->getData();
  65.         $takeOutPricing $profile->getTakeOutPricing();
  66.         $hasLocations count($value) > 0;
  67.         if ($takeOutPricing->isProvided() && !$hasLocations) {
  68.             $context
  69.                 ->buildViolation('At least 1 location should be chosen.')
  70.                 ->addViolation()
  71.             ;
  72.         } elseif ($hasLocations && !$takeOutPricing->isProvided()) {
  73.             $context
  74.                 ->buildViolation('At least 1 price should be defined.')
  75.                 ->addViolation()
  76.             ;
  77.         }
  78.     }
  79.     /**
  80.      * @inheritDoc
  81.      */
  82.     public function mapDataToForms($viewData$forms)
  83.     {
  84.         if (null === $viewData) {
  85.             return null;
  86.         }
  87.         if (!$viewData instanceof TakeOutPricing) {
  88.             throw new Exception\UnexpectedTypeException($viewDataTakeOutPricing::class);
  89.         }
  90.         /** @var FormInterface[] $forms */
  91.         $forms iterator_to_array($forms);
  92.         $forms['one_hour_price']->setData($viewData->getOneHourPrice());
  93.         $forms['two_hours_price']->setData($viewData->getTwoHoursPrice());
  94.         $forms['night_price']->setData($viewData->getNightPrice());
  95.         $forms['locations']->setData($viewData->getLocations());
  96.     }
  97.     /**
  98.      * @inheritDoc
  99.      */
  100.     public function mapFormsToData($forms, &$viewData): void
  101.     {
  102.         /** @var FormInterface[] $forms */
  103.         $forms iterator_to_array($forms);
  104.         $viewData = new TakeOutPricing(
  105.             $forms['one_hour_price']->getData(),
  106.             $forms['two_hours_price']->getData(),
  107.             $forms['night_price']->getData(),
  108.             $forms['locations']->getData() ?? []
  109.         );
  110.     }
  111. }