BooleanValidator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 49
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B validate() 0 19 5
A client() 0 5 1
1
<?php /** MicroBooleanValidator */
2
3
namespace Micro\Validator;
4
5
use Micro\Form\IFormModel;
6
7
/**
8
 * BooleanValidator class file.
9
 *
10
 * @author Oleg Lunegov <[email protected]>
11
 * @link https://github.com/linpax/microphp-framework
12
 * @copyright Copyright (c) 2013 Oleg Lunegov
13
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
14
 * @package Micro
15
 * @subpackage Validator
16
 * @version 1.0
17
 * @since 1.0
18
 */
19
class BooleanValidator extends BaseValidator
20
{
21
    /**
22
     * Initial validator
23
     *
24
     * @access public
25
     *
26
     * @param array $params Configuration array
27
     *
28
     * @result void
29
     */
30
    public function __construct(array $params)
31
    {
32
        $this->params = array_merge($this->params, ['true' => true, 'false' => false]);
33
        parent::__construct($params);
34
    }
35
36
    /**
37
     * @inheritdoc
38
     */
39
    public function validate(IFormModel $model)
40
    {
41
        foreach ($this->elements AS $element) {
42
            if (!$model->checkAttributeExists($element)) {
43
                $this->errors[] = 'Parameter '.$element.' not defined in class '.get_class($model);
44
45
                return false;
46
            }
47
            $elementValue = $model->$element;
48
49
            if (($elementValue !== $this->params['true']) && ($elementValue !== $this->params['false'])) {
50
                $this->errors[] = $element.' error: required element is empty.';
51
52
                return false;
53
            }
54
        }
55
56
        return true;
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function client(IFormModel $model)
63
    {
64
        return 'if (this.value != '.$this->params['true'].' && this.value != '.$this->params['false'].') {'.
65
        ' e.preventDefault(); this.focus(); alert(\'Value not compatible\'); }';
66
    }
67
}
68