Passed
Push — master ( 62cd3f...b14e96 )
by Sergei
02:41
created

Count::getObjectValidated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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\LimitInterface;
11
use Yiisoft\Validator\Rule\Trait\LimitTrait;
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\RuleWithOptionsInterface;
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
    RuleWithOptionsInterface,
31
    SkipOnErrorInterface,
32
    WhenInterface,
33
    SkipOnEmptyInterface,
34
    LimitInterface
35
{
36
    use LimitTrait;
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
     * @psalm-param WhenType $when
83
     */
84
    public function __construct(
85
        int|null $exactly = null,
86
        int|null $min = null,
87
        int|null $max = null,
88
        private string $incorrectInputMessage = 'This value must be an array or implement \Countable interface.',
89
        string $lessThanMinMessage = 'This value must contain at least {min, number} {min, plural, one{item} ' .
90
        'other{items}}.',
91 18
        string $greaterThanMaxMessage = 'This value must contain at most {max, number} {max, plural, one{item} ' .
92
        'other{items}}.',
93
        string $notExactlyMessage = 'This value must contain exactly {exactly, number} {exactly, plural, one{item} ' .
94
        'other{items}}.',
95
        private mixed $skipOnEmpty = null,
96
        private bool $skipOnError = false,
97
        private Closure|null $when = null,
98
    ) {
99
        $this->initLimitProperties(
100
            $min,
101 1
            $max,
102
            $exactly,
103 1
            $lessThanMinMessage,
104
            $greaterThanMaxMessage,
105
            $notExactlyMessage
106 5
        );
107
    }
108 5
109
    public function getName(): string
110
    {
111 1
        return 'count';
112
    }
113 1
114
    /**
115 1
     * Get error message used when the value is neither an array nor an object implementing {@see \Countable} interface.
116
     *
117
     * @return string Error message.
118 1
     *
119 1
     * @see $incorrectInputMessage
120
     */
121
    public function getIncorrectInputMessage(): string
122
    {
123 34
        return $this->incorrectInputMessage;
124
    }
125 34
126
    public function getOptions(): array
127
    {
128
        return array_merge($this->getLimitOptions(), [
129
            'incorrectInputMessage' => [
130
                'template' => $this->getIncorrectInputMessage(),
131
                'parameters' => [],
132
            ],
133
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
134
            'skipOnError' => $this->skipOnError,
135
        ]);
136
    }
137
138
    public function getHandler(): string
139
    {
140
        return CountHandler::class;
141
    }
142
}
143