Passed
Pull Request — master (#41)
by Alexander
09:27
created

MatchRegularExpression::message()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yiisoft\Validator\Rule;
4
5
use yii\helpers\Yii;
0 ignored issues
show
Bug introduced by
The type yii\helpers\Yii was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use yii\exceptions\InvalidConfigException;
0 ignored issues
show
Bug introduced by
The type yii\exceptions\InvalidConfigException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Yiisoft\Validator\DataSetInterface;
8
use Yiisoft\Validator\Result;
9
use Yiisoft\Validator\Rule;
10
11
/**
12
 * RegularExpressionValidator validates that the attribute value matches the specified [[pattern]].
13
 *
14
 * If the [[not]] property is set true, the validator will ensure the attribute value do NOT match the [[pattern]].
15
 */
16
class MatchRegularExpression extends Rule
17
{
18
    /**
19
     * @var string the regular expression to be matched with
20
     */
21
    private string $pattern;
22
    /**
23
     * @var bool whether to invert the validation logic. Defaults to false. If set to true,
24
     * the regular expression defined via [[pattern]] should NOT match the attribute value.
25
     */
26
    private bool $not = false;
27
28
    private string $message = 'Value is invalid.';
29
30 1
    public function __construct(string $pattern)
31
    {
32 1
        $this->pattern = $pattern;
33
    }
34
35 1
    public function not(): self
36
    {
37 1
        $this->not = true;
38 1
        return $this;
39
    }
40
41
    public function message(string $message): self
42
    {
43
        $this->message = $message;
44
        return $this;
45
    }
46
47 1
    protected function validateValue($value, DataSetInterface $dataSet = null): Result
48
    {
49 1
        $result = new Result();
50
51 1
        $valid = !is_array($value) &&
52 1
            ((!$this->not && preg_match($this->pattern, $value))
53 1
            || ($this->not && !preg_match($this->pattern, $value)));
54
55 1
        if (!$valid) {
56 1
            $result->addError($this->formatMessage($this->message));
57
        }
58
59 1
        return $result;
60
    }
61
}
62