lib/composer/DiscountBundle/src/VisualMedia/DiscountBundle/Form/Type/DiscountOrderType.php line 74

Open in your IDE?
  1. <?php
  2. /**
  3. * Discount Order Type
  4. *
  5. * @author Bertin van den Ham <b.vandenham@dappr.nl>
  6. */
  7. namespace VisualMedia\DiscountBundle\Form\Type;
  8. use Symfony\Component\Form\AbstractType;
  9. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  10. use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  11. use Symfony\Component\Form\Extension\Core\Type\MoneyType;
  12. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  13. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  14. use Symfony\Component\Form\Extension\Core\Type\TextType;
  15. use Symfony\Component\Form\FormBuilderInterface;
  16. use Symfony\Component\Form\FormEvent;
  17. use Symfony\Component\Form\FormEvents;
  18. use Symfony\Component\Form\FormInterface;
  19. use Symfony\Component\OptionsResolver\OptionsResolver;
  20. use Symfony\Component\Validator\Constraints\NotBlank;
  21. use Symfony\Contracts\EventDispatcher\Event;
  22. use VisualMedia\DiscountBundle\Entity\Discount;
  23. use VisualMedia\DiscountBundle\Form\Constraint\DiscountValidConstraint;
  24. use VisualMedia\DiscountBundle\Model\DiscountModel;
  25. use VisualMedia\LisaBundle\Form\Type\BooleanType;
  26. use VisualMedia\LisaBundle\Form\Type\DateTimeType;
  27. use VisualMedia\OrderBundle\Entity\BaseOrder;
  28. use function GuzzleHttp\Psr7\parse_header;
  29. /**
  30. * Discount Order Type
  31. */
  32. class DiscountOrderType extends AbstractType
  33. {
  34. public const NAME = 'discount_order';
  35. public const DISCOUNT_NAME = 'discount';
  36. public const DISCOUNT_CODE = 'code';
  37. public const DISCOUNT_CHECK_CODE = 'checkCode';
  38. public const DISCOUNT_REMOVE_CODE = 'removeCode';
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function getName(): string
  43. {
  44. return static::NAME;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function buildForm(FormBuilderInterface $builder, array $options)
  50. {
  51. $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($options) {
  52. $form = $event->getForm();
  53. if (null === $order = $options['order']) {
  54. return;
  55. }
  56. $this->discountOrderFormModifier($form, $order, FormEvents::PRE_SET_DATA);
  57. });
  58. }
  59. /**
  60. * @param FormInterface $form
  61. * @param BaseOrder $order
  62. * @param string|null $formEvent
  63. * @param void
  64. */
  65. protected function discountOrderFormModifier(FormInterface $form, BaseOrder $order, string $formEvent = null): void
  66. {
  67. if ($order->getDiscount() === null) {
  68. $form->add(static::DISCOUNT_CODE, TextType::class, [
  69. 'label' => 'discount_order.code',
  70. 'required' => false,
  71. 'constraints' => $this->discountCodeFieldConstraints()
  72. ]);
  73. $form->add(static::DISCOUNT_CHECK_CODE, SubmitType::class, [
  74. 'label' => 'discount_order.checkCode'
  75. ]);
  76. }
  77. else {
  78. $form->add(static::DISCOUNT_REMOVE_CODE, SubmitType::class, [
  79. 'label' => 'x'
  80. ]);
  81. }
  82. }
  83. /**
  84. * Return a array of Contraints for the discount order field
  85. * this function could be used to add extra custom Constraints for App
  86. * specific code.
  87. * @return array|
  88. */
  89. protected function discountCodeFieldConstraints(): array
  90. {
  91. return [
  92. new DiscountValidConstraint(),
  93. ];
  94. }
  95. /**
  96. * {@inheritDoc}
  97. */
  98. public function configureOptions(OptionsResolver $resolver): void
  99. {
  100. $resolver->setDefaults([
  101. 'order' => null
  102. ]);
  103. }
  104. }