Test Failed
Pull Request — master (#222)
by Rustam
02:30
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 5
{
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 5
        private bool $skipOnError = false,
39
        private ?Closure $when = null,
40
    ) {
41 4
    }
42
43 4
    /**
44
     * @return array
45 4
     */
46 4
    public function getAttributes(): array
47 3
    {
48
        return $this->attributes;
49
    }
50
51 4
    /**
52
     * @return int
53 4
     */
54 2
    public function getMin(): int
55 2
    {
56
        return $this->min;
57
    }
58 4
59
    /**
60
     * @return string
61 4
     */
62
    public function getMessage(): string
63 4
    {
64
        return $this->message;
65
    }
66 2
67
    /**
68 2
     * @return bool
69 2
     */
70 2
    public function isSkipOnEmpty(): bool
71 2
    {
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
    #[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
            'attributes' => $this->attributes,
102
            'min' => $this->min,
103
            'message' => [
104
                'message' => $this->message,
105
                'parameters' => ['min' => $this->min],
106
            ],
107
            'skipOnEmpty' => $this->skipOnEmpty,
108
            'skipOnError' => $this->skipOnError,
109
        ];
110
    }
111
}
112