|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Didatus\TextCaptcha; |
|
4
|
|
|
|
|
5
|
|
|
use Didatus\TextCaptcha\Generator\Arithmetical; |
|
6
|
|
|
use Didatus\TextCaptcha\Generator\Month; |
|
7
|
|
|
use Didatus\TextCaptcha\Generator\Week; |
|
8
|
|
|
use Symfony\Component\Translation\Loader\PhpFileLoader; |
|
9
|
|
|
use Symfony\Component\Translation\Translator; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class TextCaptchaHandler |
|
13
|
|
|
* @package Didatus\TextCaptcha |
|
14
|
|
|
*/ |
|
15
|
|
|
class TextCaptchaHandler |
|
16
|
|
|
{ |
|
17
|
|
|
private $translator; |
|
18
|
|
|
|
|
19
|
|
|
private $generators = []; |
|
20
|
|
|
|
|
21
|
8 |
|
public function __construct($locale = 'de_DE') |
|
22
|
|
|
{ |
|
23
|
8 |
|
$this->translator = $this->initiateTranslator($locale); |
|
24
|
|
|
|
|
25
|
8 |
|
$this->registerCaptchaGenerator(Week::class); |
|
26
|
8 |
|
$this->registerCaptchaGenerator(Month::class); |
|
27
|
8 |
|
$this->registerCaptchaGenerator(Arithmetical::class); |
|
28
|
8 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return Translator |
|
32
|
|
|
*/ |
|
33
|
8 |
|
public function getTranslator(): Translator |
|
34
|
|
|
{ |
|
35
|
8 |
|
return $this->translator; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
public function generateTextCaptcha() |
|
39
|
|
|
{ |
|
40
|
1 |
|
$generator = $this->generators[rand(0, count($this->generators))]; |
|
41
|
|
|
|
|
42
|
1 |
|
return $generator->getTextCaptcha(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
8 |
|
private function registerCaptchaGenerator(string $generatorName) |
|
46
|
|
|
{ |
|
47
|
8 |
|
$this->generators[] = new $generatorName($this->translator); |
|
48
|
8 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return Translator |
|
52
|
|
|
*/ |
|
53
|
8 |
|
private function initiateTranslator($locale):Translator |
|
54
|
|
|
{ |
|
55
|
8 |
|
$translator = new Translator($locale); |
|
56
|
8 |
|
$translator->addLoader('phpfile', new PhpFileLoader()); |
|
57
|
8 |
|
$translator->addResource('phpfile', __DIR__ . '/../translations/mathematical.de.php', 'de_DE'); |
|
58
|
8 |
|
$translator->addResource('phpfile', __DIR__ . '/../translations/mathematical.en.php', 'en_US'); |
|
59
|
8 |
|
$translator->addResource('phpfile', __DIR__ . '/../translations/calender.de.php', 'de_DE'); |
|
60
|
8 |
|
$translator->addResource('phpfile', __DIR__ . '/../translations/calender.en.php', 'en_US'); |
|
61
|
|
|
|
|
62
|
8 |
|
return $translator; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|