CheckPattern::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace mindplay\kissform\Validators;
4
5
use mindplay\kissform\Facets\FieldInterface;
6
use mindplay\kissform\Facets\ValidatorInterface;
7
use mindplay\kissform\InputModel;
8
use mindplay\kissform\InputValidation;
9
10
/**
11
 * Validate input matching a regular expression pattern.
12
 */
13
class CheckPattern implements ValidatorInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    private $pattern;
19
20
    /**
21
     * @var string
22
     */
23
    private $error;
24
25
    /**
26
     * @param string $pattern regular expression pattern to match
27
     * @param string $error   error message
28
     */
29 5
    public function __construct($pattern, $error)
30
    {
31 5
        $this->pattern = $pattern;
32 5
        $this->error = $error;
33 5
    }
34
35 1
    public function validate(FieldInterface $field, InputModel $model, InputValidation $validation)
36
    {
37 1
        $input = $model->getInput($field);
38
39 1
        if ($input === null) {
40 1
            return; // no value
41
        }
42
43 1
        if (!preg_match($this->pattern, $input)) {
44 1
            $model->setError($field, $this->error);
45
        }
46 1
    }
47
}
48