Passed
Push — master ( 9562b0...4734f7 )
by Sergei
04:27 queued 02:02
created

Regex::getHandlerClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
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 JetBrains\PhpStorm\Language;
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\RuleWithOptionsInterface;
14
use Yiisoft\Validator\SkipOnEmptyInterface;
15
use Yiisoft\Validator\SkipOnErrorInterface;
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
 * @psalm-import-type WhenType from WhenInterface
24
 */
25
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
26
final class Regex implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
27
{
28
    use SkipOnEmptyTrait;
29
    use SkipOnErrorTrait;
30
    use WhenTrait;
31 5
32
    public function __construct(
33
        #[Language('RegExp')]
34
        /**
35
         * @var string the regular expression to be matched with
36
         */
37
        private string $pattern,
38
        /**
39
         * @var bool whether to invert the validation logic. Defaults to `false`. If set to `true`, the regular
40
         * expression defined via {@see $pattern} should NOT match the value.
41
         */
42
        private bool $not = false,
43
        private string $incorrectInputMessage = 'Value should be string.',
44
        private string $message = 'Value is invalid.',
45
0 ignored issues
show
Coding Style introduced by
Blank lines are not allowed in a multi-line function declaration
Loading history...
46
        /**
47
         * @var bool|callable|null
48
         */
49
        private $skipOnEmpty = null,
50
        private bool $skipOnError = false,
51
        /**
52
         * @var WhenType
53
         */
54
        private Closure|null $when = null,
55
    ) {
56
    }
57 1
58
    public function getName(): string
59 1
    {
60
        return 'regex';
61
    }
62 9
63
    public function getPattern(): string
64 9
    {
65
        return $this->pattern;
66
    }
67 9
68
    public function isNot(): bool
69 9
    {
70
        return $this->not;
71
    }
72 9
73
    public function getIncorrectInputMessage(): string
74 9
    {
75
        return $this->incorrectInputMessage;
76
    }
77 6
78
    public function getMessage(): string
79 6
    {
80
        return $this->message;
81
    }
82 2
83
    public function getOptions(): array
84
    {
85 2
        return [
86 2
            'pattern' => $this->pattern,
87
            'not' => $this->not,
88 2
            'incorrectInputMessage' => [
89
                'template' => $this->incorrectInputMessage,
90
                'parameters' => [],
91
            ],
92 2
            'message' => [
93
                'template' => $this->message,
94
                'parameters' => [],
95 2
            ],
96 2
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
97
            'skipOnError' => $this->skipOnError,
98
        ];
99
    }
100 18
101
    public function getHandler(): string
102 18
    {
103
        return RegexHandler::class;
104
    }
105
}
106