Passed
Pull Request — master (#222)
by Alexander
05:06 queued 02:29
created

AtLeastValidator::validate()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 6
nop 4
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule\AtLeast;
6
7
use Yiisoft\Validator\Result;
8
use Yiisoft\Validator\Rule\EmptyCheckTrait;
9
use Yiisoft\Validator\Rule\RuleValidatorInterface;
10
use Yiisoft\Validator\ValidationContext;
11
use Yiisoft\Validator\ValidatorInterface;
12
13
/**
14
 * Checks if at least {@see AtLeast::$min} of many attributes are filled.
15
 */
16
final class AtLeastValidator implements RuleValidatorInterface
17
{
18
    use EmptyCheckTrait;
19
20 5
    public function validate(mixed $value, object $rule, ValidatorInterface $validator, ?ValidationContext $context = null): Result
21
    {
22 5
        $filledCount = 0;
23
24 5
        foreach ($rule->attributes as $attribute) {
25 5
            if (!$this->isEmpty($value->{$attribute})) {
26 4
                $filledCount++;
27
            }
28
        }
29
30 5
        $result = new Result();
31
32 5
        if ($filledCount < $rule->min) {
33 3
            $result->addError($rule->message, ['min' => $rule->min]);
34
        }
35
36 5
        return $result;
37
    }
38
}
39