Passed
Pull Request — master (#222)
by Rustam
02:34
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\ParametrizedRuleInterface;
11
use Yiisoft\Validator\PreValidatableRuleInterface;
12
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 12 at column 27
Loading history...
13
use Yiisoft\Validator\Rule\Trait\PreValidatableTrait;
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, PreValidatableRuleInterface
22
{
23
    use HandlerClassNameTrait;
24
    use PreValidatableTrait;
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
    /**
59
     * @return int
60
     */
61 5
    public function getMin(): int
62
    {
63 5
        return $this->min;
64
    }
65
66
    /**
67
     * @return string
68
     */
69 3
    public function getMessage(): string
70
    {
71 3
        return $this->message;
72
    }
73
74 2
    #[ArrayShape([
75
        'attributes' => 'array',
76
        'min' => 'int',
77
        'message' => 'array',
78
        'skipOnEmpty' => 'bool',
79
        'skipOnError' => 'bool',
80
    ])]
81
    public function getOptions(): array
82
    {
83
        return [
84 2
            'attributes' => $this->attributes,
85 2
            'min' => $this->min,
86
            'message' => [
87 2
                'message' => $this->message,
88 2
                'parameters' => ['min' => $this->min],
89
            ],
90 2
            'skipOnEmpty' => $this->skipOnEmpty,
91 2
            'skipOnError' => $this->skipOnError,
92
        ];
93
    }
94
}
95