Pattern::validate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 3
dl 0
loc 14
ccs 7
cts 7
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonGuard\Constraint\DraftFour;
4
5
use League\JsonGuard;
6
use League\JsonGuard\Assert;
7
use League\JsonGuard\ConstraintInterface;
8
use League\JsonGuard\Validator;
9
use function League\JsonGuard\error;
10
11
final class Pattern implements ConstraintInterface
12
{
13
    const KEYWORD = 'pattern';
14
15
    /**
16
     * {@inheritdoc}
17
     */
18 4
    public function validate($value, $parameter, Validator $validator)
19
    {
20 4
        Assert::type($parameter, 'string', self::KEYWORD, $validator->getSchemaPath());
21
22 2
        if (!is_string($value)) {
23 2
            return null;
24
        }
25
26 2
        if (preg_match(JsonGuard\delimit_pattern($parameter), $value) === 1) {
27 2
            return null;
28
        }
29
30 2
        return error('The string must match the pattern {parameter}.', $validator);
31
    }
32
}
33