]> Raphaƫl G. Git Repositories - packbundle/blobdiff - Form/CaptchaType.php
Version 0.5.0
[packbundle] / Form / CaptchaType.php
index da087c081cf6b248104a37dfd2fbab50fcc71c17..83f3b1be998bf14b404e1b0cc2cbc9a7df193f27 100644 (file)
@@ -14,14 +14,14 @@ namespace Rapsys\PackBundle\Form;
 use Rapsys\PackBundle\Util\ImageUtil;
 use Rapsys\PackBundle\Util\SluggerUtil;
 
 use Rapsys\PackBundle\Util\ImageUtil;
 use Rapsys\PackBundle\Util\SluggerUtil;
 
-use Symfony\Contracts\Translation\TranslatorInterface;
 use Symfony\Component\Form\AbstractType;
 use Symfony\Component\Form\Extension\Core\Type\HiddenType;
 use Symfony\Component\Form\Extension\Core\Type\IntegerType;
 use Symfony\Component\Form\FormBuilderInterface;
 use Symfony\Component\Form\AbstractType;
 use Symfony\Component\Form\Extension\Core\Type\HiddenType;
 use Symfony\Component\Form\Extension\Core\Type\IntegerType;
 use Symfony\Component\Form\FormBuilderInterface;
-use Symfony\Component\Form\FormEvents;
-use Symfony\Component\Form\FormEvent;
 use Symfony\Component\Form\FormError;
 use Symfony\Component\Form\FormError;
+use Symfony\Component\Form\FormEvent;
+use Symfony\Component\Form\FormEvents;
+use Symfony\Contracts\Translation\TranslatorInterface;
 
 /**
  * Captcha Type class definition
 
 /**
  * Captcha Type class definition
@@ -29,57 +29,37 @@ use Symfony\Component\Form\FormError;
  * @see https://symfony.com/doc/current/form/create_custom_field_type.html
  */
 class CaptchaType extends AbstractType {
  * @see https://symfony.com/doc/current/form/create_custom_field_type.html
  */
 class CaptchaType extends AbstractType {
-       /**
-        * @var ImageUtil $image
-        */
-       private $image;
-
-       /**
-        * @var SluggerUtil $slugger
-        */
-       private $slugger;
-
-       /**
-        * @var Translator instance
-        */
-       private $translator;
-
        /**
         * Constructor
         *
        /**
         * Constructor
         *
-        * @param ImageUtil $image
-        * @param SluggerUtil $slugger
-        * @param TranslatorInterface $translator The translator instance
+        * @param ?ImageUtil $image
+        * @param ?SluggerUtil $slugger
+        * @param ?TranslatorInterface $translator The translator instance
         */
         */
-       public function __construct(ImageUtil $image, SluggerUtil $slugger, TranslatorInterface $translator) {
-               //Set image
-               $this->image = $image;
-
-               //Set slugger
-               $this->slugger = $slugger;
-
-               //Set translator
-               $this->translator = $translator;
+       public function __construct(protected ?ImageUtil $image = null, protected ?SluggerUtil $slugger = null, protected ?TranslatorInterface $translator = null) {
        }
 
        /**
        }
 
        /**
-        * Build form
-        *
         * {@inheritdoc}
         * {@inheritdoc}
+        *
+        * Build form
         */
        public function buildForm(FormBuilderInterface $builder, array $options): void {
         */
        public function buildForm(FormBuilderInterface $builder, array $options): void {
-               //Set captcha
-               $captcha = $this->image->getCaptcha((new \DateTime('-1 year'))->getTimestamp());
+               //With image, slugger and translator
+               if ($this->image !== null && $this->slugger !== null && $this->translator !== null) {
+                       //Set captcha
+                       $captcha = $this->image->getCaptcha((new \DateTime('-1 year'))->getTimestamp());
 
 
-               //Add captcha token
-               $builder->add('_captcha_token', HiddenType::class, ['data' => $captcha['token'], 'empty_data' => $captcha['token']]);
+                       //Add captcha token
+                       $builder->add('_captcha_token', HiddenType::class, ['data' => $captcha['token'], 'empty_data' => $captcha['token'], 'mapped' => false]);
 
 
-               //Add captcha
-               $builder->add('captcha', IntegerType::class, ['label_attr' => ['class' => 'captcha'], 'label' => '<img src="'.htmlentities($captcha['src']).'" alt="'.htmlentities($captcha['equation']).'" />', 'label_html' => true, 'translation_domain' => false]);
+                       //Add captcha
+                       $builder->add('captcha', IntegerType::class, ['label_attr' => ['class' => 'captcha'], 'label' => '<img src="'.htmlentities($captcha['src']).'" alt="'.htmlentities($captcha['equation']).'" />', 'label_html' => true, 'mapped' => false, 'translation_domain' => false]);
 
 
-               //Add event listener on captcha
-               $builder->addEventListener(FormEvents::PRE_SUBMIT, [$this, 'validateCaptcha']);
-    }
+                       //Add event listener on captcha
+                       $builder->addEventListener(FormEvents::PRE_SUBMIT, [$this, 'validateCaptcha']);
+               }
+       }
 
        /**
         * Validate captcha
 
        /**
         * Validate captcha