PatternConstraint   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 46
c 0
b 0
f 0
wmc 7
lcom 0
cbo 4
ccs 17
cts 17
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A keywords() 0 4 1
A supports() 0 4 1
A normalize() 0 14 3
A apply() 0 6 2
1
<?php
2
3
/*
4
 * This file is part of the JVal package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace JVal\Constraint;
11
12
use JVal\Constraint;
13
use JVal\Context;
14
use JVal\Exception\Constraint\InvalidRegexException;
15
use JVal\Exception\Constraint\InvalidTypeException;
16
use JVal\Types;
17
use JVal\Utils;
18
use JVal\Walker;
19
use stdClass;
20
21
/**
22
 * Constraint for the "pattern" keyword.
23
 */
24
class PatternConstraint implements Constraint
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 373
    public function keywords()
30
    {
31 373
        return ['pattern'];
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 357
    public function supports($type)
38
    {
39 357
        return $type === Types::TYPE_STRING;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 15
    public function normalize(stdClass $schema, Context $context, Walker $walker)
46
    {
47 15
        $context->enterNode('pattern');
48
49 15
        if (!is_string($schema->pattern)) {
50 1
            throw new InvalidTypeException($context, Types::TYPE_STRING);
51
        }
52
53 14
        if (!Utils::isValidRegex($schema->pattern)) {
54 1
            throw new InvalidRegexException($context);
55
        }
56
57 13
        $context->leaveNode();
58 13
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 10
    public function apply($instance, stdClass $schema, Context $context, Walker $walker)
64
    {
65 10
        if (!Utils::matchesRegex($instance, $schema->pattern)) {
66 4
            $context->addViolation('should match regex "%s"', [$schema->pattern]);
67 4
        }
68 10
    }
69
}
70