RequiredHandler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 22 4
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Yiisoft\Validator\EmptyCondition\WhenEmpty;
8
use Yiisoft\Validator\Exception\UnexpectedRuleException;
9
use Yiisoft\Validator\Result;
10
use Yiisoft\Validator\RuleHandlerInterface;
11
use Yiisoft\Validator\RuleHandlerResolver\SimpleRuleHandlerContainer;
12
use Yiisoft\Validator\ValidationContext;
13
14
/**
15
 * A handler for {@see Required} rule. Validates that the specified value is passed and is not empty.
16
 *
17 46
 * @psalm-import-type EmptyConditionType from Required
18
 */
19 46
final class RequiredHandler implements RuleHandlerInterface
20 1
{
21
    /**
22
     * @var callable An empty condition (either a callable or class implementing `__invoke()` method) used to
23 45
     * determine emptiness of the value. The signature must be like the following:
24 45
     *
25 5
     * ```php
26
     * function (mixed $value, bool $isAttributeMissing): bool
27 5
     * ```
28
     *
29
     * `$isAttributeMissing` is a flag defining whether the attribute is missing (not used / not passed at all).
30 41
     *
31 26
     * Used as a default when {@see Required::$emptyCondition} is not set. A customized handler can be added to
32
     * {@see SimpleRuleHandlerContainer::$instances} to be applied to all rules of this type without explicitly
33
     * specifying empty condition for each one of them.
34 15
     *
35
     * @psalm-var EmptyConditionType
36 15
     */
37
    private $defaultEmptyCondition;
38
39
    /**
40
     * @param callable|null $defaultEmptyCondition A default empty condition used to determine emptiness of the value.
41
     *
42
     * @psalm-param EmptyConditionType|null $defaultEmptyCondition
43
     */
44
    public function __construct(
45
        callable|null $defaultEmptyCondition = null,
46
    ) {
47
        $this->defaultEmptyCondition = $defaultEmptyCondition ?? new WhenEmpty(trimString: true);
48
    }
49
50
    public function validate(mixed $value, object $rule, ValidationContext $context): Result
51
    {
52
        if (!$rule instanceof Required) {
53
            throw new UnexpectedRuleException(Required::class, $rule);
54
        }
55
56
        $result = new Result();
57
        if ($context->isAttributeMissing()) {
58
            $result->addError($rule->getNotPassedMessage(), ['attribute' => $context->getTranslatedAttribute()]);
59
60
            return $result;
61
        }
62
63
        $emptyCondition = $rule->getEmptyCondition() ?? $this->defaultEmptyCondition;
64
65
        if (!$emptyCondition($value, $context->isAttributeMissing())) {
66
            return $result;
67
        }
68
69
        $result->addError($rule->getMessage(), ['attribute' => $context->getTranslatedAttribute()]);
70
71
        return $result;
72
    }
73
}
74