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

RequiredValidator::validate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 2
nop 4
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 3
rs 10
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