Passed
Pull Request — master (#219)
by
unknown
02:29
created

HasLength::max()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
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 Attribute;
8
use Yiisoft\Validator\FormatterInterface;
9
use Yiisoft\Validator\Result;
10
use Yiisoft\Validator\Rule;
11
use Yiisoft\Validator\ValidationContext;
12
13
use function is_string;
14
15
/**
16
 * Validates that the value is of certain length.
17
 *
18
 * Note, this rule should only be used with strings.
19
 */
20
#[Attribute(Attribute::TARGET_PROPERTY)]
21
final class HasLength extends Rule
22
{
23 18
    public function __construct(
24
        /**
25
         * @var int|null minimum length. null means no minimum length limit.
26
         *
27
         * @see $tooShortMessage for the customized message for a too short string.
28
         */
29
        private ?int $min = null,
30
        /**
31
         * @var int|null maximum length. null means no maximum length limit.
32
         *
33
         * @see $tooLongMessage for the customized message for a too long string.
34
         */
35
        private ?int $max = null,
36
        /**
37
         * @var string user-defined error message used when the value is not a string.
38
         */
39
        private string $message = 'This value must be a string.',
40
        /**
41
         * @var string user-defined error message used when the length of the value is smaller than {@see $min}.
42
         */
43
        private string $tooShortMessage = 'This value should contain at least {min, number} {min, plural, one{character} other{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 should contain at most {max, number} {max, plural, one{character} other{characters}}.',
48
        /**
49
         * @var string the encoding of the string value to be validated (e.g. 'UTF-8').
50
         * If this property is not set, application wide encoding will be used.
51
         */
52
        protected string $encoding = 'UTF-8',
53
        ?FormatterInterface $formatter = null,
54
        bool $skipOnEmpty = false,
55
        bool $skipOnError = false,
56
        $when = null
57
    ) {
58 18
        parent::__construct(formatter: $formatter, skipOnEmpty: $skipOnEmpty, skipOnError: $skipOnError, when: $when);
59
    }
60
61
    /**
62
     * @see $min
63
     */
64 1
    public function min(?int $value): self
65
    {
66 1
        $new = clone $this;
67 1
        $new->min = $value;
68
69 1
        return $new;
70
    }
71
72
    /**
73
     * @see $max
74
     */
75 1
    public function max(?int $value): self
76
    {
77 1
        $new = clone $this;
78 1
        $new->max = $value;
79
80 1
        return $new;
81
    }
82
83
    /**
84
     * @see $message
85
     */
86 1
    public function message(string $value): self
87
    {
88 1
        $new = clone $this;
89 1
        $new->message = $value;
90
91 1
        return $new;
92
    }
93
94
    /**
95
     * @see $tooShortMessage
96
     */
97 1
    public function tooShortMessage(string $value): self
98
    {
99 1
        $new = clone $this;
100 1
        $new->tooShortMessage = $value;
101
102 1
        return $new;
103
    }
104
105
    /**
106
     * @see $tooLongMessage
107
     */
108 1
    public function tooLongMessage(string $value): self
109
    {
110 1
        $new = clone $this;
111 1
        $new->tooLongMessage = $value;
112
113 1
        return $new;
114
    }
115
116
    /**
117
     * @see $encoding
118
     */
119 1
    public function encoding(string $value): self
120
    {
121 1
        $new = clone $this;
122 1
        $new->encoding = $value;
123
124 1
        return $new;
125
    }
126
127 37
    protected function validateValue($value, ?ValidationContext $context = null): Result
128
    {
129 37
        $result = new Result();
130
131 37
        if (!is_string($value)) {
132 6
            $result->addError($this->formatMessage($this->message));
133 6
            return $result;
134
        }
135
136 31
        $length = mb_strlen($value, $this->encoding);
137
138 31
        if ($this->min !== null && $length < $this->min) {
139 8
            $result->addError($this->formatMessage($this->tooShortMessage, ['min' => $this->min]));
140
        }
141 31
        if ($this->max !== null && $length > $this->max) {
142 5
            $result->addError($this->formatMessage($this->tooLongMessage, ['max' => $this->max]));
143
        }
144
145 31
        return $result;
146
    }
147
148 12
    public function getOptions(): array
149
    {
150 12
        return array_merge(parent::getOptions(), [
151 12
            'min' => $this->min,
152 12
            'max' => $this->max,
153 12
            'message' => $this->formatMessage($this->message),
154 12
            'tooShortMessage' => $this->formatMessage($this->tooShortMessage, ['min' => $this->min]),
155 12
            'tooLongMessage' => $this->formatMessage($this->tooLongMessage, ['max' => $this->max]),
156 12
            'encoding' => $this->encoding,
157
        ]);
158
    }
159
}
160