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