Passed
Pull Request — master (#222)
by Dmitriy
02:24
created

RequiredValidator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 13
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 13
ccs 5
cts 5
cp 1
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 9 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule\Required;
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
use function is_string;
13
14
/**
15
 * Validates that the specified value is neither null nor empty.
16
 */
17
final class RequiredValidator implements RuleValidatorInterface
18
{
19
    use EmptyCheckTrait;
20
21 19
    public function validate(mixed $value, object $rule, ValidatorInterface $validator, ?ValidationContext $context = null): Result
22
    {
23 19
        $result = new Result();
24
25 19
        if ($this->isEmpty(is_string($value) ? trim($value) : $value)) {
26 5
            $result->addError($rule->message);
27
        }
28
29 19
        return $result;
30
    }
31
}
32