Test Setup Failed
Pull Request — master (#216)
by Dmitriy
03:06
created

HasLength   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 56
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 34 1
A getOptions() 0 17 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule\HasLength;
6
7
use Attribute;
8
use Closure;
9
10
/**
11
 * Validates that the value is of certain length.
12
 *
13
 * Note, this rule should only be used with strings.
14
 */
15
#[Attribute(Attribute::TARGET_PROPERTY)]
16
final class HasLength
17
{
18
    public function __construct(
19
        /**
20
         * @var int|null minimum length. null means no minimum length limit.
21
         *
22
         * @see $tooShortMessage for the customized message for a too short string.
23
         */
24
        public ?int $min = null,
25
        /**
26
         * @var int|null maximum length. null means no maximum length limit.
27
         *
28
         * @see $tooLongMessage for the customized message for a too long string.
29
         */
30
        public ?int $max = null,
31
        /**
32
         * @var string user-defined error message used when the value is not a string.
33
         */
34
        public string $message = 'This value must be a string.',
35
        /**
36
         * @var string user-defined error message used when the length of the value is smaller than {@see $min}.
37
         */
38
        public string $tooShortMessage = 'This value should contain at least {min, number} {min, plural, one{character} other{characters}}.',
39
        /**
40
         * @var string user-defined error message used when the length of the value is greater than {@see $max}.
41
         */
42
        public string $tooLongMessage = 'This value should contain at most {max, number} {max, plural, one{character} other{characters}}.',
43
        /**
44
         * @var string the encoding of the string value to be validated (e.g. 'UTF-8').
45
         * If this property is not set, application wide encoding will be used.
46
         */
47
        public string $encoding = 'UTF-8',
48
        public bool $skipOnEmpty = false,
49
        public bool $skipOnError = false,
50
        public ?Closure $when = null
51
    ) {
52
    }
53
54
    public function getOptions(): array
55
    {
56
        return [
57
            'min' => $this->min,
58
            'max' => $this->max,
59
            'message' => [
60
                'message' => $this->message,
61
            ],
62
            'tooShortMessage' => [
63
                'message' => $this->tooShortMessage,
64
                'parameters' => ['min' => $this->min],
65
            ],
66
            'tooLongMessage' => [
67
                'message' => $this->tooLongMessage, ['max' => $this->max],
68
                'parameters' => ['max' => $this->max],
69
            ],
70
            'encoding' => $this->encoding,
71
        ];
72
    }
73
}
74