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

FormatConstraint::addFormatValidator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 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