Passed
Pull Request — master (#222)
by Alexander
02:58
created

HasLength   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 71
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0
wmc 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use JetBrains\PhpStorm\ArrayShape;
10
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_TRAIT, expecting T_STRING or '{' on line 10 at column 27
Loading history...
11
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
12
use Yiisoft\Validator\ParametrizedRuleInterface;
13
14
/**
15
 * Validates that the value is of certain length.
16
 *
17
 * Note, this rule should only be used with strings.
18
 */
19
#[Attribute(Attribute::TARGET_PROPERTY)]
20
final class HasLength implements ParametrizedRuleInterface
21
{
22
    use HandlerClassNameTrait;
23
    use RuleNameTrait;
24
25 7
    public function __construct(
26
        /**
27
         * @var int|null minimum length. null means no minimum length limit.
28
         *
29
         * @see $tooShortMessage for the customized message for a too short string.
30
         */
31
        private ?int $min = null,
32
        /**
33
         * @var int|null maximum length. null means no maximum length limit.
34
         *
35
         * @see $tooLongMessage for the customized message for a too long string.
36
         */
37
        private ?int $max = null,
38
        /**
39
         * @var string user-defined error message used when the value is not a string.
40
         */
41
        private string $message = 'This value must be a string.',
42
        /**
43
         * @var string user-defined error message used when the length of the value is smaller than {@see $min}.
44
         */
45
        private string $tooShortMessage = 'This value should contain at least {min, number} {min, plural, one{character} other{characters}}.',
46
        /**
47
         * @var string user-defined error message used when the length of the value is greater than {@see $max}.
48
         */
49
        private string $tooLongMessage = 'This value should contain at most {max, number} {max, plural, one{character} other{characters}}.',
50
        /**
51
         * @var string the encoding of the string value to be validated (e.g. 'UTF-8').
52
         * If this property is not set, application wide encoding will be used.
53
         */
54
        private string $encoding = 'UTF-8',
55
        private bool $skipOnEmpty = false,
56
        private bool $skipOnError = false,
57
        private ?Closure $when = null
58
    ) {
59
    }
60
61
    /**
62
     * @return int|null
63
     */
64 28
    public function getMin(): ?int
65
    {
66 28
        return $this->min;
67
    }
68
69
    /**
70
     * @return int|null
71
     */
72 28
    public function getMax(): ?int
73
    {
74 28
        return $this->max;
75
    }
76
77
    /**
78
     * @return string
79
     */
80 6
    public function getMessage(): string
81
    {
82 6
        return $this->message;
83
    }
84
85
    /**
86
     * @return string
87
     */
88 5
    public function getTooShortMessage(): string
89
    {
90 5
        return $this->tooShortMessage;
91
    }
92
93
    /**
94
     * @return string
95
     */
96 4
    public function getTooLongMessage(): string
97
    {
98 4
        return $this->tooLongMessage;
99
    }
100
101
    /**
102
     * @return string
103
     */
104 28
    public function getEncoding(): string
105
    {
106 28
        return $this->encoding;
107
    }
108
109
    /**
110
     * @return bool
111
     */
112
    public function isSkipOnEmpty(): bool
113
    {
114
        return $this->skipOnEmpty;
115
    }
116
117
    /**
118
     * @return bool
119
     */
120
    public function isSkipOnError(): bool
121
    {
122
        return $this->skipOnError;
123
    }
124
125
    /**
126
     * @return Closure|null
127
     */
128
    public function getWhen(): ?Closure
129
    {
130
        return $this->when;
131
    }
132
133 5
    #[ArrayShape([
134
        'min' => 'int|null',
135
        'max' => 'int|null',
136
        'message' => 'string[]',
137
        'tooShortMessage' => 'array',
138
        'tooLongMessage' => 'array',
139
        'encoding' => 'string',
140
        'skipOnEmpty' => 'bool',
141
        'skipOnError' => 'bool',
142
    ])]
143
    public function getOptions(): array
144
    {
145
        return [
146 5
            'min' => $this->min,
147 5
            'max' => $this->max,
148
            'message' => [
149 5
                'message' => $this->message,
150
            ],
151
            'tooShortMessage' => [
152 5
                'message' => $this->tooShortMessage,
153 5
                'parameters' => ['min' => $this->min],
154
            ],
155
            'tooLongMessage' => [
156 5
                'message' => $this->tooLongMessage,
157 5
                'parameters' => ['max' => $this->max],
158
            ],
159 5
            'encoding' => $this->encoding,
160 5
            'skipOnEmpty' => $this->skipOnEmpty,
161 5
            'skipOnError' => $this->skipOnError,
162
        ];
163
    }
164
}
165