Completed
Push — master ( f1dfa3...73b458 )
by Lee
01:38
created

AbstractRegexValidator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 38
ccs 0
cts 9
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 14 3
1
<?php
2
3
namespace Lavibi\Popoya;
4
5
abstract class AbstractRegexValidator extends AbstractValidator
6
{
7
    const NOT_STRING = 'not_string';
8
    const NOT_MATCH = 'not_match';
9
    const NOT_STRING_MESSAGE = 'The given value must be string.';
10
    const NOT_MATCH_MESSAGE = 'The given value is not match regex.';
11
12
    protected $messages = [
13
        self::NOT_STRING => self::NOT_STRING_MESSAGE,
14
        self::NOT_MATCH => self::NOT_MATCH_MESSAGE
15
    ];
16
17
    protected $requiredOptions = [
18
        'regex'
19
    ];
20
21
    protected $notMatchCode = self::NOT_MATCH;
22
23
    /**
24
     * @param $value
25
     *
26
     * @return bool
27
     */
28
    public function isValid($value)
29
    {
30
        $this->value = $this->standardValue = $value;
31
32
        if (!is_string($value)) {
33
            $this->setError(self::NOT_STRING);
34
            return false;
35
        }
36
37
        if (!preg_match($this->options['regex'], $value)) {
38
            $this->setError($this->notMatchCode);
39
            return false;
40
        }
41
    }
42
}
43