CompareValidator::validate()   D
last analyzed

Complexity

Conditions 10
Paths 7

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
c 0
b 0
f 0
rs 4.8196
cc 10
eloc 18
nc 7
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php /** MicroCompareValidator */
2
3
namespace Micro\Validator;
4
5
use Micro\Base\Exception;
6
use Micro\Form\IFormModel;
7
8
/**
9
 * CompareValidator class file.
10
 *
11
 * @author Oleg Lunegov <[email protected]>
12
 * @link https://github.com/linpax/microphp-framework
13
 * @copyright Copyright (c) 2013 Oleg Lunegov
14
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
15
 * @package Micro
16
 * @subpackage Validator
17
 * @version 1.0
18
 * @since 1.0
19
 */
20
class CompareValidator extends BaseValidator
21
{
22
    /**
23
     * @inheritdoc
24
     * @throws Exception
25
     */
26
    public function validate(IFormModel $model)
27
    {
28
        if (empty($this->params['attribute']) && empty($this->params['value'])) {
29
            return false;
30
        }
31
32
        if (!$model->checkAttributeExists($this->params['attribute'])) {
33
            throw new Exception('Attribute `'.$this->params['attribute'].'` not found into '.get_class($model));
34
        }
35
36
        foreach ($this->elements AS $element) {
37
            if (!$model->checkAttributeExists($element)) {
38
                $this->errors[] = 'Parameter '.$element.' not defined in class '.get_class($model);
39
40
                return false;
41
            }
42
43
            $elementValue = $model->$element;
44
            if (!empty($this->params['value']) && ($this->params['value'] !== $elementValue)) {
45
                $this->errors[] = 'Parameter '.$element.' not equal '.$this->params['value'];
46
47
                return false;
48
            } elseif (array_key_exists('attribute',
49
                    $this->params) && ($model->{$this->params['attribute']} !== $elementValue)
50
            ) {
51
                $this->errors[] = 'Parameter '.$element.' not equal '.$model->{$this->params['attribute']};
52
53
                return false;
54
            }
55
        }
56
57
        return true;
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function client(IFormModel $model)
64
    {
65
        $value = $this->params['value'];
66
        if (!$value) {
67
            $attribute = $this->params['attribute'];
68
            $value = $model->$attribute;
69
        }
70
71
        $javaScript = 'if (this.value!="'.$value.'") { e.preventDefault(); this.focus(); alert(\'Value is not compatible\'); }';
72
73
        return $javaScript;
74
    }
75
}
76