Passed
Pull Request — master (#468)
by Sergei
03:08
created

Count   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 115
ccs 13
cts 13
cp 1
rs 10
c 1
b 0
f 0
wmc 8

7 Methods

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