Passed
Pull Request — master (#222)
by Dmitriy
02:23
created

Regex   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 50
ccs 17
cts 17
cp 1
rs 10
c 1
b 0
f 0
wmc 8
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;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_TRAIT, expecting T_STRING or '{' on line 9 at column 27
Loading history...
10
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
11
use Yiisoft\Validator\RuleInterface;
12
13
/**
14
 * Validates that the value matches the pattern specified in constructor.
15
 *
16
 * If the {@see Regex::$not} is used, the rule will ensure the value do NOT match the pattern.
17
 */
18
#[Attribute(Attribute::TARGET_PROPERTY)]
19
final class Regex implements RuleInterface
20
{
21
    use HandlerClassNameTrait;
22
    use RuleNameTrait;
23
24
    public function __construct(
25
        /**
26
         * @var string the regular expression to be matched with
27
         */
28
        public string $pattern,
29
        /**
30
         * @var bool whether to invert the validation logic. Defaults to `false`. If set to `true`, the regular
31
         * expression defined via {@see $pattern} should NOT match the value.
32
         */
33
        public bool $not = false,
34
        public string $incorrectInputMessage = 'Value should be string.',
35
        public string $message = 'Value is invalid.',
36
        public bool $skipOnEmpty = false,
37
        public bool $skipOnError = false,
38
        public ?Closure $when = null,
39
    ) {
40
    }
41
42 2
    public function getOptions(): array
43
    {
44
        return [
45 2
            'pattern' => $this->pattern,
46 2
            'not' => $this->not,
47
            'incorrectInputMessage' => [
48 2
                'message' => $this->incorrectInputMessage,
49
            ],
50
            'message' => [
51 2
                'message' => $this->message,
52
            ],
53 2
            'skipOnEmpty' => $this->skipOnEmpty,
54 2
            'skipOnError' => $this->skipOnError,
55
        ];
56
    }
57
}
58