Completed
Push — master ( cce103...0f1e5a )
by Albert
09:54
created

RegexValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 26
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A verdict() 0 13 2
A validateOptions() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Albert221\Validation\Rule;
6
7
use Albert221\Validation\Rule;
8
use Albert221\Validation\RuleValidator;
9
use Albert221\Validation\Verdict;
10
use Albert221\Validation\VerdictInterface;
11
use InvalidArgumentException;
12
13
class RegexValidator extends RuleValidator
14
{
15
    public function verdict($value, Rule $rule): VerdictInterface
16
    {
17
        $this->validateOptions($rule->getOptions());
18
19
        if (is_null($value)) {
20
            return Verdict::passing($rule);
21
        }
22
23
        return Verdict::create(
24
            1 === preg_match($rule->getOption('pattern'), $value),
25
            $rule
26
        );
27
    }
28
29
    /**
30
     * @param array $options
31
     */
32
    private function validateOptions(array $options): void
33
    {
34
        if (!array_key_exists('pattern', $options)) {
35
            throw new InvalidArgumentException('"pattern" option is required.');
36
        }
37
    }
38
}
39