Passed
Pull Request — master (#194)
by
unknown
14:54
created

Basic::rules()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
eloc 11
dl 0
loc 19
rs 9.9
c 4
b 1
f 1
cc 3
nc 1
nop 0
1
<?php
2
3
/** @noinspection PhpInconsistentReturnPointsInspection */
4
5
namespace WebDevEtc\BlogEtc\Captcha;
6
7
use DomainException;
8
9
/**
10
 * Class Basic.
11
 *
12
 * Basic anti spam captcha
13
 */
14
class Basic extends CaptchaAbstract
15
{
16
    public function __construct()
17
    {
18
        if (!config('blogetc.captcha.basic_question') || !config('blogetc.captcha.basic_answers')) {
19
            throw new DomainException('Invalid question or answers for captcha');
20
        }
21
    }
22
23
    /**
24
     * What view file should we use for the captcha field?
25
     */
26
    public function view(): string
27
    {
28
        return 'blogetc::captcha.basic';
29
    }
30
31
    /**
32
     * What rules should we use for the validation for this field?
33
     *
34
     * Enter the rules here, along with captcha validation.
35
     */
36
    public function rules(): array
37
    {
38
        $checkAnswer = static function ($attribute, $value, $fail) {
39
            $answers = config('blogetc.captcha.basic_answers');
40
41
            $value = strtolower(trim($value));
42
            $answers = strtolower($answers);
43
44
            $answersArray = array_map('trim', explode(',', $answers));
45
46
            if (!$value || !in_array($value, $answersArray, true)) {
47
                return $fail('The captcha field is incorrect.');
48
            }
49
        };
50
51
        return [
52
            'required',
53
            'string',
54
            $checkAnswer,
55
        ];
56
    }
57
58
    /**
59
     * @deprecated - please use captchaFieldName() instead
60
     */
61
    public function captcha_field_name(): string
62
    {
63
        return $this->captchaFieldName();
64
    }
65
66
    /**
67
     * What should the field name be (in the <input type='text' name='????'>).
68
     */
69
    public function captchaFieldName(): string
70
    {
71
        return 'captcha';
72
    }
73
}
74