CaptchaValidator::validate()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
rs 9.2
nc 4
cc 4
eloc 8
nop 1
1
<?php /** MicroCaptchaValidator */
2
3
namespace Micro\Validator;
4
5
use Micro\Base\Exception;
6
use Micro\Form\IFormModel;
7
use Micro\Web\UserInjector;
8
9
/**
10
 * CaptchaValidator class file.
11
 *
12
 * @author Oleg Lunegov <[email protected]>
13
 * @link https://github.com/linpax/microphp-framework
14
 * @copyright Copyright (c) 2013 Oleg Lunegov
15
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
16
 * @package Micro
17
 * @subpackage Validator
18
 * @version 1.0
19
 * @since 1.0
20
 */
21
class CaptchaValidator extends BaseValidator
22
{
23
    /** @var string $captcha compiled captcha */
24
    protected $captcha = '';
25
26
27
    /**
28
     * Constructor validator
29
     *
30
     * @access public
31
     *
32
     * @param array $params Configuration array
33
     *
34
     * @result void
35
     * @throws Exception
36
     */
37
    public function __construct(array $params)
38
    {
39
        parent::__construct($params);
40
41
        $this->captcha = (new UserInjector)->build()->getCaptcha();
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function validate(IFormModel $model)
48
    {
49
        foreach ($this->elements AS $element) {
50
            if (!$model->checkAttributeExists($element)) {
51
                $this->errors[] = 'Parameter '.$element.' not defined in class '.get_class($model);
52
53
                return false;
54
            }
55
56
            if ((new UserInjector)->build()->checkCaptcha($this->captcha)) {
57
                return false;
58
            }
59
        }
60
61
        return true;
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67
    public function client(IFormModel $model)
68
    {
69
        return '';
70
    }
71
}
72