Passed
Pull Request — master (#245)
by Rustam
12:07
created

Regex   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 25
c 1
b 0
f 0
dl 0
loc 81
ccs 16
cts 16
cp 1
rs 10

6 Methods

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