app/Plugin/Paid4/Form/Type/PaidTellType.php line 21

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright(c) 2020 RACCOON HOLDINGS, Inc. All rights reserved.
  4.  * https://paid.jp/
  5.  */
  6. namespace Plugin\Paid4\Form\Type;
  7. use Eccube\Common\EccubeConfig;
  8. use Symfony\Component\Form\AbstractType;
  9. use Symfony\Component\Form\Extension\Core\Type\TextType;
  10. use Symfony\Component\Form\FormBuilderInterface;
  11. use Symfony\Component\OptionsResolver\Options;
  12. use Symfony\Component\OptionsResolver\OptionsResolver;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. /**
  15.  * Class PhoneNumberType
  16.  */
  17. class PaidTellType extends AbstractType
  18. {
  19.     /**
  20.      * @var EccubeConfig
  21.      */
  22.     protected $eccubeConfig;
  23.     /**
  24.      * PhoneNumberType constructor.
  25.      *
  26.      * @param EccubeConfig $eccubeConfig
  27.      */
  28.     public function __construct(EccubeConfig $eccubeConfig)
  29.     {
  30.         $this->eccubeConfig $eccubeConfig;
  31.     }
  32.     /**
  33.      * @param FormBuilderInterface $builder
  34.      * @param array $options
  35.      */
  36.     public function buildForm(FormBuilderInterface $builder, array $options)
  37.     {
  38.         // 全角英数を事前に半角にする
  39.         $builder->addEventSubscriber(new \Eccube\Form\EventListener\ConvertKanaListener());
  40.         $builder->addEventSubscriber(new \Plugin\Paid4\Form\EventListener\ConvertHyphenListener());
  41.     }
  42.     /**
  43.      * @param OptionsResolver $resolver
  44.      */
  45.     public function configureOptions(OptionsResolver $resolver)
  46.     {
  47.         $constraints = function (Options $options) {
  48.             $constraints = [];
  49.             // requiredがtrueに指定されている場合, NotBlankを追加
  50.             if (isset($options['required']) && true === $options['required']) {
  51.                 $constraints[] = new Assert\NotBlank();
  52.                 $constraints[] = new Assert\Regex(array(
  53.                     'pattern' => '/^[0-9-]{0,25}$/',
  54.                 ));
  55.             }
  56.             $constraints[] = new Assert\Length([
  57.                 'max' => $this->eccubeConfig['paid4.tell_len'],
  58.             ]);
  59.             return $constraints;
  60.         };
  61.         $resolver->setDefaults([
  62.             'options' => ['constraints' => []],
  63.             'constraints' => $constraints,
  64.             'attr' => [
  65.                 'placeholder' => 'common.phone_number_sample',
  66.             ],
  67.             'trim' => true,
  68.         ]);
  69.     }
  70.     /**
  71.      * {@inheritdoc}
  72.      */
  73.     public function getParent()
  74.     {
  75.         return TextType::class;
  76.     }
  77.     /**
  78.      * {@inheritdoc}
  79.      */
  80.     public function getBlockPrefix()
  81.     {
  82.         return 'phone_number';
  83.     }
  84. }