• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 15255731762

26 May 2025 01:55PM UTC coverage: 0.0% (-26.5%) from 26.526%
15255731762

Pull #7176

github

web-flow
Merge 66f6cf4d2 into 79edced67
Pull Request #7176: Merge 4.1

0 of 387 new or added lines in 28 files covered. (0.0%)

11394 existing lines in 372 files now uncovered.

0 of 51334 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaChoiceRestriction.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Symfony\Validator\Metadata\Property\Restriction;
15

16
use ApiPlatform\Metadata\ApiProperty;
17
use ApiPlatform\Metadata\Util\TypeHelper;
18
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
19
use Symfony\Component\PropertyInfo\Type as LegacyType;
20
use Symfony\Component\TypeInfo\Type;
21
use Symfony\Component\TypeInfo\Type\CollectionType;
22
use Symfony\Component\TypeInfo\TypeIdentifier;
23
use Symfony\Component\Validator\Constraint;
24
use Symfony\Component\Validator\Constraints\Choice;
25

26
/**
27
 * @author Tomas Norkūnas <norkunas.tom@gmail.com>
28
 */
29
final class PropertySchemaChoiceRestriction implements PropertySchemaRestrictionMetadataInterface
30
{
31
    /**
32
     * {@inheritdoc}
33
     *
34
     * @param Choice $constraint
35
     */
36
    public function create(Constraint $constraint, ApiProperty $propertyMetadata): array
37
    {
38
        $choices = [];
×
39

40
        if (\is_callable($constraint->callback)) {
×
41
            $choices = ($constraint->callback)();
×
42
        } elseif (\is_array($constraint->choices)) {
×
43
            $choices = $constraint->choices;
×
44
        }
45

46
        if (!$choices) {
×
47
            return [];
×
48
        }
49

50
        $restriction = [];
×
51

52
        if (!$constraint->multiple) {
×
53
            $restriction['enum'] = $choices;
×
54

55
            return $restriction;
×
56
        }
57

58
        $restriction['type'] = 'array';
×
59

60
        $types = array_values(array_unique(array_map(fn (mixed $choice) => \is_string($choice) ? 'string' : 'number', $choices)));
×
61

62
        if ($count = \count($types)) {
×
63
            if (1 === $count) {
×
64
                $types = $types[0];
×
65
            }
66

67
            $restriction['items'] = ['type' => $types, 'enum' => $choices];
×
68
        }
69

70
        if (null !== $constraint->min) {
×
71
            $restriction['minItems'] = $constraint->min;
×
72
        }
73

74
        if (null !== $constraint->max) {
×
75
            $restriction['maxItems'] = $constraint->max;
×
76
        }
77

78
        return $restriction;
×
79
    }
80

81
    public function supports(Constraint $constraint, ApiProperty $propertyMetadata): bool
82
    {
UNCOV
83
        if (!$constraint instanceof Choice) {
×
UNCOV
84
            return false;
×
85
        }
86

87
        if (method_exists(PropertyInfoExtractor::class, 'getType')) {
×
88
            $nativeType = $propertyMetadata->getExtraProperties()['nested_schema'] ?? false
×
89
                ? Type::string()
×
90
                : $propertyMetadata->getNativeType();
×
91

92
            $isValidScalarType = fn (Type $t): bool => $t->isSatisfiedBy(
×
93
                fn (Type $subType): bool => $subType->isIdentifiedBy(TypeIdentifier::STRING, TypeIdentifier::INT, TypeIdentifier::FLOAT)
×
94
            );
×
95

96
            if ($isValidScalarType($nativeType)) {
×
97
                return true;
×
98
            }
99

100
            if ($nativeType->isSatisfiedBy(fn ($t) => $t instanceof CollectionType)) {
×
101
                if (null !== ($collectionValueType = TypeHelper::getCollectionValueType($nativeType)) && $isValidScalarType($collectionValueType)) {
×
102
                    return true;
×
103
                }
104
            }
105

106
            return false;
×
107
        }
108

109
        $types = array_map(static fn (LegacyType $type) => $type->getBuiltinType(), $propertyMetadata->getBuiltinTypes() ?? []);
×
110
        if ($propertyMetadata->getExtraProperties()['nested_schema'] ?? false) {
×
111
            $types = [LegacyType::BUILTIN_TYPE_STRING];
×
112
        }
113

114
        if (
115
            null !== ($builtinType = ($propertyMetadata->getBuiltinTypes()[0] ?? null))
×
116
            && $builtinType->isCollection()
×
117
            && \count($builtinType->getCollectionValueTypes()) > 0
×
118
        ) {
119
            $types = array_unique(array_merge($types, array_map(static fn (LegacyType $type) => $type->getBuiltinType(), $builtinType->getCollectionValueTypes())));
×
120
        }
121

122
        return \count($types) > 0 && \count(array_intersect($types, [LegacyType::BUILTIN_TYPE_STRING, LegacyType::BUILTIN_TYPE_INT, LegacyType::BUILTIN_TYPE_FLOAT])) > 0;
×
123
    }
124
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc