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

PropertySchemaLengthRestriction::create()   B

Complexity

Conditions 8
Paths 13

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 8
eloc 16
c 1
b 0
f 1
nc 13
nop 2
dl 0
loc 30
rs 8.4444
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\PropertyInfo\Type;
18
use Symfony\Component\Validator\Constraint;
19
use Symfony\Component\Validator\Constraints\Length;
20
21
/**
22
 * Class PropertySchemaLengthRestrictions.
23
 *
24
 * @author Andrii Penchuk [email protected]
25
 */
26
class PropertySchemaLengthRestriction implements PropertySchemaRestrictionMetadataInterface
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function create(Constraint $constraint, PropertyMetadata $propertyMetadata): array
32
    {
33
        $restriction = [];
34
35
        switch ($propertyMetadata->getType()->getBuiltinType()) {
36
            case Type::BUILTIN_TYPE_STRING:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
37
38
                if (isset($constraint->min)) {
39
                    $restriction['minLength'] = (int) $constraint->min;
40
                }
41
42
                if (isset($constraint->max)) {
43
                    $restriction['maxLength'] = (int) $constraint->max;
44
                }
45
46
                break;
47
            case Type::BUILTIN_TYPE_INT:
48
            case Type::BUILTIN_TYPE_FLOAT:
49
                if (isset($constraint->min)) {
50
                    $restriction['minimum'] = (int) $constraint->min;
51
                }
52
53
                if (isset($constraint->max)) {
54
                    $restriction['maximum'] = (int) $constraint->max;
55
                }
56
57
                break;
58
        }
59
60
        return $restriction;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function supports(Constraint $constraint, PropertyMetadata $propertyMetadata): bool
67
    {
68
        return $constraint instanceof Length && null !== $propertyMetadata->getType();
69
    }
70
}
71