Passed
Pull Request — master (#464)
by Sergei
05:44 queued 03:13
created

Count::getObjectValidated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
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
 * Validates that the value contains certain number of items. Can be applied to arrays or classes implementing
22
 * {@see Countable} interface.
23
 *
24
 * @psalm-import-type WhenType from WhenInterface
25
 */
26
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
27
final class Count implements
28
    RuleWithOptionsInterface,
29
    SkipOnErrorInterface,
30
    WhenInterface,
31
    SkipOnEmptyInterface,
32
    LimitInterface
33
{
34
    use LimitTrait;
35
    use SkipOnEmptyTrait;
36
    use SkipOnErrorTrait;
37
    use WhenTrait;
38 18
39
    public function __construct(
40
        /**
41
         * @var int|null minimum number of items. null means no minimum number limit. Can't be combined with
42
         * {@see $exactly}.
43
         *
44
         * @see $lessThanMinMessage for the customized message for a value with too few items.
45
         */
46
        int|null $min = null,
47
        /**
48
         * @var int|null maximum number of items. null means no maximum number limit. Can't be combined with
49
         * {@see $exactly}.
50
         *
51
         * @see $greaterThanMaxMessage for the customized message for a value wuth too many items.
52
         */
53
        int|null $max = null,
54
        /**
55
         * @var int|null exact number of items. `null` means no strict comparison. Mutually exclusive with {@see $min}
56
         * and {@see $max}.
57
         */
58
        int|null $exactly = null,
59
        /**
60
         * @var string user-defined error message used when the value is neither an array nor implementing
61
         * {@see \Countable} interface.
62
         *
63
         * @see Countable
64
         */
65
        private string $incorrectInputMessage = 'This value must be an array or implement \Countable interface.',
66
        /**
67
         * @var string user-defined error message used when the number of items is smaller than {@see $min}.
68
         */
69
        string $lessThanMinMessage = 'This value must contain at least {min, number} {min, plural, one{item} ' .
70
        'other{items}}.',
71
        /**
72
         * @var string user-defined error message used when the number of items is greater than {@see $max}.
73
         */
74
        string $greaterThanMaxMessage = 'This value must contain at most {max, number} {max, plural, one{item} ' .
75
        'other{items}}.',
76
        /**
77
         * @var string user-defined error message used when the number of items does not equal {@see $exactly}.
78
         */
79
        string $notExactlyMessage = 'This value must contain exactly {exactly, number} {exactly, plural, one{item} ' .
80
        'other{items}}.',
81
0 ignored issues
show
Coding Style introduced by
Blank lines are not allowed in a multi-line function declaration
Loading history...
82
        /**
83
         * @var bool|callable|null
84
         */
85
        private $skipOnEmpty = null,
86
        private bool $skipOnError = false,
87
        /**
88
         * @var WhenType
89
         */
90
        private Closure|null $when = null,
91 18
    ) {
92
        $this->initLimitProperties(
93
            $min,
94
            $max,
95
            $exactly,
96
            $lessThanMinMessage,
97
            $greaterThanMaxMessage,
98
            $notExactlyMessage
99
        );
100
    }
101 1
102
    public function getName(): string
103 1
    {
104
        return 'count';
105
    }
106 5
107
    public function getIncorrectInputMessage(): string
108 5
    {
109
        return $this->incorrectInputMessage;
110
    }
111 1
112
    public function getOptions(): array
113 1
    {
114
        return array_merge($this->getLimitOptions(), [
115 1
            'incorrectInputMessage' => [
116
                'template' => $this->getIncorrectInputMessage(),
117
                'parameters' => [],
118 1
            ],
119 1
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
120
            'skipOnError' => $this->skipOnError,
121
        ]);
122
    }
123 34
124
    public function getHandler(): string
125 34
    {
126
        return CountHandler::class;
127
    }
128
}
129