• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

contributte / reCAPTCHA / 5447406801

pending completion
5447406801

push

github

f3l1x
Phpstan: fixes

14 of 14 new or added lines in 3 files covered. (100.0%)

115 of 132 relevant lines covered (87.12%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.76
/src/Forms/InvisibleReCaptchaField.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\ReCaptcha\Forms;
4

5
use Contributte\ReCaptcha\ReCaptchaProvider;
6
use Nette\Forms\Controls\HiddenField;
7
use Nette\Forms\Form;
8
use Nette\Forms\Rules;
9
use Nette\Utils\Html;
10

11
class InvisibleReCaptchaField extends HiddenField
12
{
13

14
        private ReCaptchaProvider $provider;
15

16
        private bool $configured = false;
17

18
        private ?string $message = null;
19

20
        public function __construct(ReCaptchaProvider $provider, ?string $message = null)
1✔
21
        {
22
                parent::__construct();
1✔
23

24
                $this->provider = $provider;
1✔
25

26
                $this->setOmitted(true);
1✔
27
                $this->control = Html::el('div');
1✔
28
                $this->control->addClass('g-recaptcha');
1✔
29

30
                $this->message = $message;
1✔
31
        }
1✔
32

33
        public function loadHttpData(): void
34
        {
35
                parent::loadHttpData();
1✔
36

37
                $form = $this->getForm();
1✔
38
                assert($form !== null);
1✔
39
                $this->setValue($form->getHttpData(Form::DATA_TEXT, ReCaptchaProvider::FORM_PARAMETER));
1✔
40
        }
1✔
41

42
        public function setMessage(string $message): self
43
        {
44
                $this->message = $message;
×
45

46
                return $this;
×
47
        }
48

49
        public function validate(): void
50
        {
51
                $this->configureValidation();
×
52

53
                parent::validate();
×
54
        }
55

56
        public function getRules(): Rules
57
        {
58
                $this->configureValidation();
×
59

60
                return parent::getRules();
×
61
        }
62

63
        public function verify(): bool
64
        {
65
                return $this->provider->validateControl($this) === true;
×
66
        }
67

68
        public function getControl(?string $caption = null): Html
1✔
69
        {
70
                $this->configureValidation();
1✔
71

72
                $el = parent::getControl();
1✔
73
                $el->addAttributes([
1✔
74
                        'data-sitekey' => $this->provider->getSiteKey(),
1✔
75
                        'data-size' => 'invisible',
1✔
76
                ]);
77

78
                return $el;
1✔
79
        }
80

81
        private function configureValidation(): void
82
        {
83
                if ($this->configured) {
1✔
84
                        return;
×
85
                }
86

87
                $message = $this->message ?? 'Are you a bot?';
1✔
88
                $this->addRule(fn ($code): bool => $this->verify() === true, $message);
1✔
89
                $this->configured = true;
1✔
90
        }
1✔
91

92
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc