vendor/sylius/sylius/src/Sylius/Bundle/PaymentBundle/Form/Type/PaymentMethodChoiceType.php line 26

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\PaymentBundle\Form\Type;
  12. use Sylius\Component\Payment\Model\PaymentInterface;
  13. use Sylius\Component\Payment\Resolver\PaymentMethodsResolverInterface;
  14. use Sylius\Component\Resource\Repository\RepositoryInterface;
  15. use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer;
  16. use Symfony\Component\Form\AbstractType;
  17. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  18. use Symfony\Component\Form\FormBuilderInterface;
  19. use Symfony\Component\OptionsResolver\Options;
  20. use Symfony\Component\OptionsResolver\OptionsResolver;
  21. final class PaymentMethodChoiceType extends AbstractType
  22. {
  23.     public function __construct(
  24.         private PaymentMethodsResolverInterface $paymentMethodsResolver,
  25.         private RepositoryInterface $paymentMethodRepository
  26.     ) {
  27.     }
  28.     public function buildForm(FormBuilderInterface $builder, array $options): void
  29.     {
  30.         if ($options['multiple']) {
  31.             $builder->addModelTransformer(new CollectionToArrayTransformer());
  32.         }
  33.     }
  34.     public function configureOptions(OptionsResolver $resolver): void
  35.     {
  36.         $resolver
  37.             ->setDefaults([
  38.                 'choices' => function (Options $options) {
  39.                     if (isset($options['subject'])) {
  40.                         return $this->paymentMethodsResolver->getSupportedMethods($options['subject']);
  41.                     }
  42.                     return $this->paymentMethodRepository->findAll();
  43.                 },
  44.                 'choice_value' => 'code',
  45.                 'choice_label' => 'name',
  46.                 'choice_translation_domain' => false,
  47.             ])
  48.             ->setDefined([
  49.                 'subject',
  50.             ])
  51.             ->setAllowedTypes('subject'PaymentInterface::class)
  52.         ;
  53.     }
  54.     public function getParent(): string
  55.     {
  56.         return ChoiceType::class;
  57.     }
  58.     public function getBlockPrefix(): string
  59.     {
  60.         return 'sylius_payment_method_choice';
  61.     }
  62. }