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

StringValidator::init()   F

Complexity

Conditions 13
Paths 240

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 13

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 19
cts 19
cp 1
rs 3.951
c 0
b 0
f 0
cc 13
eloc 18
nc 240
nop 0
crap 13

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
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\validators;
9
10
use Yii;
11
12
/**
13
 * StringValidator validates that the attribute value is of certain length.
14
 *
15
 * Note, this validator should only be used with string-typed attributes.
16
 *
17
 * @author Qiang Xue <[email protected]>
18
 * @since 2.0
19
 */
20
class StringValidator extends Validator
21
{
22
    /**
23
     * @var int|array specifies the length limit of the value to be validated.
24
     * This can be specified in one of the following forms:
25
     *
26
     * - an integer: the exact length that the value should be of;
27
     * - an array of one element: the minimum length that the value should be of. For example, `[8]`.
28
     *   This will overwrite [[min]].
29
     * - an array of two elements: the minimum and maximum lengths that the value should be of.
30
     *   For example, `[8, 128]`. This will overwrite both [[min]] and [[max]].
31
     * @see tooShort for the customized message for a too short string.
32
     * @see tooLong for the customized message for a too long string.
33
     * @see notEqual for the customized message for a string that does not match desired length.
34
     */
35
    public $length;
36
    /**
37
     * @var int maximum length. If not set, it means no maximum length limit.
38
     * @see tooLong for the customized message for a too long string.
39
     */
40
    public $max;
41
    /**
42
     * @var int minimum length. If not set, it means no minimum length limit.
43
     * @see tooShort for the customized message for a too short string.
44
     */
45
    public $min;
46
    /**
47
     * @var string user-defined error message used when the value is not a string.
48
     */
49
    public $message;
50
    /**
51
     * @var string user-defined error message used when the length of the value is smaller than [[min]].
52
     */
53
    public $tooShort;
54
    /**
55
     * @var string user-defined error message used when the length of the value is greater than [[max]].
56
     */
57
    public $tooLong;
58
    /**
59
     * @var string user-defined error message used when the length of the value is not equal to [[length]].
60
     */
61
    public $notEqual;
62
    /**
63
     * @var string the encoding of the string value to be validated (e.g. 'UTF-8').
64
     * If this property is not set, [[\yii\base\Application::charset]] will be used.
65
     */
66
    public $encoding;
67
68
69
    /**
70
     * @inheritdoc
71
     */
72 49
    public function init()
73
    {
74 49
        parent::init();
75 49
        if (is_array($this->length)) {
76 1
            if (isset($this->length[0])) {
77 1
                $this->min = $this->length[0];
78
            }
79 1
            if (isset($this->length[1])) {
80 1
                $this->max = $this->length[1];
81
            }
82 1
            $this->length = null;
83
        }
84 49
        if ($this->encoding === null) {
85 49
            $this->encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
86
        }
87 49
        if ($this->message === null) {
88 49
            $this->message = Yii::t('yii', '{attribute} must be a string.');
89
        }
90 49
        if ($this->min !== null && $this->tooShort === null) {
91 4
            $this->tooShort = Yii::t('yii', '{attribute} should contain at least {min, number} {min, plural, one{character} other{characters}}.');
92
        }
93 49
        if ($this->max !== null && $this->tooLong === null) {
94 17
            $this->tooLong = Yii::t('yii', '{attribute} should contain at most {max, number} {max, plural, one{character} other{characters}}.');
95
        }
96 49
        if ($this->length !== null && $this->notEqual === null) {
97 2
            $this->notEqual = Yii::t('yii', '{attribute} should contain {length, number} {length, plural, one{character} other{characters}}.');
98
        }
99 49
    }
100
101
    /**
102
     * @inheritdoc
103
     */
104 17
    public function validateAttribute($model, $attribute)
105
    {
106 17
        $value = $model->$attribute;
107
108 17
        if (!is_string($value)) {
109 2
            $this->addError($model, $attribute, $this->message);
110
111 2
            return;
112
        }
113
114 16
        $length = mb_strlen($value, $this->encoding);
115
116 16
        if ($this->min !== null && $length < $this->min) {
117 1
            $this->addError($model, $attribute, $this->tooShort, ['min' => $this->min]);
118
        }
119 16
        if ($this->max !== null && $length > $this->max) {
120 4
            $this->addError($model, $attribute, $this->tooLong, ['max' => $this->max]);
121
        }
122 16
        if ($this->length !== null && $length !== $this->length) {
123 1
            $this->addError($model, $attribute, $this->notEqual, ['length' => $this->length]);
124
        }
125 16
    }
126
127
    /**
128
     * @inheritdoc
129
     */
130 3
    protected function validateValue($value)
131
    {
132 3
        if (!is_string($value)) {
133 1
            return [$this->message, []];
134
        }
135
136 3
        $length = mb_strlen($value, $this->encoding);
137
138 3
        if ($this->min !== null && $length < $this->min) {
139 2
            return [$this->tooShort, ['min' => $this->min]];
140
        }
141 3
        if ($this->max !== null && $length > $this->max) {
142 2
            return [$this->tooLong, ['max' => $this->max]];
143
        }
144 3
        if ($this->length !== null && $length !== $this->length) {
145 1
            return [$this->notEqual, ['length' => $this->length]];
146
        }
147
148 3
        return null;
149
    }
150
}
151