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