Passed
Pull Request — master (#222)
by Alexander
02:58
created

AtLeast   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 56
ccs 19
cts 19
cp 1
rs 10
c 1
b 0
f 0
wmc 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use JetBrains\PhpStorm\ArrayShape;
10
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_TRAIT, expecting T_STRING or '{' on line 10 at column 27
Loading history...
11
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
12
use Yiisoft\Validator\ParametrizedRuleInterface;
13
14
/**
15
 * Checks if at least {@see AtLeast::$min} of many attributes are filled.
16
 */
17
#[Attribute(Attribute::TARGET_PROPERTY)]
18
final class AtLeast implements ParametrizedRuleInterface
19
{
20
    use HandlerClassNameTrait;
21
    use RuleNameTrait;
22
23
    public function __construct(
24
        /**
25
         * The list of required attributes that will be checked.
26
         */
27
        private array $attributes,
28
        /**
29
         * The minimum required quantity of filled attributes to pass the validation.
30
         * Defaults to 1.
31
         */
32
        private int $min = 1,
33
        /**
34
         * Message to display in case of error.
35
         */
36
        private string $message = 'The model is not valid. Must have at least "{min}" filled attributes.',
37
        private bool $skipOnEmpty = false,
38
        private bool $skipOnError = false,
39
        private ?Closure $when = null,
40
    ) {
41
    }
42
43
    /**
44
     * @return array
45
     */
46 5
    public function getAttributes(): array
47
    {
48 5
        return $this->attributes;
49
    }
50
51
    /**
52
     * @return int
53
     */
54 5
    public function getMin(): int
55
    {
56 5
        return $this->min;
57
    }
58
59
    /**
60
     * @return string
61
     */
62 3
    public function getMessage(): string
63
    {
64 3
        return $this->message;
65
    }
66
67
    /**
68
     * @return bool
69
     */
70
    public function isSkipOnEmpty(): bool
71
    {
72
        return $this->skipOnEmpty;
73
    }
74
75
    /**
76
     * @return bool
77
     */
78
    public function isSkipOnError(): bool
79
    {
80
        return $this->skipOnError;
81
    }
82
83
    /**
84
     * @return Closure|null
85
     */
86
    public function getWhen(): ?Closure
87
    {
88
        return $this->when;
89
    }
90
91 2
    #[ArrayShape([
92
        'attributes' => 'array',
93
        'min' => 'int',
94
        'message' => 'array',
95
        'skipOnEmpty' => 'bool',
96
        'skipOnError' => 'bool',
97
    ])]
98
    public function getOptions(): array
99
    {
100
        return [
101 2
            'attributes' => $this->attributes,
102 2
            'min' => $this->min,
103
            'message' => [
104 2
                'message' => $this->message,
105 2
                'parameters' => ['min' => $this->min],
106
            ],
107 2
            'skipOnEmpty' => $this->skipOnEmpty,
108 2
            'skipOnError' => $this->skipOnError,
109
        ];
110
    }
111
}
112