Passed
Pull Request — master (#148)
by Wilmer
11:07
created

HasLength   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 112
ccs 40
cts 44
cp 0.9091
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 rule() 0 3 1
A tooShortMessage() 0 5 1
A min() 0 5 1
A tooLongMessage() 0 5 1
A max() 0 5 1
A getOptions() 0 15 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\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
final 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} characters or more, currently has ' .
43
        '{length} characters.';
44
    /**
45
     * @var string user-defined error message used when the length of the value is greater than {@see $max}.
46
     */
47
    private string $tooLongMessage = 'This value must contain at most {max} characters or less, currently has ' .
48
        '{length} characters.';
49
    /**
50
     * @var string the encoding of the string value to be validated (e.g. 'UTF-8').
51
     * If this property is not set, application wide encoding will be used.
52
     */
53
    protected string $encoding = 'UTF-8';
54
55 6
    public static function rule(): self
56
    {
57 6
        return new self();
58
    }
59
60 7
    protected function validateValue($value, ValidationContext $context = null): Result
61
    {
62 7
        $result = new Result();
63
64 7
        if (!is_string($value)) {
65 2
            $result->addError($this->formatMessage($this->message));
66 2
            return $result;
67
        }
68
69 7
        $length = mb_strlen($value, $this->encoding);
70
71 7
        if ($this->min !== null && $length < $this->min) {
72 3
            $result->addError($this->formatMessage($this->tooShortMessage, ['min' => $this->min, 'length' => $length]));
73
        }
74 7
        if ($this->max !== null && $length > $this->max) {
75 4
            $result->addError($this->formatMessage($this->tooLongMessage, ['max' => $this->max, 'length' => $length]));
76
        }
77
78 7
        return $result;
79
    }
80
81 3
    public function min(int $value): self
82
    {
83 3
        $new = clone $this;
84 3
        $new->min = $value;
85 3
        return $new;
86
    }
87
88 8
    public function max(int $value): self
89
    {
90 8
        $new = clone $this;
91 8
        $new->max = $value;
92 8
        return $new;
93
    }
94
95
    public function encoding(string $encoding): self
96
    {
97
        $new = clone $this;
98
        $new->encoding = $encoding;
99
        return $new;
100
    }
101
102 1
    public function tooShortMessage(string $message): self
103
    {
104 1
        $new = clone $this;
105 1
        $new->tooShortMessage = $message;
106 1
        return $new;
107
    }
108
109 1
    public function tooLongMessage(string $message): self
110
    {
111 1
        $new = clone $this;
112 1
        $new->tooLongMessage = $message;
113 1
        return $new;
114
    }
115
116 6
    public function getOptions(): array
117
    {
118 6
        return array_merge(
119 6
            parent::getOptions(),
120
            [
121 6
                'message' => $this->formatMessage($this->message),
122 6
                'min' => $this->min,
123 6
                'tooShortMessage' => $this->formatMessage(
124 6
                    $this->tooShortMessage, ['min' => $this->min, 'length' => strlen($this->message)]
125
                ),
126 6
                'max' => $this->max,
127 6
                'tooLongMessage' => $this->formatMessage(
128 6
                    $this->tooLongMessage, ['max' => $this->max, 'length' => strlen($this->message)]
129
                ),
130 6
                'encoding' => $this->encoding,
131
            ],
132
        );
133
    }
134
}
135