Passed
Pull Request — master (#81)
by Def
01:28
created

HasLength   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 104
ccs 38
cts 42
cp 0.9048
rs 10
c 0
b 0
f 0
wmc 13

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