1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Attribute; |
8
|
|
|
use Closure; |
9
|
|
|
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait; |
|
|
|
|
10
|
|
|
use Yiisoft\Validator\Rule\Trait\RuleNameTrait; |
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
|
|
|
|