Completed
Push — 2.1 ( 75349f...bf116e )
by Alexander
29:27
created

CompareValidator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 5
dl 0
loc 50
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 6 1
B getClientOptions() 0 30 4
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\jquery\validators\client;
9
10
use yii\helpers\Html;
11
use yii\jquery\ValidationAsset;
12
use yii\validators\client\ClientValidator;
13
14
/**
15
 * CompareValidator composes client-side validation code from [[\yii\validators\CompareValidator]].
16
 *
17
 * @see \yii\validators\CompareValidator
18
 * @see ValidationAsset
19
 *
20
 * @author Paul Klimov <[email protected]>
21
 * @since 2.1.0
22
 */
23
class CompareValidator extends ClientValidator
24
{
25
    /**
26
     * @inheritdoc
27
     */
28
    public function build($validator, $model, $attribute, $view)
29
    {
30
        ValidationAsset::register($view);
31
        $options = $this->getClientOptions($validator, $model, $attribute);
0 ignored issues
show
Compatibility introduced by
$validator of type object<yii\validators\Validator> is not a sub-type of object<yii\validators\CompareValidator>. It seems like you assume a child class of the class yii\validators\Validator to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
32
        return 'yii.validation.compare(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
33
    }
34
35
    /**
36
     * Returns the client-side validation options.
37
     * @param \yii\validators\CompareValidator $validator the server-side validator.
38
     * @param \yii\base\Model $model the model being validated
39
     * @param string $attribute the attribute name being validated
40
     * @return array the client-side validation options
41
     */
42
    public function getClientOptions($validator, $model, $attribute)
43
    {
44
        $options = [
45
            'operator' => $validator->operator,
46
            'type' => $validator->type,
47
        ];
48
49
        if ($validator->compareValue !== null) {
50
            $options['compareValue'] = $validator->compareValue;
51
            $compareLabel = $compareValue = $compareValueOrAttribute = $validator->compareValue;
52
        } else {
53
            $compareAttribute = $validator->compareAttribute === null ? $attribute . '_repeat' : $validator->compareAttribute;
54
            $compareValue = $model->getAttributeLabel($compareAttribute);
55
            $options['compareAttribute'] = Html::getInputId($model, $compareAttribute);
56
            $compareLabel = $compareValueOrAttribute = $model->getAttributeLabel($compareAttribute);
57
        }
58
59
        if ($validator->skipOnEmpty) {
60
            $options['skipOnEmpty'] = 1;
61
        }
62
63
        $options['message'] = $validator->formatMessage($validator->message, [
64
            'attribute' => $model->getAttributeLabel($attribute),
65
            'compareAttribute' => $compareLabel,
66
            'compareValue' => $compareValue,
67
            'compareValueOrAttribute' => $compareValueOrAttribute,
68
        ]);
69
70
        return $options;
71
    }
72
}