Passed
Push — master ( 2d0b5c...b0044e )
by Sergei
02:41
created

StringValueHandler::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
nc 3
nop 3
dl 0
loc 14
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Yiisoft\Validator\Exception\UnexpectedRuleException;
8
use Yiisoft\Validator\Result;
9
use Yiisoft\Validator\RuleHandlerInterface;
10
use Yiisoft\Validator\ValidationContext;
11
12
use function is_string;
13
14
/**
15
 * Validates that the value is a string.
16
 *
17
 * @see StringValue
18
 */
19
final class StringValueHandler implements RuleHandlerInterface
20
{
21
    public function validate($value, object $rule, ValidationContext $context): Result
22
    {
23
        if (!$rule instanceof StringValue) {
24
            throw new UnexpectedRuleException(StringValue::class, $rule);
25
        }
26
27
        if (!is_string($value)) {
28
            return (new Result())->addError($rule->getMessage(), [
29
                'attribute' => $context->getTranslatedAttribute(),
30
                'type' => get_debug_type($value),
31
            ]);
32
        }
33
34
        return new Result();
35
    }
36
}
37