Completed
Push — master ( 799d88...dee4c0 )
by Antoine
21s queued 13s
created

PropertySchemaFormat::create()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 9
c 1
b 0
f 1
nc 5
nop 2
dl 0
loc 19
rs 9.6111
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\Restriction;
15
16
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
17
use Symfony\Component\Validator\Constraint;
18
use Symfony\Component\Validator\Constraints\Email;
19
use Symfony\Component\Validator\Constraints\Ip;
20
use Symfony\Component\Validator\Constraints\Uuid;
21
22
/**
23
 * Class PropertySchemaFormat.
24
 *
25
 * @author Andrii Penchuk [email protected]
26
 */
27
class PropertySchemaFormat implements PropertySchemaRestrictionMetadataInterface
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function create(Constraint $constraint, PropertyMetadata $propertyMetadata): array
33
    {
34
        if ($constraint instanceof Email) {
35
            return  ['format' => 'email'];
36
        }
37
38
        if ($constraint instanceof Uuid) {
39
            return ['format' => 'uuid'];
40
        }
41
42
        if ($constraint instanceof Ip) {
43
            if ($constraint->version === $constraint::V4) {
44
                return ['format' => 'ipv4'];
45
            }
46
47
            return ['format' => 'ipv6'];
48
        }
49
50
        return [];
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function supports(Constraint $constraint, PropertyMetadata $propertyMetadata): bool
57
    {
58
        $schema = $propertyMetadata->getSchema();
59
60
        return empty($schema['format']);
61
    }
62
}
63