UUIDValidator::validate()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 1
crap 4
1
<?php
2
3
namespace Nicofuma\SwaggerBundle\JsonSchema\Constraints\Format;
4
5
use Nicofuma\SwaggerBundle\Exception\FormatConstraintException;
6
use Symfony\Component\Validator\Constraints\Uuid;
7
use Symfony\Component\Validator\ConstraintViolation;
8
use Symfony\Component\Validator\Validator\ValidatorInterface;
9
10
class UUIDValidator implements FormatValidatorInterface
11
{
12
    /** @var ValidatorInterface */
13
    private $validator;
14
15 2
    public function __construct(ValidatorInterface $validator = null)
16
    {
17 2
        $this->validator = $validator;
18 2
    }
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 2
    public function validate($value)
24
    {
25 2
        if ($this->validator !== null) {
26 2
            $errorList = $this->validator->validate($value, [new Uuid()]);
27
28 2
            if (count($errorList) > 0) {
29 1
                $errors = [];
30
31
                /** @var ConstraintViolation $error */
32 1
                foreach ($errorList as $error) {
33 1
                    $errors[] = $error->getMessage();
34 1
                }
35
36 1
                throw new FormatConstraintException($errors);
37
            }
38 1
        }
39 1
    }
40
}
41