Length::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
nc 1
nop 11
dl 0
loc 23
cc 1
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use Yiisoft\Validator\CountableLimitInterface;
10
use Yiisoft\Validator\Rule\Trait\CountableLimitTrait;
11
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
12
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
13
use Yiisoft\Validator\Rule\Trait\WhenTrait;
14
use Yiisoft\Validator\DumpedRuleInterface;
15
use Yiisoft\Validator\SkipOnEmptyInterface;
16
use Yiisoft\Validator\SkipOnErrorInterface;
17
use Yiisoft\Validator\WhenInterface;
18
19
/**
20
 * Defines validation options to check that the value is a string of a certain length.
21
 *
22
 * To check that the value only has the string type, use {@see StringValue} rule instead.
23
 *
24
 * @see LengthHandler
25
 *
26
 * @psalm-import-type WhenType from WhenInterface
27
 */
28
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
29
final class Length implements
30
    DumpedRuleInterface,
31
    SkipOnErrorInterface,
32
    WhenInterface,
33
    SkipOnEmptyInterface,
34
    CountableLimitInterface
35
{
36
    use CountableLimitTrait;
37
    use SkipOnEmptyTrait;
38
    use SkipOnErrorTrait;
39
    use WhenTrait;
40
41
    /**
42
     * @param int|null $exactly Exact length. `null` means no strict comparison. Mutually exclusive with
43
     * {@see $min} and {@see $max}.
44
     * @param int|null $min Minimum length. `null` means no minimum length limit. Can't be combined with
45
     * {@see $exactly}. See {@see $lessThanMinMessage} for the customized message for a too short string.
46
     * @param int|null $max maximum length. `null` means no maximum length limit. Can't be combined with
47
     * {@see $exactly}. See {@see $greaterThanMaxMessage} for the customized message for a too long string.
48
     * @param string $incorrectInputMessage Error message used when the value is not a string.
49
     *
50
     * You may use the following placeholders in the message:
51
     *
52
     * - `{attribute}`: the translated label of the attribute being validated.
53
     * - `{type}`: the type of the value being validated.
54
     * @param string $lessThanMinMessage Error message used when the length of the value is smaller than {@see $min}.
55
     *
56
     * You may use the following placeholders in the message:
57
     *
58
     * - `{attribute}`: the translated label of the attribute being validated.
59
     * - `{min}`: minimum number of items required.
60
     * - `{number}`: actual number of items.
61
     * @param string $greaterThanMaxMessage Error message used when the length of the value is greater than {@see $max}.
62
     *
63
     * You may use the following placeholders in the message:
64
     *
65
     * - `{attribute}`: the translated label of the attribute being validated.
66
     * - `{max}`: maximum number of items required.
67
     * - `{number}`: actual number of items.
68
     * @param string $notExactlyMessage Error message used when the number of items does not equal {@see $exactly}.
69
     *
70
     * You may use the following placeholders in the message:
71
     *
72
     * - `{attribute}`: the translated label of the attribute being validated.
73
     * - `{exactly}`: exact number of items required.
74
     * - `{number}`: actual number of items.
75
     * @param string $encoding The encoding of the string value to be validated (e.g. 'UTF-8').
76
     * If this property is not set, application wide encoding will be used.
77
     * @param bool|callable|null $skipOnEmpty Whether to skip this rule if the value validated is empty.
78
     * See {@see SkipOnEmptyInterface}.
79
     * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error.
80
     * See {@see SkipOnErrorInterface}.
81
     * @param Closure|null $when A callable to define a condition for applying the rule.
82
     * See {@see WhenInterface}.
83
     *
84
     * @psalm-param WhenType $when
85
     */
86
    public function __construct(
87
        int|null $exactly = null,
88
        int|null $min = null,
89
        int|null $max = null,
90
        private string $incorrectInputMessage = 'The value must be a string.',
91
        string $lessThanMinMessage = 'This value must contain at least {min, number} {min, plural, one{character} ' .
92
        'other{characters}}.',
93
        string $greaterThanMaxMessage = 'This value must contain at most {max, number} {max, plural, one{character} ' .
94
        'other{characters}}.',
95
        string $notExactlyMessage = 'This value must contain exactly {exactly, number} {exactly, plural, ' .
96
        'one{character} other{characters}}.',
97
        private string $encoding = 'UTF-8',
98
        private mixed $skipOnEmpty = null,
99
        private bool $skipOnError = false,
100
        private Closure|null $when = null
101
    ) {
102
        $this->initCountableLimitProperties(
103
            $min,
104
            $max,
105
            $exactly,
106
            $lessThanMinMessage,
107
            $greaterThanMaxMessage,
108
            $notExactlyMessage
109
        );
110
    }
111
112
    public function getName(): string
113
    {
114
        return self::class;
115
    }
116
117
    /**
118
     * Get error message used when the value is neither an array nor implementing {@see \Countable} interface.
119
     *
120
     * @return string Error message.
121
     *
122
     * @see $incorrectInputMessage
123
     */
124
    public function getIncorrectInputMessage(): string
125
    {
126
        return $this->incorrectInputMessage;
127
    }
128
129
    /**
130
     * Get the encoding of the string value to be validated (e.g. 'UTF-8').
131
     * If this property is not set, application wide encoding will be used.
132
     *
133
     * @return string Encoding of the string value to be validated.
134
     *
135
     * @see $encoding
136
     */
137
    public function getEncoding(): string
138
    {
139
        return $this->encoding;
140
    }
141
142
    public function getOptions(): array
143
    {
144
        return array_merge($this->getLimitOptions(), [
145
            'incorrectInputMessage' => [
146
                'template' => $this->incorrectInputMessage,
147
                'parameters' => [],
148
            ],
149
            'encoding' => $this->encoding,
150
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
151
            'skipOnError' => $this->skipOnError,
152
        ]);
153
    }
154
155
    public function getHandler(): string
156
    {
157
        return LengthHandler::class;
158
    }
159
}
160