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

FormatConstraint::check()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 4
nop 4
crap 5
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