Passed
Pull Request — master (#222)
by Dmitriy
12:29
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 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 9 at column 27
Loading history...
10
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
11
use Yiisoft\Validator\RuleInterface;
12
13
/**
14
 * Checks if at least {@see AtLeast::$min} of many attributes are filled.
15
 */
16
#[Attribute(Attribute::TARGET_PROPERTY)]
17
final class AtLeast implements RuleInterface
18
{
19
    use HandlerClassNameTrait;
20
    use RuleNameTrait;
21
22
    public function __construct(
23
        /**
24
         * The list of required attributes that will be checked.
25
         */
26
        public array $attributes,
27
        /**
28
         * The minimum required quantity of filled attributes to pass the validation.
29
         * Defaults to 1.
30
         */
31
        public int $min = 1,
32
        /**
33
         * Message to display in case of error.
34
         */
35
        public string $message = 'The model is not valid. Must have at least "{min}" filled attributes.',
36
        public bool $skipOnEmpty = false,
37
        public bool $skipOnError = false,
38
        public ?Closure $when = null,
39
    ) {
40
    }
41
42 2
    public function getOptions(): array
43
    {
44
        return [
45 2
            'attributes' => $this->attributes,
46 2
            'min' => $this->min,
47
            'message' => [
48 2
                'message' => $this->message,
49 2
                'parameters' => ['min' => $this->min],
50
            ],
51 2
            'skipOnEmpty' => $this->skipOnEmpty,
52 2
            'skipOnError' => $this->skipOnError,
53
        ];
54
    }
55
}
56