Passed
Push — master ( 99ab46...a20a6f )
by Alexander
02:59
created

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