diff options
Diffstat (limited to 'src/Captcha.php')
-rw-r--r-- | src/Captcha.php | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/Captcha.php b/src/Captcha.php new file mode 100644 index 0000000..c60a186 --- /dev/null +++ b/src/Captcha.php | |||
@@ -0,0 +1,52 @@ | |||
1 | <?php | ||
2 | // src/Captcha.php | ||
3 | |||
4 | declare(strict_types=1); | ||
5 | |||
6 | class Captcha | ||
7 | { | ||
8 | private int $a; | ||
9 | private int $b; | ||
10 | private int $solution; | ||
11 | |||
12 | public function __construct(){ | ||
13 | $this->a = rand(2, 9); | ||
14 | $this->b = rand(2, 9); | ||
15 | $this->solution = $this->a * $this->b; | ||
16 | } | ||
17 | |||
18 | public function getA(): string | ||
19 | { | ||
20 | return $this->toLettersFrench($this->a); | ||
21 | } | ||
22 | public function getB(): string | ||
23 | { | ||
24 | return $this->toLettersFrench($this->b); | ||
25 | } | ||
26 | public function getSolution(): int | ||
27 | { | ||
28 | return $this->solution; | ||
29 | } | ||
30 | |||
31 | private function toLettersFrench(int $number): string | ||
32 | { | ||
33 | return match($number){ | ||
34 | 2 => 'deux', | ||
35 | 3 => 'trois', | ||
36 | 4 => 'quatre', | ||
37 | 5 => 'cinq', | ||
38 | 6 => 'six', | ||
39 | 7 => 'sept', | ||
40 | 8 => 'huit', | ||
41 | 9 => 'neuf', | ||
42 | default => '', // erreur | ||
43 | }; | ||
44 | } | ||
45 | static public function controlInput(string $input = '0'): int | ||
46 | { | ||
47 | // un POST est une chaîne qu'on doit convertir en nombre dans deux conditions: | ||
48 | // test de format: $input est un nombre | ||
49 | // test d'intégrité: supprimer les décimales avec (int) ne change pas la valeur du nombre | ||
50 | return is_numeric($input) && $input == (int)$input ? (int)$input : 0; | ||
51 | } | ||
52 | } \ No newline at end of file | ||