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

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\OptionsResolver\OptionsResolver;
  8. /**
  9. * A field for entering a recaptcha text.
  10. */
  11. class EWZRecaptchaType extends AbstractEWZRecaptchaType
  12. {
  13. /**
  14. * Use AJAX api?
  15. *
  16. * @var bool
  17. */
  18. protected $ajax;
  19. /** @var LocaleResolver */
  20. protected $localeResolver;
  21. /**
  22. * @param string $publicKey Recaptcha public key
  23. * @param bool $enabled Recaptcha status
  24. * @param bool $ajax Ajax status
  25. * @param LocaleResolver $localeResolver
  26. */
  27. public function __construct(string $publicKey, bool $enabled, bool $ajax, LocaleResolver $localeResolver, string $apiHost = 'www.google.com')
  28. {
  29. parent::__construct($publicKey, $enabled, $apiHost);
  30. $this->ajax = $ajax;
  31. $this->localeResolver = $localeResolver;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function configureOptions(OptionsResolver $resolver): void
  37. {
  38. $resolver->setDefaults(array(
  39. 'compound' => false,
  40. 'language' => $this->localeResolver->resolve(),
  41. 'public_key' => null,
  42. 'url_challenge' => null,
  43. 'url_noscript' => null,
  44. 'attr' => array(
  45. 'options' => array(
  46. 'theme' => 'light',
  47. 'type' => 'image',
  48. 'size' => 'normal',
  49. 'callback' => null,
  50. 'expiredCallback' => null,
  51. 'bind' => null,
  52. 'defer' => false,
  53. 'async' => false,
  54. 'badge' => null,
  55. ),
  56. ),
  57. ));
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function getParent(): string
  63. {
  64. return TextType::class;
  65. }
  66. /**
  67. * Gets the Javascript source URLs.
  68. *
  69. * @param string $key The script name
  70. *
  71. * @return string The javascript source URL
  72. */
  73. public function getScriptURL($key)
  74. {
  75. return isset($this->scripts[$key]) ? $this->scripts[$key] : null;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. protected function addCustomVars(FormView $view, FormInterface $form, array $options): void
  81. {
  82. $view->vars = array_replace($view->vars, array(
  83. 'ewz_recaptcha_ajax' => $this->ajax,
  84. ));
  85. if (!isset($options['language'])) {
  86. $options['language'] = $this->localeResolver->resolve();
  87. }
  88. if (!$this->ajax) {
  89. $view->vars = array_replace($view->vars, array(
  90. 'url_challenge' => sprintf('%s?hl=%s', $this->recaptchaApiServer, $options['language']),
  91. ));
  92. } else {
  93. $view->vars = array_replace($view->vars, array(
  94. 'url_api' => sprintf('//%s/recaptcha/api/js/recaptcha_ajax.js', $this->apiHost),
  95. ));
  96. }
  97. }
  98. }