Passed
Pull Request — master (#364)
by Alexander
05:49 queued 02:34
created

AtLeast::getAttributes()   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 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