Passed
Push — master ( 0363dc...b69f59 )
by Rustam
02:19
created

Required   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 12
c 0
b 0
f 0
dl 0
loc 35
ccs 7
cts 7
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptions() 0 9 1
A getMessage() 0 3 1
A __construct() 0 9 1
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\BeforeValidationInterface;
12
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
13
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
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, BeforeValidationInterface
22
{
23
    use BeforeValidationTrait;
24
    use HandlerClassNameTrait;
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