Passed
Pull Request — master (#517)
by
unknown
04:04 queued 01:19
created

Required::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 12
ccs 0
cts 0
cp 0
crap 2
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\RuleWithOptionsInterface;
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 array.
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
 * @psalm-import-type WhenType from WhenInterface
34
 */
35
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
36
final class Required implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface
37 37
{
38
    use SkipOnErrorTrait;
39
    use WhenTrait;
40
41
    /**
42
     * @var callable|null An empty condition (either a callable or class implementing `__invoke()` method) used to
43
     * determine emptiness of the value. The signature must be like the following:
44
     *
45
     * ```php
46
     * function (mixed $value, bool $isAttributeMissing): bool
47 37
     * ```
48
     *
49
     * `$isAttributeMissing` is a flag defining whether the attribute is missing (not used / not passed at all).
50 1
     *
51
     * @psalm-var EmptyConditionType|null
52 1
     */
53
    private $emptyCondition;
54
55 16
    /**
56
     * @param string $message Error message used when validation fails because the validated value is empty.
57 16
     *
58
     * You may use the following placeholders in the message:
59
     *
60 6
     * - `{attribute}`: the translated label of the attribute being validated.
61
     * @param string $notPassedMessage Error message used when validation fails because the validated value is not
62 6
     * passed.
63
     *
64
     * You may use the following placeholders in the message:
65
     *
66
     * - `{attribute}`: the translated label of the attribute being validated.
67
     * @param callable|null $emptyCondition An empty condition used to determine emptiness of the value.
68 45
     * @psalm-param EmptyConditionType|null $emptyCondition
69
     *
70 45
     * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error. See
71
     * {@see SkipOnErrorInterface}.
72
     * @param Closure|null $when A callable to define a condition for applying the rule. See {@see WhenInterface}.
73 1
     * @psalm-param WhenType $when
74
     */
75
    public function __construct(
76
        private string $message = 'Value cannot be blank.',
77 1
        private string $notPassedMessage = 'Value not passed.',
78
        callable|null $emptyCondition = null,
79
        private bool $skipOnError = false,
80
        private Closure|null $when = null,
81 1
    ) {
82
        $this->emptyCondition = $emptyCondition;
83
    }
84 1
85
    public function getName(): string
86
    {
87
        return 'required';
88 46
    }
89
90 46
    /**
91
     * Gets error message used when validation fails because the validated value is empty.
92
     *
93
     * @return string Error message / template.
94
     *
95
     * @see $message
96
     */
97
    public function getMessage(): string
98
    {
99
        return $this->message;
100
    }
101
102
    /**
103
     * Gets error message used when validation fails because the validated value is not passed.
104
     *
105
     * @return string Error message / template.
106
     *
107
     * @see $message
108
     */
109
    public function getNotPassedMessage(): string
110
    {
111
        return $this->notPassedMessage;
112
    }
113
114
    /**
115
     * Gets empty condition used to determine emptiness of the value.
116
     *
117
     * @return callable|null Empty condition.
118
     * @psalm-return EmptyConditionType|null
119
     *
120
     * @see $emptyCondition
121
     */
122
    public function getEmptyCondition(): ?callable
123
    {
124
        return $this->emptyCondition;
125
    }
126
127
    public function getOptions(): array
128
    {
129
        return [
130
            'message' => [
131
                'template' => $this->message,
132
                'parameters' => [],
133
            ],
134
            'notPassedMessage' => [
135
                'template' => $this->notPassedMessage,
136
                'parameters' => [],
137
            ],
138
            'skipOnError' => $this->skipOnError,
139
        ];
140
    }
141
142
    public function getHandler(): string
143
    {
144
        return RequiredHandler::class;
145
    }
146
}
147