vendor/bitbag/elasticsearch-plugin/src/Form/Type/PriceFilterType.php line 25

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file has been created by developers from BitBag.
  4.  * Feel free to contact us once you face any issues or want to start
  5.  * another great project.
  6.  * You can find more information about us on https://bitbag.io and write us
  7.  * an email on hello@bitbag.io.
  8.  */
  9. declare(strict_types=1);
  10. namespace BitBag\SyliusElasticsearchPlugin\Form\Type;
  11. use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\PriceNameResolverInterface;
  12. use Sylius\Bundle\MoneyBundle\Form\Type\MoneyType;
  13. use Sylius\Component\Currency\Context\CurrencyContextInterface;
  14. use Symfony\Component\Form\FormBuilderInterface;
  15. use Symfony\Component\Form\FormEvent;
  16. use Symfony\Component\Form\FormEvents;
  17. use Symfony\Component\Validator\Constraints\LessThan;
  18. use Symfony\Component\Validator\Constraints\PositiveOrZero;
  19. use Symfony\Component\Validator\Constraints\Type;
  20. final class PriceFilterType extends AbstractFilterType
  21. {
  22.     public const MAXIMUM_PRICE_VALUE 9999999999999999;
  23.     private PriceNameResolverInterface $priceNameResolver;
  24.     private CurrencyContextInterface $currencyContext;
  25.     public function __construct(
  26.         PriceNameResolverInterface $priceNameResolver,
  27.         CurrencyContextInterface $currencyContext
  28.     ) {
  29.         $this->priceNameResolver $priceNameResolver;
  30.         $this->currencyContext $currencyContext;
  31.     }
  32.     public function buildForm(FormBuilderInterface $builder, array $options): void
  33.     {
  34.         $builder
  35.             ->add($this->priceNameResolver->resolveMinPriceName(), MoneyType::class, [
  36.                 'label' => 'bitbag_sylius_elasticsearch_plugin.ui.min_price',
  37.                 'required' => false,
  38.                 'currency' => $this->currencyContext->getCurrencyCode(),
  39.                 'constraints' => [
  40.                     new Type([
  41.                         'type' => 'numeric',
  42.                         'message' => 'bitbag_sylius_elasticsearch_plugin.min_price_numeric',
  43.                     ]),
  44.                     new PositiveOrZero([
  45.                         'message' => 'bitbag_sylius_elasticsearch_plugin.min_price_positive_or_zero',
  46.                     ]),
  47.                     new LessThan(self::MAXIMUM_PRICE_VALUEoptions: [
  48.                         'message' => 'bitbag_sylius_elasticsearch_plugin.price_value_too_large',
  49.                     ]),
  50.                 ],
  51.             ])
  52.             ->add($this->priceNameResolver->resolveMaxPriceName(), MoneyType::class, [
  53.                 'label' => 'bitbag_sylius_elasticsearch_plugin.ui.max_price',
  54.                 'required' => false,
  55.                 'currency' => $this->currencyContext->getCurrencyCode(),
  56.                 'constraints' => [
  57.                     new Type([
  58.                         'type' => 'numeric',
  59.                         'message' => 'bitbag_sylius_elasticsearch_plugin.max_price_numeric',
  60.                     ]),
  61.                     new PositiveOrZero([
  62.                         'message' => 'bitbag_sylius_elasticsearch_plugin.max_price_positive_or_zero',
  63.                     ]),
  64.                     new LessThan(self::MAXIMUM_PRICE_VALUEoptions: [
  65.                         'message' => 'bitbag_sylius_elasticsearch_plugin.price_value_too_large',
  66.                     ]),
  67.                 ],
  68.             ])
  69.             ->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
  70.                 if (!empty($event->getData())) {
  71.                     $data = [];
  72.                     foreach ($event->getData() as $key => $item) {
  73.                         $data[$key] = trim($item);
  74.                     }
  75.                     $event->setData($data);
  76.                 }
  77.             })
  78.         ;
  79.     }
  80. }