Completed
Push — master ( 8b8539...de5cc9 )
by Tristan
13:08 queued 08:55
created

FormatConstraint   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 40
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B check() 0 20 5
A addFormatValidator() 0 4 1
1
<?php
2
3
namespace Nicofuma\SwaggerBundle\JsonSchema\Constraints;
4
5
use JsonSchema\Constraints\FormatConstraint as FormatConstraintBase;
6
use Nicofuma\SwaggerBundle\Exception\FormatConstraintException;
7
use Nicofuma\SwaggerBundle\JsonSchema\Constraints\Format\FormatValidatorInterface;
8
9
class FormatConstraint extends FormatConstraintBase
10
{
11
    /** @var FormatValidatorInterface[] */
12
    private $formatMap = [];
13
14
    /**
15
     * {@inheritdoc}
16
     */
17 4
    public function check($element, $schema = null, $path = null, $i = null)
18
    {
19 4
        if (!isset($schema->format)) {
20 1
            return;
21
        }
22
23 3
        if (array_key_exists($schema->format, $this->formatMap)) {
24
            try {
25 1
                $this->formatMap[$schema->format]->validate($element);
26 1
            } catch (FormatConstraintException $e) {
27 1
                foreach ($e->getErrors() as $error) {
28 1
                    $this->addError($path, $error, 'format');
29 1
                }
30
            }
31
32 1
            return;
33
        }
34
35 2
        parent::check($element, $schema, $path, $i);
36 2
    }
37
38
    /**
39
     * Add a format validator.
40
     *
41
     * @param string                   $format
42
     * @param FormatValidatorInterface $formatValidator
43
     */
44 1
    public function addFormatValidator($format, FormatValidatorInterface $formatValidator)
45
    {
46 1
        $this->formatMap[$format] = $formatValidator;
47 1
    }
48
}
49