Passed
Pull Request — master (#222)
by Dmitriy
02:30
created

AtLeast::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 6
dl 0
loc 18
ccs 0
cts 1
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule\AtLeast;
6
7
use Attribute;
8
use Closure;
9
use Yiisoft\Validator\Rule\RuleNameTrait;
10
use Yiisoft\Validator\Rule\HandlerClassNameTrait;
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