Passed
Pull Request — master (#222)
by Alexander
04:51 queued 02:32
created

HasLength   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 61
ccs 12
cts 12
cp 1
rs 10
wmc 2

2 Methods

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