Passed
Pull Request — master (#333)
by Sergei
02:38
created

HasLength::getMessage()   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
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
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 Yiisoft\Validator\Rule\Trait\LimitTrait;
10
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
11
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
12
use Yiisoft\Validator\Rule\Trait\WhenTrait;
13
use Yiisoft\Validator\SerializableRuleInterface;
14
use Yiisoft\Validator\SkipOnEmptyInterface;
15
use Yiisoft\Validator\SkipOnErrorInterface;
16
use Yiisoft\Validator\ValidationContext;
17
use Yiisoft\Validator\WhenInterface;
18
19
/**
20
 * Validates that the value is of certain length.
21
 *
22
 * Note, this rule should only be used with strings.
23
 */
24
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
25
final class HasLength implements SerializableRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
26
{
27
    use LimitTrait;
28
    use SkipOnEmptyTrait;
29
    use SkipOnErrorTrait;
30
    use WhenTrait;
31
32 15
    public function __construct(
33
        /**
34
         * @var int|null minimum length. null means no minimum length limit. Can't be combined with
35
         * {@see $exactly}.
36
         *
37
         * @see $lessThanMinMessage for the customized message for a too short string.
38
         */
39
        ?int $min = null,
40
        /**
41
         * @var int|null maximum length. null means no maximum length limit. Can't be combined with
42
         * {@see $exactly}.
43
         *
44
         * @see $greaterThanMaxMessage for the customized message for a too long string.
45
         */
46
        ?int $max = null,
47
        /**
48
         * @var int|null exact length. `null` means no strict comparison. Mutually exclusive with {@see $min} and
49
         * {@see $max}.
50
         */
51
        ?int $exactly = null,
52
        /**
53
         * @var string user-defined error message used when the value is not a string.
54
         */
55
        private string $message = 'This value must be a string.',
56
        /**
57
         * @var string user-defined error message used when the length of the value is smaller than {@see $min}.
58
         */
59
        string $lessThanMinMessage = 'This value must contain at least {min, number} {min, plural, one{character} ' .
60
        'other{characters}}.',
61
        /**
62
         * @var string user-defined error message used when the length of the value is greater than {@see $max}.
63
         */
64
        string $greaterThanMaxMessage = 'This value must contain at most {max, number} {max, plural, one{character} ' .
65
        'other{characters}}.',
66
        string $notExactlyMessage = 'This value must contain exactly {exactly, number} {exactly, plural, ' .
67
        'one{character} other{characters}}.',
68
        /**
69
         * @var string the encoding of the string value to be validated (e.g. 'UTF-8').
70
         * If this property is not set, application wide encoding will be used.
71
         */
72
        private string $encoding = 'UTF-8',
73
74
        /**
75
         * @var bool|callable|null
76
         */
77
        private $skipOnEmpty = null,
78
        private bool $skipOnError = false,
79
        /**
80
         * @var Closure(mixed, ValidationContext):bool|null
81
         */
82
        private ?Closure $when = null
83
    ) {
84 15
        $this->initLimitProperties(
85
            $min,
86
            $max,
87
            $exactly,
88
            $lessThanMinMessage,
89
            $greaterThanMaxMessage,
90
            $notExactlyMessage
91
        );
92
    }
93
94 2
    public function getName(): string
95
    {
96 2
        return 'hasLength';
97
    }
98
99 5
    public function getMessage(): string
100
    {
101 5
        return $this->message;
102
    }
103
104 58
    public function getEncoding(): string
105
    {
106 58
        return $this->encoding;
107
    }
108
109 4
    public function getOptions(): array
110
    {
111 4
        return array_merge($this->getLimitOptions(), [
112
            'message' => [
113 4
                'message' => $this->message,
114
            ],
115 4
            'encoding' => $this->encoding,
116 4
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
117 4
            'skipOnError' => $this->skipOnError,
118
        ]);
119
    }
120
121 37
    public function getHandlerClassName(): string
122
    {
123 37
        return HasLengthHandler::class;
124
    }
125
}
126