Test Failed
Pull Request — master (#244)
by Dmitriy
02:50
created

AtLeast   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 22
c 1
b 0
f 0
dl 0
loc 69
ccs 14
cts 14
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getMessage() 0 3 1
A getOptions() 0 18 1
A __construct() 0 21 1
A getMin() 0 3 1
A getAttributes() 0 3 1
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\ParametrizedRuleInterface;
11
use Yiisoft\Validator\BeforeValidationInterface;
12
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
13
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
14
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
15
use Yiisoft\Validator\ValidationContext;
16
17
/**
18
 * Checks if at least {@see AtLeast::$min} of many attributes are filled.
19
 */
20
#[Attribute(Attribute::TARGET_PROPERTY)]
21
final class AtLeast implements ParametrizedRuleInterface, BeforeValidationInterface
22
{
23
    use BeforeValidationTrait;
24
    use HandlerClassNameTrait;
25
    use RuleNameTrait;
26
27 2
    public function __construct(
28
        /**
29
         * The list of required attributes that will be checked.
30
         */
31
        private array $attributes,
32
        /**
33
         * The minimum required quantity of filled attributes to pass the validation.
34
         * Defaults to 1.
35
         */
36
        private int $min = 1,
37
        /**
38
         * Message to display in case of error.
39
         */
40
        private string $message = 'The model is not valid. Must have at least "{min}" filled attributes.',
41
        private bool $skipOnEmpty = false,
42
        private bool $skipOnError = false,
43
        /**
44
         * @var Closure(mixed, ValidationContext):bool|null
45
         */
46
        private ?Closure $when = null,
47
    ) {
48
    }
49
50
    /**
51
     * @return array
52
     */
53 5
    public function getAttributes(): array
54
    {
55 5
        return $this->attributes;
56
    }
57
58 5
    public function getMin(): int
59
    {
60 5
        return $this->min;
61
    }
62
63
    /**
64
     * @return string
65
     */
66 3
    public function getMessage(): string
67
    {
68 3
        return $this->message;
69
    }
70
71 2
    #[ArrayShape([
72
        'attributes' => 'array',
73
        'min' => 'int',
74
        'message' => 'array',
75
        'skipOnEmpty' => 'bool',
76
        'skipOnError' => 'bool',
77
    ])]
78
    public function getOptions(): array
79
    {
80
        return [
81 2
            'attributes' => $this->attributes,
82 2
            'min' => $this->min,
83
            'message' => [
84 2
                'message' => $this->message,
85 2
                'parameters' => ['min' => $this->min],
86
            ],
87 2
            'skipOnEmpty' => $this->skipOnEmpty,
88 2
            'skipOnError' => $this->skipOnError,
89
        ];
90
    }
91
}
92