Passed
Pull Request — master (#331)
by Sergei
02:46
created

Required::getNotPassedMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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