Passed
Pull Request — master (#364)
by Alexander
07:53 queued 04:53
created

AtLeast   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 90.48%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
eloc 20
c 3
b 0
f 0
dl 0
loc 77
ccs 19
cts 21
cp 0.9048
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getMin() 0 3 1
A getAttributes() 0 3 1
A getMessage() 0 3 1
A getIncorrectInputMessage() 0 3 1
A getOptions() 0 12 1
A getHandlerClassName() 0 3 1
A __construct() 0 12 1
A getName() 0 3 1
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\SkipOnEmptyTrait;
10
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
11
use Yiisoft\Validator\Rule\Trait\WhenTrait;
12
use Yiisoft\Validator\SerializableRuleInterface;
13
use Yiisoft\Validator\SkipOnEmptyInterface;
14
use Yiisoft\Validator\SkipOnErrorInterface;
15
use Yiisoft\Validator\ValidationContext;
16
use Yiisoft\Validator\WhenInterface;
17
18
/**
19
 * Checks if at least {@see AtLeast::$min} of many attributes are filled.
20
 */
21
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
22
final class AtLeast implements SerializableRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
23
{
24
    use SkipOnEmptyTrait;
25
    use SkipOnErrorTrait;
26
    use WhenTrait;
27
28
    /**
29
     * @param string[] $attributes The list of required attributes that will be checked.
30
     * @param int $min The minimum required quantity of filled attributes to pass the validation.
31
     * Defaults to 1.
32
     * @param string $incorrectInputMessage = 'Value must be array or iterable.',
33
     * @param string $message Message to display in case of error.
34
     * @param bool|callable|null $skipOnEmpty
35
     * @param bool $skipOnError
36
     * @param Closure(mixed, ValidationContext):bool|null $when
37
     */
38 3
    public function __construct(
39
        /**
40
         * @var string[]
41
         */
42
        private array $attributes,
43
        private int $min = 1,
44
        private string $incorrectInputMessage = 'Value must be an array or an object.',
45
        private string $message = 'The model is not valid. Must have at least "{min}" filled attributes.',
46
        private mixed $skipOnEmpty = null,
47
        private bool $skipOnError = false,
48
        private ?Closure $when = null
49
    ) {
50
    }
51
52 1
    public function getName(): string
53
    {
54 1
        return 'atLeast';
55
    }
56
57
    /**
58
     * @return string[]
59
     */
60 11
    public function getAttributes(): array
61
    {
62 11
        return $this->attributes;
63
    }
64
65 11
    public function getMin(): int
66
    {
67 11
        return $this->min;
68
    }
69
70
    public function getIncorrectInputMessage(): string
71
    {
72
        return $this->incorrectInputMessage;
73
    }
74
75 3
    public function getMessage(): string
76
    {
77 3
        return $this->message;
78
    }
79
80 2
    public function getOptions(): array
81
    {
82
        return [
83 2
            'attributes' => $this->attributes,
84 2
            'min' => $this->min,
85 2
            'incorrectInputMessage' => $this->incorrectInputMessage,
86
            'message' => [
87 2
                'message' => $this->message,
88 2
                'parameters' => ['min' => $this->min],
89
            ],
90 2
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
91 2
            'skipOnError' => $this->skipOnError,
92
        ];
93
    }
94
95 11
    public function getHandlerClassName(): string
96
    {
97 11
        return AtLeastHandler::class;
98
    }
99
}
100