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

Required   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 28
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use JetBrains\PhpStorm\ArrayShape;
10
use Yiisoft\Validator\ParametrizedRuleInterface;
11
use Yiisoft\Validator\PreValidatableRuleInterface;
12
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_TRAIT, expecting T_STRING or '{' on line 12 at column 27
Loading history...
13
use Yiisoft\Validator\Rule\Trait\PreValidatableTrait;
14
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
15
use Yiisoft\Validator\ValidationContext;
16
17
/**
18
 * Validates that the specified value is neither null nor empty.
19
 */
20
#[Attribute(Attribute::TARGET_PROPERTY)]
21
final class Required implements ParametrizedRuleInterface, PreValidatableRuleInterface
22
{
23
    use HandlerClassNameTrait;
24
    use PreValidatableTrait;
25
    use RuleNameTrait;
26
27 14
    public function __construct(
28
        private string $message = 'Value cannot be blank.',
29
        private bool $skipOnEmpty = false,
30
        private bool $skipOnError = false,
31
        /**
32
         * @var Closure(mixed, ValidationContext):bool|null
33
         */
34
        private ?Closure $when = null,
35
    ) {
36
    }
37
38
    /**
39
     * @return string
40
     */
41 6
    public function getMessage(): string
42
    {
43 6
        return $this->message;
44
    }
45
46 2
    #[ArrayShape(['message' => 'string[]', 'skipOnEmpty' => 'bool', 'skipOnError' => 'bool'])]
47
    public function getOptions(): array
48
    {
49
        return [
50
            'message' => [
51 2
                'message' => $this->message,
52
            ],
53 2
            'skipOnEmpty' => $this->skipOnEmpty,
54 2
            'skipOnError' => $this->skipOnError,
55
        ];
56
    }
57
}
58