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

BooleanValidator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 4
dl 0
loc 40
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 6 1
B getClientOptions() 0 20 5
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\jquery\ValidationAsset;
11
use yii\validators\client\ClientValidator;
12
13
/**
14
 * BooleanValidator composes client-side validation code from [[\yii\validators\BooleanValidator]].
15
 *
16
 * @see \yii\validators\BooleanValidator
17
 * @see ValidationAsset
18
 *
19
 * @author Paul Klimov <[email protected]>
20
 * @since 2.1.0
21
 */
22
class BooleanValidator extends ClientValidator
23
{
24
    /**
25
     * @inheritdoc
26
     */
27 1
    public function build($validator, $model, $attribute, $view)
28
    {
29 1
        ValidationAsset::register($view);
30 1
        $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\BooleanValidator>. 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...
31 1
        return 'yii.validation.boolean(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
32
    }
33
34
    /**
35
     * Returns the client-side validation options.
36
     * @param \yii\validators\BooleanValidator $validator the server-side validator.
37
     * @param \yii\base\Model $model the model being validated
38
     * @param string $attribute the attribute name being validated
39
     * @return array the client-side validation options
40
     */
41 1
    public function getClientOptions($validator, $model, $attribute)
42
    {
43
        $options = [
44 1
            'trueValue' => $validator->trueValue,
45 1
            'falseValue' => $validator->falseValue,
46 1
            'message' => $validator->formatMessage($validator->message, [
47 1
                'attribute' => $model->getAttributeLabel($attribute),
48 1
                'true' => $validator->trueValue === true ? 'true' : $validator->trueValue,
49 1
                'false' => $validator->falseValue === false ? 'false' : $validator->falseValue,
50
            ]),
51
        ];
52 1
        if ($validator->skipOnEmpty) {
53 1
            $options['skipOnEmpty'] = 1;
54
        }
55 1
        if ($validator->strict) {
56 1
            $options['strict'] = 1;
57
        }
58
59 1
        return $options;
60
    }
61
}