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

AtLeast::getMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 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