Passed
Pull Request — master (#332)
by Dmitriy
03:02
created

Required::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 0
c 0
b 0
f 0
dl 0
loc 14
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 5
crap 1
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\BeforeValidationInterface;
10
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
11
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
12
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
13
use Yiisoft\Validator\SerializableRuleInterface;
14
use Yiisoft\Validator\SkipOnEmptyInterface;
15
use Yiisoft\Validator\ValidationContext;
16
17
/**
18
 * Validates that the specified value is neither null nor empty.
19
 */
20
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
21
final class Required implements SerializableRuleInterface, BeforeValidationInterface, SkipOnEmptyInterface
22
{
23
    use BeforeValidationTrait;
24
    use RuleNameTrait;
25
    use SkipOnEmptyTrait;
26
27 38
    public function __construct(
28
        private string $message = 'Value cannot be blank.',
29
        private string $notPassedMessage = 'Value not passed.',
30
31
        /**
32
         * @var bool|callable|null
33
         */
34
        private $skipOnEmpty = null,
35
        private bool $skipOnError = false,
36
        /**
37
         * @var Closure(mixed, ValidationContext):bool|null
38
         */
39
        private ?Closure $when = null,
40
    ) {
41
    }
42
43 15
    public function getMessage(): string
44
    {
45 15
        return $this->message;
46
    }
47
48 5
    public function getNotPassedMessage(): string
49
    {
50 5
        return $this->notPassedMessage;
51
    }
52
53 1
    public function getOptions(): array
54
    {
55
        return [
56 1
            'message' => $this->message,
57 1
            'notPassedMessage' => $this->notPassedMessage,
58 1
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
59 1
            'skipOnError' => $this->skipOnError,
60
        ];
61
    }
62
63 40
    public function getHandlerClassName(): string
64
    {
65 40
        return RequiredHandler::class;
66
    }
67
}
68