Passed
Push — master ( 89f940...819311 )
by Alexander
02:27
created

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