Passed
Push — master ( 36a33a...a2faa0 )
by Aleksandr
01:53
created

RegexConstraint::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Drobotik\Eav\Validation\Constraints;
4
5
use Drobotik\Eav\Interfaces\ConstraintInterface;
6
7
class RegexConstraint implements ConstraintInterface
8
{
9
    private string $pattern;
10
    private string $message;
11
12
    public function __construct(string $pattern, string $message = "This value does not match the required pattern.")
13
    {
14
        $this->pattern = $pattern;
15
        $this->message = $message;
16
    }
17
18 1
    public function validate($value): ?string
19
    {
20 1
        if (!preg_match($this->pattern, $value)) {
21 1
            return $this->message;
22
        }
23
24 1
        return null;
25
    }
26
}