Passed
Push — master ( 3bffb6...f32028 )
by Aleksandr
31:58
created

RegexConstraint::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Drobotik\Eav\Validation\Constraints;
4
5
use Drobotik\Eav\Interface\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
    public function validate($value): ?string
19
    {
20
        if (!is_string($value)) {
21
            return "This value must be a string.";
22
        }
23
24
        if (!preg_match($this->pattern, $value)) {
25
            return $this->message;
26
        }
27
28
        return null;
29
    }
30
}