-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: Extract phpweb\Spam\Challenge
- Loading branch information
1 parent
b95f3e7
commit a88faf2
Showing
6 changed files
with
202 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpweb\Spam; | ||
|
||
final readonly class Challenge | ||
{ | ||
private const NUMBERS = [ | ||
0 => 'zero', | ||
1 => 'one', | ||
2 => 'two', | ||
3 => 'three', | ||
4 => 'four', | ||
5 => 'five', | ||
6 => 'six', | ||
7 => 'seven', | ||
8 => 'eight', | ||
9 => 'nine', | ||
]; | ||
|
||
private const CHALLENGES = [ | ||
// name, print, generator | ||
'max' => [ | ||
'max', | ||
[ | ||
self::class, | ||
'print_prefix', | ||
], | ||
], | ||
'min' => [ | ||
'min', | ||
[ | ||
self::class, | ||
'print_prefix', | ||
], | ||
], | ||
'minus' => [ | ||
[ | ||
self::class, | ||
'minus', | ||
], | ||
[ | ||
self::class, | ||
'print_infix', | ||
], | ||
[ | ||
self::class, | ||
'gen_minus', | ||
], | ||
], | ||
'plus' => [ | ||
[ | ||
self::class, | ||
'plus', | ||
], | ||
[ | ||
self::class, | ||
'print_infix', | ||
], | ||
[ | ||
self::class, | ||
'gen_plus', | ||
], | ||
], | ||
]; | ||
|
||
private function __construct( | ||
public string $function, | ||
public string $argumentOne, | ||
public string $argumentTwo, | ||
public string $question, | ||
) { | ||
} | ||
|
||
public static function create(): self | ||
{ | ||
$function = array_rand(self::CHALLENGES); | ||
|
||
$challenge = self::CHALLENGES[$function]; | ||
|
||
$a = mt_rand(0, 9); | ||
$argumentOne = self::NUMBERS[$a]; | ||
|
||
$b = isset($challenge[2]) ? $challenge[2]($a) : mt_rand(0, 9); | ||
$argumentTwo = self::NUMBERS[$b]; | ||
|
||
$question = $challenge[1]( | ||
$function, | ||
$argumentOne, | ||
$argumentTwo, | ||
); | ||
|
||
return new self( | ||
$function, | ||
$argumentOne, | ||
$argumentTwo, | ||
$question, | ||
); | ||
} | ||
|
||
public static function isValidAnswer( | ||
string $function, | ||
string $argumentOne, | ||
string $argumentTwo, | ||
string $answer, | ||
): bool { | ||
if (!array_key_exists($function, self::CHALLENGES)) { | ||
return false; | ||
} | ||
|
||
$challenge = self::CHALLENGES[$function]; | ||
|
||
$a = array_search($argumentOne, self::NUMBERS, true); | ||
|
||
if (!is_int($a)) { | ||
return false; | ||
} | ||
|
||
$b = array_search($argumentTwo, self::NUMBERS, true); | ||
|
||
if (!is_int($b)) { | ||
return false; | ||
} | ||
|
||
$expected = self::NUMBERS[$challenge[0]($a, $b)]; | ||
|
||
return $expected === $answer; | ||
} | ||
|
||
private static function plus(int $a, int $b): int | ||
{ | ||
return $a + $b; | ||
} | ||
|
||
private static function gen_plus(int $a): int | ||
{ | ||
return mt_rand(0, 9 - $a); | ||
} | ||
|
||
private static function minus(int $a, int $b): int | ||
{ | ||
return $a - $b; | ||
} | ||
|
||
private static function gen_minus(int $a): int | ||
{ | ||
return mt_rand(0, $a); | ||
} | ||
|
||
private static function print_infix(string $name, string $a, string $b): string | ||
{ | ||
return "$a $name $b"; | ||
} | ||
|
||
private static function print_prefix(string $name, string $a, string $b): string | ||
{ | ||
return "$name($a, $b)"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 11 additions & 7 deletions
18
tests/gen-challenge.phpt → tests/Spam/Challenge/random.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters