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