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

RegularExpressionValidator::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 4
crap 2
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\helpers\Json;
12
use yii\jquery\ValidationAsset;
13
use yii\validators\client\ClientValidator;
14
use yii\web\JsExpression;
15
16
/**
17
 * RegularExpressionValidator composes client-side validation code from [[\yii\validators\RegularExpressionValidator]].
18
 *
19
 * @see \yii\validators\RegularExpressionValidator
20
 * @see ValidationAsset
21
 *
22
 * @author Paul Klimov <[email protected]>
23
 * @since 2.1.0
24
 */
25
class RegularExpressionValidator extends ClientValidator
26
{
27
    /**
28
     * @inheritdoc
29
     */
30
    public function build($validator, $model, $attribute, $view)
31
    {
32
        ValidationAsset::register($view);
33
        $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\RegularExpressionValidator>. 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...
34
        return 'yii.validation.regularExpression(value, messages, ' . Json::htmlEncode($options) . ');';
35
    }
36
37
    /**
38
     * Returns the client-side validation options.
39
     * @param \yii\validators\RegularExpressionValidator $validator the server-side validator.
40
     * @param \yii\base\Model $model the model being validated
41
     * @param string $attribute the attribute name being validated
42
     * @return array the client-side validation options
43
     */
44
    public function getClientOptions($validator, $model, $attribute)
45
    {
46
        $pattern = Html::escapeJsRegularExpression($validator->pattern);
47
48
        $options = [
49
            'pattern' => new JsExpression($pattern),
50
            'not' => $validator->not,
51
            'message' => $validator->formatMessage($validator->message, [
52
                'attribute' => $model->getAttributeLabel($attribute),
53
            ]),
54
        ];
55
        if ($validator->skipOnEmpty) {
56
            $options['skipOnEmpty'] = 1;
57
        }
58
59
        return $options;
60
    }
61
}