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