Completed
Push — 1.x ( 2692eb...201d44 )
by Dorian
12s
created

RegexValidator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 31
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A validate() 0 6 4
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