Completed
Pull Request — 1.x (#5)
by Dorian
01:32
created

RegexValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace MetaHydrator\Validator;
3
4
use MetaHydrator\Exception\ValidationException;
5
6
class RegexValidator extends AbstractValidator
7
{
8
    /**
9
     * @var string
10
     */
11
    private $pattern;
12
13
    /**
14
     * RegexValidator constructor.
15
     * @param string $pattern
16
     * @param string $errorMessage
17
     */
18
    public function __construct(string $pattern, $errorMessage = "")
19
    {
20
        parent::__construct($errorMessage);
21
        $this->pattern = $pattern;
22
    }
23
24
    /**
25
     * @param mixed $value
26
     * @param $contextObject
27
     *
28
     * @throws ValidationException
29
     */
30
    public function validate($value, $contextObject = null)
31
    {
32
        if ($value !== null && $value !== "" && !preg_match($this->pattern, strval($value))) {
33
            $this->throw();
34
        }
35
    }
36
}
37