Count   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 114
ccs 13
cts 13
cp 1
rs 10
c 1
b 0
f 0
wmc 5

5 Methods

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