Required::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 5
dl 0
loc 8
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
10
use Yiisoft\Validator\Rule\Trait\WhenTrait;
11
use Yiisoft\Validator\DumpedRuleInterface;
12
use Yiisoft\Validator\SkipOnErrorInterface;
13
use Yiisoft\Validator\WhenInterface;
14
15
/**
16
 * Contains a set of options to determine if the value is not empty according to {@see Required::$emptyCondition}. When
17
 * rule-level condition is not set, a handler-level condition ({@see RequiredHandler::$defaultEmptyCondition}) is
18
 * applied (which is also customizable). In case of using attributes, the attribute must be present with passed
19
 * non-empty value.
20
 *
21
 * With default settings in order for value to pass the validation it must satisfy all the following conditions:
22
 *
23
 * - Passed.
24
 * - Not `null`.
25
 * - Not an empty string (after trimming).
26
 * - Not an empty iterable.
27
 *
28
 * When using with other rules, it must come first.
29
 *
30
 * @see RequiredHandler Corresponding handler performing the actual validation.
31
 *
32
 * @psalm-type EmptyConditionType = callable(mixed,bool):bool
33
 *
34
 * @psalm-import-type WhenType from WhenInterface
35
 */
36
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
37 37
final class Required implements DumpedRuleInterface, SkipOnErrorInterface, WhenInterface
38
{
39
    use SkipOnErrorTrait;
40
    use WhenTrait;
41
42
    /**
43
     * @var callable|null An empty condition (either a callable or class implementing `__invoke()` method) used to
44
     * determine emptiness of the value. The signature must be like the following:
45
     *
46
     * ```php
47 37
     * function (mixed $value, bool $isAttributeMissing): bool
48
     * ```
49
     *
50 1
     * `$isAttributeMissing` is a flag defining whether the attribute is missing (not used / not passed at all).
51
     *
52 1
     * @psalm-var EmptyConditionType|null
53
     */
54
    private $emptyCondition;
55 16
56
    /**
57 16
     * @param string $message Error message used when validation fails because the validated value is empty.
58
     *
59
     * You may use the following placeholders in the message:
60 6
     *
61
     * - `{attribute}`: the translated label of the attribute being validated.
62 6
     * @param string $notPassedMessage Error message used when validation fails because the validated value is not
63
     * passed.
64
     *
65
     * You may use the following placeholders in the message:
66
     *
67
     * - `{attribute}`: the translated label of the attribute being validated.
68 45
     * @param callable|null $emptyCondition An empty condition used to determine emptiness of the value.
69
     *
70 45
     * @psalm-param EmptyConditionType|null $emptyCondition
71
     *
72
     * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error. See
73 1
     * {@see SkipOnErrorInterface}.
74
     * @param Closure|null $when A callable to define a condition for applying the rule. See {@see WhenInterface}.
75
     *
76
     * @psalm-param WhenType $when
77 1
     */
78
    public function __construct(
79
        private string $message = 'Value cannot be blank.',
80
        private string $notPassedMessage = 'Value not passed.',
81 1
        callable|null $emptyCondition = null,
82
        private bool $skipOnError = false,
83
        private Closure|null $when = null,
84 1
    ) {
85
        $this->emptyCondition = $emptyCondition;
86
    }
87
88 46
    public function getName(): string
89
    {
90 46
        return self::class;
91
    }
92
93
    /**
94
     * Gets error message used when validation fails because the validated value is empty.
95
     *
96
     * @return string Error message / template.
97
     *
98
     * @see $message
99
     */
100
    public function getMessage(): string
101
    {
102
        return $this->message;
103
    }
104
105
    /**
106
     * Gets error message used when validation fails because the validated value is not passed.
107
     *
108
     * @return string Error message / template.
109
     *
110
     * @see $message
111
     */
112
    public function getNotPassedMessage(): string
113
    {
114
        return $this->notPassedMessage;
115
    }
116
117
    /**
118
     * Gets empty condition used to determine emptiness of the value.
119
     *
120
     * @return callable|null Empty condition.
121
     *
122
     * @psalm-return EmptyConditionType|null
123
     *
124
     * @see $emptyCondition
125
     */
126
    public function getEmptyCondition(): ?callable
127
    {
128
        return $this->emptyCondition;
129
    }
130
131
    public function getOptions(): array
132
    {
133
        return [
134
            'message' => [
135
                'template' => $this->message,
136
                'parameters' => [],
137
            ],
138
            'notPassedMessage' => [
139
                'template' => $this->notPassedMessage,
140
                'parameters' => [],
141
            ],
142
            'skipOnError' => $this->skipOnError,
143
        ];
144
    }
145
146
    public function getHandler(): string
147
    {
148
        return RequiredHandler::class;
149
    }
150
}
151