didatus /
TextCaptcha
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Didatus\TextCaptcha\Generator; |
||
| 4 | |||
| 5 | use Didatus\TextCaptcha\TextCaptcha; |
||
| 6 | use Symfony\Component\Translation\Translator; |
||
| 7 | |||
| 8 | class Arithmetical implements GeneratorInterface |
||
| 9 | { |
||
| 10 | private $translator; |
||
| 11 | |||
| 12 | private $numbers = [ |
||
| 13 | 'zero', |
||
| 14 | 'one', |
||
| 15 | 'two', |
||
| 16 | 'three', |
||
| 17 | 'four', |
||
| 18 | 'five', |
||
| 19 | 'six', |
||
| 20 | 'seven', |
||
| 21 | 'eight', |
||
| 22 | 'nine', |
||
| 23 | ]; |
||
| 24 | |||
| 25 | private $arithemticalFunctions = [ |
||
| 26 | '+' => 'plus', |
||
| 27 | '-' => 'minus', |
||
| 28 | ]; |
||
| 29 | |||
| 30 | private $captcha; |
||
| 31 | |||
| 32 | 8 | public function __construct(Translator $translator) |
|
| 33 | { |
||
| 34 | 8 | $this->translator = $translator; |
|
| 35 | 8 | } |
|
| 36 | |||
| 37 | 3 | public function getTextCaptcha(): TextCaptcha |
|
| 38 | { |
||
| 39 | 3 | if (is_null($this->captcha)) { |
|
| 40 | 3 | $this->generateQuestion(); |
|
| 41 | } |
||
| 42 | |||
| 43 | 3 | return $this->captcha; |
|
| 44 | } |
||
| 45 | |||
| 46 | 3 | private function generateQuestion() |
|
| 47 | { |
||
| 48 | 3 | $questionParts = []; |
|
| 49 | 3 | $questionParts[] = rand(0, 9); |
|
| 50 | 3 | $questionParts[] = array_rand($this->arithemticalFunctions); |
|
| 51 | 3 | $questionParts[] = rand(0, 9); |
|
| 52 | 3 | $questionParts[] = array_rand($this->arithemticalFunctions); |
|
| 53 | 3 | $questionParts[] = rand(0, 9); |
|
| 54 | |||
| 55 | 3 | $this->captcha = new TextCaptcha( |
|
| 56 | 3 | $this->getQuestionString($questionParts), |
|
| 57 | 3 | $this->calculateAnswer($questionParts) |
|
| 58 | ); |
||
| 59 | 3 | } |
|
| 60 | |||
| 61 | 3 | private function getQuestionString(array $questionParts):string |
|
| 62 | { |
||
| 63 | 3 | $question = ''; |
|
| 64 | 3 | foreach ($questionParts as $key => $part) { |
|
| 65 | 3 | if ($key % 2 == 0) { |
|
| 66 | 3 | $question .= $this->translator->trans($this->numbers[$part]); |
|
| 67 | } else { |
||
| 68 | 3 | $question .= $this->translator->trans($this->arithemticalFunctions[$part]); |
|
| 69 | } |
||
| 70 | 3 | $question .= ' '; |
|
| 71 | } |
||
| 72 | |||
| 73 | /* |
||
| 74 | $question = ''; |
||
| 75 | $question .= $this->numbers[$questionParts[0]]; |
||
| 76 | $question .= ' ' . $this->arithemticalFunctions[$questionParts[1]] . ' '; |
||
| 77 | $question .= $this->numbers[$questionParts[2]]; |
||
| 78 | $question .= ' ' . $this->arithemticalFunctions[$questionParts[3]] . ' '; |
||
| 79 | $question .= $this->numbers[$questionParts[4]]; |
||
| 80 | */ |
||
| 81 | |||
| 82 | 3 | return rtrim($question); |
|
| 83 | } |
||
| 84 | |||
| 85 | 3 | private function calculateAnswer(array $questionParts): string |
|
| 86 | { |
||
| 87 | 3 | $result = array_shift($questionParts); |
|
| 88 | |||
| 89 | 3 | for ($i = 0; $i < count($questionParts); $i=$i+2) { |
|
|
0 ignored issues
–
show
|
|||
| 90 | 3 | switch($questionParts[$i]) { |
|
| 91 | 3 | case '+': |
|
| 92 | 3 | $result = $result + $questionParts[$i+1]; |
|
| 93 | 3 | break; |
|
| 94 | 2 | case '-': |
|
| 95 | 2 | $result = $result - $questionParts[$i+1]; |
|
| 96 | 2 | break; |
|
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | 3 | return $result; |
|
| 101 | } |
||
| 102 | } |
||
| 103 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: