|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Contributte\Forms\Captcha\Seznam\Form; |
| 4 | + |
| 5 | +use Contributte\Forms\Captcha\Seznam\Factory; |
| 6 | +use Contributte\Forms\Captcha\Seznam\Provider\Provider; |
| 7 | +use Contributte\Forms\Captcha\Seznam\Validator\Validator; |
| 8 | +use Nette\Forms\Container; |
| 9 | +use Nette\Forms\Controls\BaseControl; |
| 10 | +use Nette\Forms\Controls\HiddenField; |
| 11 | +use Nette\Forms\Controls\TextInput; |
| 12 | +use Nette\Forms\Form; |
| 13 | +use Nette\Utils\Html; |
| 14 | + |
| 15 | +class SeznamCaptchaContainer extends Container |
| 16 | +{ |
| 17 | + |
| 18 | + private Validator $validator; |
| 19 | + |
| 20 | + private Provider $provider; |
| 21 | + |
| 22 | + public function __construct(Factory $factory) |
| 23 | + { |
| 24 | + $this->provider = $factory->createProvider(); |
| 25 | + $this->validator = $factory->createValidator(); |
| 26 | + |
| 27 | + $imageControl = new class ('Captcha') extends BaseControl { |
| 28 | + |
| 29 | + private string $imageUrl = ''; |
| 30 | + |
| 31 | + public function __construct(string $label) |
| 32 | + { |
| 33 | + parent::__construct($label); |
| 34 | + |
| 35 | + $this->control = Html::el('img'); |
| 36 | + $this->control->addClass('captcha-image seznam-captcha-image'); |
| 37 | + } |
| 38 | + |
| 39 | + public function setImageUrl(string $url): void |
| 40 | + { |
| 41 | + $this->imageUrl = $url; |
| 42 | + } |
| 43 | + |
| 44 | + public function getControl(): Html |
| 45 | + { |
| 46 | + $img = parent::getControl(); |
| 47 | + assert($img instanceof Html); |
| 48 | + |
| 49 | + $img->addAttributes(['src' => $this->imageUrl]); |
| 50 | + |
| 51 | + return $img; |
| 52 | + } |
| 53 | + |
| 54 | + }; |
| 55 | + $imageControl->setImageUrl($this->provider->getImage()); |
| 56 | + |
| 57 | + $codeInput = new TextInput('Code', 5); |
| 58 | + $codeInput->getControlPrototype()->addClass('captcha-input seznam-captcha-input'); |
| 59 | + |
| 60 | + $hashField = new HiddenField($this->provider->getHash()); |
| 61 | + |
| 62 | + $this['image'] = $imageControl; |
| 63 | + $this['code'] = $codeInput; |
| 64 | + $this['hash'] = $hashField; |
| 65 | + } |
| 66 | + |
| 67 | + public function getImage(): BaseControl |
| 68 | + { |
| 69 | + $control = $this->getComponent('image'); |
| 70 | + assert($control instanceof BaseControl); |
| 71 | + |
| 72 | + return $control; |
| 73 | + } |
| 74 | + |
| 75 | + public function getCode(): TextInput |
| 76 | + { |
| 77 | + $control = $this->getComponent('code'); |
| 78 | + assert($control instanceof TextInput); |
| 79 | + |
| 80 | + return $control; |
| 81 | + } |
| 82 | + |
| 83 | + public function getHash(): HiddenField |
| 84 | + { |
| 85 | + $control = $this->getComponent('hash'); |
| 86 | + assert($control instanceof HiddenField); |
| 87 | + |
| 88 | + return $control; |
| 89 | + } |
| 90 | + |
| 91 | + public function verify(): bool |
| 92 | + { |
| 93 | + /** @var Form $form */ |
| 94 | + $form = $this->getForm(); |
| 95 | + |
| 96 | + /** @var string $hash */ |
| 97 | + $hash = $form->getHttpData(Form::DataLine, $this->getHash()->getHtmlName()); |
| 98 | + |
| 99 | + /** @var string $code */ |
| 100 | + $code = $form->getHttpData(Form::DataLine, $this->getCode()->getHtmlName()); |
| 101 | + |
| 102 | + return $this->validator->validate($code, $hash); |
| 103 | + } |
| 104 | + |
| 105 | + public function getValidator(): Validator |
| 106 | + { |
| 107 | + return $this->validator; |
| 108 | + } |
| 109 | + |
| 110 | + public function getProvider(): Provider |
| 111 | + { |
| 112 | + return $this->provider; |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments