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

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