Passed
Pull Request — master (#222)
by Alexander
02:58
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\Rule\Trait\RuleNameTrait;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_TRAIT, expecting T_STRING or '{' on line 10 at column 27
Loading history...
11
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
12
use Yiisoft\Validator\ParametrizedRuleInterface;
13
14
/**
15
 * Validates that the specified value is neither null nor empty.
16
 */
17
#[Attribute(Attribute::TARGET_PROPERTY)]
18
final class Required implements ParametrizedRuleInterface
19
{
20
    use HandlerClassNameTrait;
21
    use RuleNameTrait;
22
23 12
    public function __construct(
24
        private string $message = 'Value cannot be blank.',
25
        private bool $skipOnEmpty = false,
26
        private bool $skipOnError = false,
27
        private ?Closure $when = null,
28
    ) {
29
    }
30
31
    /**
32
     * @return string
33
     */
34 5
    public function getMessage(): string
35
    {
36 5
        return $this->message;
37
    }
38
39
    /**
40
     * @return bool
41
     */
42
    public function isSkipOnEmpty(): bool
43
    {
44
        return $this->skipOnEmpty;
45
    }
46
47
    /**
48
     * @return bool
49
     */
50
    public function isSkipOnError(): bool
51
    {
52
        return $this->skipOnError;
53
    }
54
55
    /**
56
     * @return Closure|null
57
     */
58
    public function getWhen(): ?Closure
59
    {
60
        return $this->when;
61
    }
62
63 2
    #[ArrayShape(['message' => 'string[]', 'skipOnEmpty' => 'bool', 'skipOnError' => 'bool'])]
64
    public function getOptions(): array
65
    {
66
        return [
67
            'message' => [
68 2
                'message' => $this->message,
69
            ],
70 2
            'skipOnEmpty' => $this->skipOnEmpty,
71 2
            'skipOnError' => $this->skipOnError,
72
        ];
73
    }
74
}
75