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

AbstractRegexValidator::isValid()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 9
cp 0
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
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