Passed
Pull Request — master (#274)
by Alexander
05:12 queued 02:35
created

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