Passed
Pull Request — master (#219)
by
unknown
02:40
created

AtLeast::getFormattedMessage()   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
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Yiisoft\Validator\FormatterInterface;
9
use Yiisoft\Validator\Result;
10
use Yiisoft\Validator\Rule;
11
use Yiisoft\Validator\ValidationContext;
12
13
/**
14
 * Checks if at least {@see AtLeast::$min} of many object attributes are filled.
15
 */
16
#[Attribute(Attribute::TARGET_PROPERTY)]
17
final class AtLeast extends Rule
18
{
19 8
    public function __construct(
20
        /**
21
         * @var string[] The list of required attributes that will be checked.
22
         */
23
        private array $attributes,
24
        /**
25
         * @var int The minimum required quantity of filled attributes to pass the validation.
26
         */
27
        private int $min = 1,
28
        /**
29
         * @var string Message to display in case of error.
30
         */
31
        private string $message = 'The model is not valid. Must have at least "{min}" filled attributes.',
32
        ?FormatterInterface $formatter = null,
33
        bool $skipOnEmpty = false,
34
        bool $skipOnError = false,
35
        $when = null
36
    ) {
37 8
        parent::__construct(formatter: $formatter, skipOnEmpty: $skipOnEmpty, skipOnError: $skipOnError, when: $when);
38
    }
39
40
    /**
41
     * @param string[] $value
42
     *
43
     * @see $attributes
44
     */
45 1
    public function attributes(array $value): self
46
    {
47 1
        $new = clone $this;
48 1
        $new->attributes = $value;
49
50 1
        return $new;
51
    }
52
53
    /**
54
     * @see $min
55
     */
56 1
    public function min(int $value): self
57
    {
58 1
        $new = clone $this;
59 1
        $new->min = $value;
60
61 1
        return $new;
62
    }
63
64
    /**
65
     * @see $message
66
     */
67 1
    public function message(string $value): self
68
    {
69 1
        $new = clone $this;
70 1
        $new->message = $value;
71
72 1
        return $new;
73
    }
74
75 4
    protected function validateValue($value, ?ValidationContext $context = null): Result
76
    {
77 4
        $filledCount = 0;
78
79 4
        foreach ($this->attributes as $attribute) {
80 4
            if (!$this->isEmpty($value->{$attribute})) {
81 3
                $filledCount++;
82
            }
83
        }
84
85 4
        $result = new Result();
86
87 4
        if ($filledCount < $this->min) {
88 2
            $message = $this->getFormattedMessage();
89 2
            $result->addError($message);
90
        }
91
92 4
        return $result;
93
    }
94
95 7
    private function getFormattedMessage(): string
96
    {
97 7
        return $this->formatMessage($this->message, ['min' => $this->min]);
98
    }
99
100 5
    public function getOptions(): array
101
    {
102 5
        return array_merge(parent::getOptions(), [
103 5
            'attributes' => $this->attributes,
104 5
            'min' => $this->min,
105 5
            'message' => $this->getFormattedMessage(),
106
        ]);
107
    }
108
}
109