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

HasLength::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 6
    public function max(int $value): self
78
    {
79 6
        $new = clone $this;
80 6
        $new->max = $value;
81 6
        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 1
    public function getName(): string
106
    {
107 1
        return 'hasLength';
108
    }
109
110 4
    public function getOptions(): array
111
    {
112 4
        return array_merge(
113
            [
114 4
                'message' => $this->translateMessage($this->message),
115 4
                'min' => $this->min,
116 4
                'tooShortMessage' => $this->translateMessage($this->tooShortMessage, ['min' => $this->min]),
117 4
                'max' => $this->max,
118 4
                'tooLongMessage' => $this->translateMessage($this->tooLongMessage, ['max' => $this->max]),
119 4
                'encoding' => $this->encoding,
120
            ],
121 4
            parent::getOptions()
122
        );
123
    }
124
}
125