Passed
Pull Request — master (#333)
by Sergei
02:38
created

Regex::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 23
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 7
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\SkipOnEmptyTrait;
10
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
11
use Yiisoft\Validator\Rule\Trait\WhenTrait;
12
use Yiisoft\Validator\SerializableRuleInterface;
13
use Yiisoft\Validator\SkipOnEmptyInterface;
14
use Yiisoft\Validator\SkipOnErrorInterface;
15
use Yiisoft\Validator\ValidationContext;
16
use Yiisoft\Validator\WhenInterface;
17
18
/**
19
 * Validates that the value matches the pattern specified in constructor.
20
 *
21
 * If the {@see Regex::$not} is used, the rule will ensure the value do NOT match the pattern.
22
 */
23
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
24
final class Regex implements SerializableRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
25
{
26
    use SkipOnEmptyTrait;
27
    use SkipOnErrorTrait;
28
    use WhenTrait;
29
30 4
    public function __construct(
31
        /**
32
         * @var string the regular expression to be matched with
33
         */
34
        private string $pattern,
35
        /**
36
         * @var bool whether to invert the validation logic. Defaults to `false`. If set to `true`, the regular
37
         * expression defined via {@see $pattern} should NOT match the value.
38
         */
39
        private bool $not = false,
40
        private string $incorrectInputMessage = 'Value should be string.',
41
        private string $message = 'Value is invalid.',
42
43
        /**
44
         * @var bool|callable|null
45
         */
46
        private $skipOnEmpty = null,
47
        private bool $skipOnError = false,
48
        /**
49
         * @var Closure(mixed, ValidationContext):bool|null
50
         */
51
        private ?Closure $when = null,
52
    ) {
53
    }
54
55 1
    public function getName(): string
56
    {
57 1
        return 'regex';
58
    }
59
60 7
    public function getPattern(): string
61
    {
62 7
        return $this->pattern;
63
    }
64
65 7
    public function isNot(): bool
66
    {
67 7
        return $this->not;
68
    }
69
70 7
    public function getIncorrectInputMessage(): string
71
    {
72 7
        return $this->incorrectInputMessage;
73
    }
74
75 4
    public function getMessage(): string
76
    {
77 4
        return $this->message;
78
    }
79
80 2
    public function getOptions(): array
81
    {
82
        return [
83 2
            'pattern' => $this->pattern,
84 2
            'not' => $this->not,
85
            'incorrectInputMessage' => [
86 2
                'message' => $this->incorrectInputMessage,
87
            ],
88
            'message' => [
89 2
                'message' => $this->message,
90
            ],
91 2
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
92 2
            'skipOnError' => $this->skipOnError,
93
        ];
94
    }
95
96 3
    public function getHandlerClassName(): string
97
    {
98 3
        return RegexHandler::class;
99
    }
100
}
101