Passed
Pull Request — master (#41)
by Alexander
01:57 queued 36s
created

HasLength   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 84.21%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 67
ccs 16
cts 19
cp 0.8421
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validateValue() 0 18 6
A encoding() 0 4 1
A min() 0 4 1
A max() 0 4 1
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 Yiisoft\Validator\Rule;
9
10
use Yiisoft\Validator\DataSetInterface;
11
use Yiisoft\Validator\Result;
12
use Yiisoft\Validator\Rule;
13
14
/**
15
 * StringValidator validates that the attribute value is of certain length.
16
 *
17
 * Note, this validator should only be used with string-typed attributes.
18
 */
19
class HasLength extends Rule
20
{
21
    /**
22
     * @var int maximum length. If not set, it means no maximum length limit.
23
     * @see tooLongMessage for the customized message for a too long string.
24
     */
25
    private $max;
26
    /**
27
     * @var int minimum length. If not set, it means no minimum length limit.
28
     * @see tooShortMessage for the customized message for a too short string.
29
     */
30
    private $min;
31
    /**
32
     * @var string user-defined error message used when the value is not a string.
33
     */
34
    private $message = '{attribute} must be a string.';
35
    /**
36
     * @var string user-defined error message used when the length of the value is smaller than [[min]].
37
     */
38
    private $tooShortMessage = '{attribute} should contain at least {min, number} {min, plural, one{character} other{characters}}.';
39
    /**
40
     * @var string user-defined error message used when the length of the value is greater than [[max]].
41
     */
42
    private $tooLongMessage = '{attribute} should contain at most {max, number} {max, plural, one{character} other{characters}}.';
43
44
    /**
45
     * @var string the encoding of the string value to be validated (e.g. 'UTF-8').
46
     * If this property is not set, application wide encoding will be used.
47
     */
48
    protected $encoding = 'UTF-8';
49
50 2
    public function min(int $value): self
51
    {
52 2
        $this->min = $value;
53 2
        return $this;
54
    }
55
56 2
    public function max(int $value): self
57
    {
58 2
        $this->max = $value;
59 2
        return $this;
60
    }
61
62
    public function encoding(string $encoding): self
63
    {
64
        $this->encoding = $encoding;
65
        return $this;
66
    }
67
68 4
    protected function validateValue($value, DataSetInterface $dataSet = null): Result
69
    {
70 4
        $result = new Result();
71
72 4
        if (!is_string($value)) {
73 1
            return $result->addError($this->formatMessage($this->message));
74
        }
75
76 4
        $length = mb_strlen($value, $this->encoding);
77
78 4
        if ($this->min !== null && $length < $this->min) {
79 2
            $result = $result->addError($this->formatMessage($this->tooShortMessage, ['min' => $this->min]));
80
        }
81 4
        if ($this->max !== null && $length > $this->max) {
82 2
            $result = $result->addError($this->formatMessage($this->tooLongMessage, ['min' => $this->max]));
83
        }
84
85 4
        return $result;
86
    }
87
}
88