summaryrefslogtreecommitdiff
path: root/src/Captcha.php
blob: c60a18691b0c31a58f0274069500f47f896da912 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
// src/Captcha.php

declare(strict_types=1);

class Captcha
{
	private int $a;
	private int $b;
	private int $solution;

	public function __construct(){
		$this->a = rand(2, 9);
		$this->b = rand(2, 9);
		$this->solution = $this->a * $this->b;
	}

	public function getA(): string
	{
		return $this->toLettersFrench($this->a);
	}
	public function getB(): string
	{
		return $this->toLettersFrench($this->b);
	}
	public function getSolution(): int
	{
		return $this->solution;
	}

	private function toLettersFrench(int $number): string
	{
		return match($number){
			2 => 'deux',
			3 => 'trois',
			4 => 'quatre',
			5 => 'cinq',
			6 => 'six',
			7 => 'sept',
			8 => 'huit',
			9 => 'neuf',
			default => '', // erreur
		};
	}
	static public function controlInput(string $input = '0'): int
	{
	    // un POST est une chaîne qu'on doit convertir en nombre dans deux conditions:
	    // test de format: $input est un nombre
	    // test d'intégrité: supprimer les décimales avec (int) ne change pas la valeur du nombre
	    return is_numeric($input) && $input == (int)$input ? (int)$input : 0;
	}
}