-
Notifications
You must be signed in to change notification settings - Fork 554
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
77a0620
commit a076df1
Showing
6 changed files
with
241 additions
and
91 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,203 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpweb\Spam; | ||
|
||
final class Challenge | ||
{ | ||
private const NUM = array( | ||
0 => 'zero', | ||
1 => 'one', | ||
2 => 'two', | ||
3 => 'three', | ||
4 => 'four', | ||
5 => 'five', | ||
6 => 'six', | ||
7 => 'seven', | ||
8 => 'eight', | ||
9 => 'nine', | ||
); | ||
|
||
private const CHALLENGES = array( | ||
// name, print, generator | ||
'max' => array( | ||
'max', | ||
[ | ||
self::class, | ||
'print_prefix' | ||
], | ||
), | ||
'min' => array( | ||
'min', | ||
[ | ||
self::class, | ||
'print_prefix' | ||
], | ||
), | ||
'minus' => array( | ||
[ | ||
self::class, | ||
'minus' | ||
], | ||
[ | ||
self::class, | ||
'print_infix' | ||
], | ||
[ | ||
self::class, | ||
'gen_minus' | ||
], | ||
), | ||
'plus' => array( | ||
[ | ||
self::class, | ||
'plus' | ||
], | ||
[ | ||
self::class, | ||
'print_infix' | ||
], | ||
[ | ||
self::class, | ||
'gen_plus' | ||
], | ||
), | ||
); | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $function; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $argumentOne; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $argumentTwo; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $question; | ||
|
||
private function __construct( | ||
string $function, | ||
string $argumentOne, | ||
string $argumentTwo, | ||
string $question | ||
) { | ||
$this->function = $function; | ||
$this->argumentOne = $argumentOne; | ||
$this->argumentTwo = $argumentTwo; | ||
$this->question = $question; | ||
} | ||
|
||
public static function create(): self | ||
{ | ||
$function = array_rand(self::CHALLENGES); | ||
|
||
$challenge = self::CHALLENGES[$function]; | ||
|
||
$a = rand(0, 9); | ||
$argumentOne = self::NUM[$a]; | ||
$b = isset($challenge[2]) ? $challenge[2]($a) : rand(0, 9); | ||
$argumentTwo = self::NUM[$b]; | ||
|
||
$question = $challenge[1]( | ||
$function, | ||
$argumentOne, | ||
$argumentTwo | ||
); | ||
|
||
return new self( | ||
$function, | ||
$argumentOne, | ||
$argumentTwo, | ||
$question | ||
); | ||
} | ||
|
||
public static function isValidAnswer( | ||
$function, | ||
$argumentOne, | ||
$argumentTwo, | ||
$answer | ||
): bool { | ||
if (!is_string($function)) { | ||
return false; | ||
} | ||
|
||
if (!array_key_exists($function, self::CHALLENGES)) { | ||
return false; | ||
} | ||
|
||
$challenge = self::CHALLENGES[$function]; | ||
|
||
if (!is_array($challenge)) { | ||
return false; | ||
} | ||
|
||
$a = array_search($argumentOne, self::NUM, false); | ||
|
||
if (!is_int($a)) { | ||
return false; | ||
} | ||
|
||
$b = array_search($argumentTwo, self::NUM, false); | ||
|
||
if (!is_int($b)) { | ||
return false; | ||
} | ||
|
||
return self::NUM[$challenge[0]($a, $b)] === $answer; | ||
} | ||
|
||
public static function plus($a, $b) { | ||
return $a + $b; | ||
} | ||
|
||
public static function gen_plus($a) { | ||
return rand(0, 9 - $a); | ||
} | ||
|
||
public static function minus($a, $b) { | ||
return $a - $b; | ||
} | ||
|
||
public static function gen_minus($a) { | ||
return rand(0, $a); | ||
} | ||
|
||
public static function print_infix($name, $a, $b) { | ||
return "$a $name $b"; | ||
} | ||
|
||
public static function print_prefix($name, $a, $b) { | ||
return "$name($a, $b)"; | ||
} | ||
|
||
public function function(): string | ||
{ | ||
return $this->function; | ||
} | ||
|
||
public function argumentOne(): string | ||
{ | ||
return $this->argumentOne; | ||
} | ||
|
||
public function argumentTwo(): string | ||
{ | ||
return $this->argumentTwo; | ||
} | ||
|
||
public function question(): string | ||
{ | ||
return $this->question; | ||
} | ||
} |
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
Oops, something went wrong.