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

AtLeastValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 21
ccs 9
cts 9
cp 1
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 17 4
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