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

NumberValidator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 6
dl 0
loc 55
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 6 1
C getClientOptions() 0 35 7
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\Json;
11
use yii\jquery\ValidationAsset;
12
use yii\validators\client\ClientValidator;
13
use yii\web\JsExpression;
14
15
/**
16
 * NumberValidator composes client-side validation code from [[\yii\validators\NumberValidator]].
17
 *
18
 * @see \yii\validators\NumberValidator
19
 * @see ValidationAsset
20
 *
21
 * @author Paul Klimov <[email protected]>
22
 * @since 2.1.0
23
 */
24
class NumberValidator extends ClientValidator
25
{
26
    /**
27
     * @inheritdoc
28
     */
29 1
    public function build($validator, $model, $attribute, $view)
30
    {
31 1
        ValidationAsset::register($view);
32 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\NumberValidator>. 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...
33 1
        return 'yii.validation.number(value, messages, ' . Json::htmlEncode($options) . ');';
34
    }
35
36
    /**
37
     * Returns the client-side validation options.
38
     * @param \yii\validators\NumberValidator $validator the server-side validator.
39
     * @param \yii\base\Model $model the model being validated
40
     * @param string $attribute the attribute name being validated
41
     * @return array the client-side validation options
42
     */
43 1
    public function getClientOptions($validator, $model, $attribute)
44
    {
45 1
        $label = $model->getAttributeLabel($attribute);
46
47
        $options = [
48 1
            'pattern' => new JsExpression($validator->integerOnly ? $validator->integerPattern : $validator->numberPattern),
49 1
            'message' => $validator->formatMessage($validator->message, [
50 1
                'attribute' => $label,
51
            ]),
52
        ];
53
54 1
        if ($validator->min !== null) {
55
            // ensure numeric value to make javascript comparison equal to PHP comparison
56
            // https://github.com/yiisoft/yii2/issues/3118
57 1
            $options['min'] = is_string($validator->min) ? (float) $validator->min : $validator->min;
58 1
            $options['tooSmall'] = $validator->formatMessage($validator->tooSmall, [
59 1
                'attribute' => $label,
60 1
                'min' => $validator->min,
61
            ]);
62
        }
63 1
        if ($validator->max !== null) {
64
            // ensure numeric value to make javascript comparison equal to PHP comparison
65
            // https://github.com/yiisoft/yii2/issues/3118
66 1
            $options['max'] = is_string($validator->max) ? (float) $validator->max : $validator->max;
67 1
            $options['tooBig'] = $validator->formatMessage($validator->tooBig, [
68 1
                'attribute' => $label,
69 1
                'max' => $validator->max,
70
            ]);
71
        }
72 1
        if ($validator->skipOnEmpty) {
73 1
            $options['skipOnEmpty'] = 1;
74
        }
75
76 1
        return $options;
77
    }
78
}