Passed
Pull Request — master (#61)
by Alexander
01:36
created

HasLength   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 88.57%

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 92
ccs 31
cts 35
cp 0.8857
rs 10
c 0
b 0
f 0
wmc 12

7 Methods

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