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: |
|
|
|
|
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
|
|
|
|
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.