aboutsummaryrefslogtreecommitdiff
path: root/src/Captcha.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Captcha.php')
-rw-r--r--src/Captcha.php55
1 files changed, 0 insertions, 55 deletions
diff --git a/src/Captcha.php b/src/Captcha.php
deleted file mode 100644
index a0c7a54..0000000
--- a/src/Captcha.php
+++ /dev/null
@@ -1,55 +0,0 @@
1<?php
2// src/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
9class 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