vendor/excelwebzone/recaptcha-bundle/src/Form/Type/EWZRecaptchaType.php line 15

Open in your IDE?
  1. <?php
  2. namespace EWZ\Bundle\RecaptchaBundle\Form\Type;
  3. use EWZ\Bundle\RecaptchaBundle\Locale\LocaleResolver;
  4. use Symfony\Component\Form\Extension\Core\Type\TextType;
  5. use Symfony\Component\Form\FormInterface;
  6. use Symfony\Component\Form\FormView;
  7. use Symfony\Component\Form\AbstractType;
  8. use Symfony\Component\OptionsResolver\OptionsResolver;
  9. /**
  10.  * A field for entering a recaptcha text.
  11.  */
  12. class EWZRecaptchaType extends AbstractType
  13. {
  14.     /**
  15.      * The reCAPTCHA server URL.
  16.      * 
  17.      * @var string
  18.      */
  19.     protected $recaptchaApiServer;
  20.     
  21.     /**
  22.      * The reCAPTCHA JS server URL.
  23.      * 
  24.      * @var string
  25.      */
  26.     protected $recaptchaApiJsServer;
  27.     /**
  28.      * The public key.
  29.      *
  30.      * @var string
  31.      */
  32.     protected $publicKey;
  33.     /**
  34.      * The API server host name.
  35.      *
  36.      * @var string
  37.      */
  38.     protected $apiHost;
  39.     /**
  40.      * Enable recaptcha?
  41.      *
  42.      * @var bool
  43.      */
  44.     protected $enabled;
  45.     /**
  46.      * Use AJAX api?
  47.      *
  48.      * @var bool
  49.      */
  50.     protected $ajax;
  51.     /**
  52.      * @var LocaleResolver
  53.      */
  54.     protected $localeResolver;
  55.     /**
  56.      * @param string         $publicKey      Recaptcha public key
  57.      * @param bool           $enabled        Recaptcha status
  58.      * @param bool           $ajax           Ajax status
  59.      * @param LocaleResolver $localeResolver
  60.      */
  61.     public function __construct($publicKey$enabled$ajaxLocaleResolver $localeResolver$apiHost 'www.google.com')
  62.     {
  63.         $this->publicKey $publicKey;
  64.         $this->enabled $enabled;
  65.         $this->ajax $ajax;
  66.         $this->apiHost $apiHost;
  67.         $this->localeResolver $localeResolver;
  68.         $this->recaptchaApiJsServer sprintf('//%s/recaptcha/api/js/recaptcha_ajax.js'$apiHost);
  69.         $this->recaptchaApiServer sprintf('https://%s/recaptcha/api.js'$apiHost);
  70.     }
  71.     /**
  72.      * {@inheritdoc}
  73.      */
  74.     public function buildView(FormView $viewFormInterface $form, array $options)
  75.     {
  76.         $view->vars array_replace($view->vars, array(
  77.             'ewz_recaptcha_enabled' => $this->enabled,
  78.             'ewz_recaptcha_ajax' => $this->ajax,
  79.             'ewz_recaptcha_apihost' => $this->apiHost
  80.         ));
  81.         if (!$this->enabled) {
  82.             return;
  83.         }
  84.         if (!isset($options['language'])) {
  85.             $options['language'] = $this->localeResolver->resolve();
  86.         }
  87.         if (!$this->ajax) {
  88.             $view->vars array_replace($view->vars, array(
  89.                 'url_challenge' => sprintf('%s?hl=%s'$this->recaptchaApiServer$options['language']),
  90.                 'public_key' => $this->publicKey,
  91.             ));
  92.         } else {
  93.             $view->vars array_replace($view->vars, array(
  94.                 'url_api' => $this->recaptchaApiJsServer,
  95.                 'public_key' => $this->publicKey,
  96.             ));
  97.         }
  98.     }
  99.     /**
  100.      * {@inheritdoc}
  101.      */
  102.     public function configureOptions(OptionsResolver $resolver)
  103.     {
  104.         $resolver->setDefaults(array(
  105.             'compound' => false,
  106.             'language' => $this->localeResolver->resolve(),
  107.             'public_key' => null,
  108.             'url_challenge' => null,
  109.             'url_noscript' => null,
  110.             'attr' => array(
  111.                 'options' => array(
  112.                     'theme' => 'light',
  113.                     'type' => 'image',
  114.                     'size' => 'normal',
  115.                     'callback' => null,
  116.                     'expiredCallback' => null,
  117.                     'bind' => null,
  118.                     'defer' => false,
  119.                     'async' => false,
  120.                     'badge' => null,
  121.                 ),
  122.             ),
  123.         ));
  124.     }
  125.     /**
  126.      * {@inheritdoc}
  127.      */
  128.     public function getParent()
  129.     {
  130.         return TextType::class;
  131.     }
  132.     /**
  133.      * {@inheritdoc}
  134.      */
  135.     public function getBlockPrefix()
  136.     {
  137.         return 'ewz_recaptcha';
  138.     }
  139.     /**
  140.      * Gets the Javascript source URLs.
  141.      *
  142.      * @param string $key The script name
  143.      *
  144.      * @return string The javascript source URL
  145.      */
  146.     public function getScriptURL($key)
  147.     {
  148.         return isset($this->scripts[$key]) ? $this->scripts[$key] : null;
  149.     }
  150.     /**
  151.      * Gets the public key.
  152.      *
  153.      * @return string The javascript source URL
  154.      */
  155.     public function getPublicKey()
  156.     {
  157.         return $this->publicKey;
  158.     }
  159.     /**
  160.      * Gets the API host name.
  161.      *
  162.      * @return string The hostname for API
  163.      */
  164.     public function getApiHost()
  165.     {
  166.         return $this->apiHost;
  167.     }
  168. }