Skip to content

Commit ffb971a

Browse files
committed
fix: resolve QA errors and merge docs into single README
- Fix phpstan errors (type safety, return types, mixed handling) - Fix codesniffer errors (method order, parent spacing, annotations) - Merge seznamcaptcha docs into main README.md - Remove @license annotations (forbidden by ruleset)
1 parent 96e5855 commit ffb971a

10 files changed

Lines changed: 229 additions & 219 deletions

File tree

.docs/README.md

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
- Controls
1010
- [Date/time inputs](#date-time-inputs) (DateTimeInput, DateInput, TimeInput)
1111
- Captcha
12-
- [Seznam Captcha](seznamcaptcha/README.md)
12+
- [Seznam Captcha](#seznam-captcha)
1313

1414
## Setup
1515

@@ -348,3 +348,118 @@ $control->setValueAs(DateTime::class); // value in input timezone as \DateTime
348348
$control->setValueInTzAs(DateTime::class); // value in server default timezone as \DateTime
349349
$control->setValueInTzAs(DateTime::class, new DateTimeZone('Americe/New_York')); // value in given timezone as \DateTime
350350
```
351+
352+
## Captcha
353+
354+
### Seznam Captcha
355+
356+
Integration with [Seznam.cz Captcha](https://captcha.seznam.cz) service.
357+
358+
#### Usage
359+
360+
```neon
361+
extensions:
362+
captcha: Contributte\Forms\Captcha\Seznam\DI\SeznamCaptchaExtension
363+
```
364+
365+
#### Configuration
366+
367+
By default is `auto: on` and `method: http`, you can disable it and bind addCaptcha to your forms by yourself.
368+
369+
```neon
370+
captcha:
371+
auto: off # on | off
372+
method: xmlrpc # http | xmlrpc
373+
```
374+
375+
#### Form
376+
377+
![captcha](seznamcaptcha/captcha.png)
378+
379+
Just register an extension and keep `auto` argument as it is.
380+
381+
```php
382+
use Nette\Application\UI\Form;
383+
384+
protected function createComponentForm(): Form
385+
{
386+
$form = new Form();
387+
388+
$form->addCaptcha('captcha')
389+
->setRequired('Are you robot?');
390+
391+
$form->addSubmit('send');
392+
393+
$form->onSuccess[] = function (Form $form): void {
394+
dump($form['captcha']);
395+
};
396+
397+
return $form;
398+
}
399+
```
400+
401+
#### Rendering
402+
403+
##### Automatic
404+
405+
```latte
406+
{control form}
407+
```
408+
409+
##### Manual
410+
411+
It needs a `CaptchaContainer` consists of 2 inputs `image` and `code`.
412+
413+
```latte
414+
<form n:name="form">
415+
{input captcha-image}
416+
{input captcha-code}
417+
</form>
418+
```
419+
420+
#### Example
421+
422+
```php
423+
use Contributte\Forms\Captcha\Seznam\CaptchaValidator;
424+
use Contributte\Forms\Captcha\Seznam\Forms\CaptchaHash;
425+
use Contributte\Forms\Captcha\Seznam\Forms\CaptchaImage;
426+
use Contributte\Forms\Captcha\Seznam\Forms\CaptchaInput;
427+
use Contributte\Forms\Captcha\Seznam\Provider\ProviderFactory;
428+
use Nette\Application\UI\Form;
429+
430+
/** @var ProviderFactory @inject */
431+
public ProviderFactory $providerFactory;
432+
433+
protected function createComponentForm(): Form
434+
{
435+
$form = new Form();
436+
437+
$provider = $this->providerFactory->create();
438+
$form['image'] = new CaptchaImage('Captcha', $provider);
439+
$form['hash'] = new CaptchaHash($provider);
440+
$form['code'] = new CaptchaInput('Code');
441+
442+
$form->addSubmit('send');
443+
444+
$form->onValidate[] = function (Form $form) use ($provider): void {
445+
$validator = new CaptchaValidator($provider);
446+
447+
$hash = $form['hash']->getHttpHash();
448+
$code = $form['code']->getHttpCode();
449+
450+
if ($validator->validate($code, $hash) !== true) {
451+
$form->addError('Are you robot?');
452+
}
453+
};
454+
455+
$form->onSuccess[] = function (Form $form): void {
456+
dump($form);
457+
};
458+
459+
return $form;
460+
}
461+
```
462+
463+
For better usability add this functionality to your `BaseForms`, `BaseFormFactory` or something like this.
464+
465+
You can also create a trait for it.

.docs/seznamcaptcha/README.md

Lines changed: 0 additions & 122 deletions
This file was deleted.

src/Captcha/Seznam/Backend/Client.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33
namespace Contributte\Forms\Captcha\Seznam\Backend;
44

5-
use RuntimeException;
6-
7-
/**
8-
* @license https://github.com/seznam/captcha-client
9-
*/
105
abstract class Client
116
{
127

src/Captcha/Seznam/Backend/HttpClient.php

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,9 @@
44

55
use RuntimeException;
66

7-
/**
8-
* @license https://github.com/seznam/captcha-client
9-
*/
107
class HttpClient extends Client
118
{
129

13-
/**
14-
* @param array<string, mixed> $params
15-
* @return array{status: int, data: string|false}
16-
*/
17-
protected function call(string $methodName, array $params = []): array
18-
{
19-
$url = sprintf('https://%s:%d/%s?%s', $this->serverHostname, $this->serverPort, $methodName, http_build_query($params));
20-
$ch = curl_init($url);
21-
22-
if ($ch === false) {
23-
throw new RuntimeException('Failed to initialize curl');
24-
}
25-
26-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
27-
28-
if ($this->proxyHostname !== null) {
29-
curl_setopt($ch, CURLOPT_PROXY, $this->proxyHostname);
30-
curl_setopt($ch, CURLOPT_PROXYPORT, $this->proxyPort);
31-
}
32-
33-
$response = curl_exec($ch);
34-
$info = curl_getinfo($ch);
35-
curl_close($ch);
36-
37-
return [
38-
'status' => (int) $info['http_code'],
39-
'data' => $response,
40-
];
41-
}
42-
4310
public function create(): string
4411
{
4512
$result = $this->call('captcha.create');
@@ -84,4 +51,35 @@ public function check(string $hash, string $code): bool
8451
return $result['status'] === 200;
8552
}
8653

54+
/**
55+
* @param array<string, mixed> $params
56+
* @return array{status: int, data: string|false}
57+
*/
58+
protected function call(string $methodName, array $params = []): array
59+
{
60+
$url = sprintf('https://%s:%d/%s?%s', $this->serverHostname, $this->serverPort, $methodName, http_build_query($params));
61+
$ch = curl_init($url);
62+
63+
if ($ch === false) {
64+
throw new RuntimeException('Failed to initialize curl');
65+
}
66+
67+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
68+
69+
if ($this->proxyHostname !== null) {
70+
curl_setopt($ch, CURLOPT_PROXY, $this->proxyHostname);
71+
curl_setopt($ch, CURLOPT_PROXYPORT, $this->proxyPort);
72+
}
73+
74+
/** @var string|false $response */
75+
$response = curl_exec($ch);
76+
$info = curl_getinfo($ch);
77+
curl_close($ch);
78+
79+
return [
80+
'status' => $info['http_code'],
81+
'data' => $response,
82+
];
83+
}
84+
8785
}

0 commit comments

Comments
 (0)