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

api-platform / core / 20847864477

09 Jan 2026 09:47AM UTC coverage: 29.1% (+0.005%) from 29.095%
20847864477

Pull #7649

github

web-flow
Merge b342dd5db into d640d106b
Pull Request #7649: feat(validator): uuid/ulid parameter validation

0 of 4 new or added lines in 1 file covered. (0.0%)

15050 existing lines in 491 files now uncovered.

16996 of 58406 relevant lines covered (29.1%)

81.8 hits per line

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

71.83
/src/Metadata/IdentifiersExtractor.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\Metadata;
15

16
use ApiPlatform\Metadata\Exception\RuntimeException;
17
use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation;
18
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
19
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Metadata\Util\CompositeIdentifierParser;
22
use ApiPlatform\Metadata\Util\ResourceClassInfoTrait;
23
use ApiPlatform\Metadata\Util\TypeHelper;
24
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
25
use Symfony\Component\PropertyAccess\PropertyAccess;
26
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
27
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
28

29
/**
30
 * {@inheritdoc}
31
 *
32
 * @author Antoine Bluchet <soyuka@gmail.com>
33
 */
34
final class IdentifiersExtractor implements IdentifiersExtractorInterface
35
{
36
    use ResourceClassInfoTrait;
37
    private readonly PropertyAccessorInterface $propertyAccessor;
38

39
    public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, ?PropertyAccessorInterface $propertyAccessor = null)
40
    {
UNCOV
41
        $this->resourceMetadataFactory = $resourceMetadataFactory;
2,398✔
UNCOV
42
        $this->resourceClassResolver = $resourceClassResolver;
2,398✔
UNCOV
43
        $this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
2,398✔
44
    }
45

46
    /**
47
     * {@inheritdoc}
48
     *
49
     * TODO: 3.0 identifiers should be stringable?
50
     */
51
    public function getIdentifiersFromItem(object $item, ?Operation $operation = null, array $context = []): array
52
    {
UNCOV
53
        if (!$this->isResourceClass($this->getObjectClass($item))) {
2,017✔
54
            try {
UNCOV
55
                return ['id' => $this->propertyAccessor->getValue($item, 'id')];
25✔
56
            } catch (NoSuchPropertyException $e) {
2✔
57
                throw new RuntimeException('Not able to retrieve identifiers.', $e->getCode(), $e);
2✔
58
            }
59
        }
60

UNCOV
61
        if ($operation && $operation->getClass()) {
1,998✔
UNCOV
62
            return $this->getIdentifiersFromOperation($item, $operation, $context);
1,998✔
63
        }
64

UNCOV
65
        $resourceClass = $this->getResourceClass($item, true);
220✔
UNCOV
66
        $operation ??= $this->resourceMetadataFactory->create($resourceClass)->getOperation(null, false, true);
220✔
67

UNCOV
68
        return $this->getIdentifiersFromOperation($item, $operation, $context);
220✔
69
    }
70

71
    /**
72
     * @param array<string, mixed> $context
73
     */
74
    private function getIdentifiersFromOperation(object $item, Operation $operation, array $context = []): array
75
    {
UNCOV
76
        if ($operation instanceof HttpOperation) {
1,998✔
UNCOV
77
            $links = $operation->getUriVariables();
1,998✔
78
        } elseif ($operation instanceof GraphQlOperation) {
×
79
            $links = $operation->getLinks();
×
80
        }
81

UNCOV
82
        $identifiers = [];
1,998✔
UNCOV
83
        foreach ($links ?? [] as $k => $link) {
1,998✔
UNCOV
84
            if (1 < (is_countable($link->getIdentifiers()) ? \count($link->getIdentifiers()) : 0)) {
1,935✔
85
                $compositeIdentifiers = [];
16✔
86
                foreach ($link->getIdentifiers() as $identifier) {
16✔
87
                    $compositeIdentifiers[$identifier] = $this->getIdentifierValue($item, $link->getFromClass() ?? $operation->getClass(), $identifier, $link->getParameterName(), null, $context, $operation);
16✔
88
                }
89

90
                $identifiers[$link->getParameterName()] = CompositeIdentifierParser::stringify($compositeIdentifiers);
16✔
91
                continue;
16✔
92
            }
93

UNCOV
94
            $parameterName = $link->getParameterName();
1,934✔
UNCOV
95
            $identifiers[$parameterName] = $this->getIdentifierValue($item, $link->getFromClass() ?? $operation->getClass(), $link->getIdentifiers()[0] ?? $k, $parameterName, $link->getToProperty(), $context, $operation);
1,934✔
96
        }
97

UNCOV
98
        return $identifiers;
1,996✔
99
    }
100

101
    /**
102
     * Gets the value of the given class property.
103
     *
104
     * @param array<string, mixed> $context
105
     */
106
    private function getIdentifierValue(object $item, string $class, string $property, string $parameterName, ?string $toProperty, array $context, Operation $operation): float|bool|int|string
107
    {
UNCOV
108
        if ($item instanceof $class) {
1,935✔
109
            try {
UNCOV
110
                return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, $property), $parameterName);
1,933✔
UNCOV
111
            } catch (NoSuchPropertyException $e) {
166✔
112
                throw new RuntimeException('Not able to retrieve identifiers.', $e->getCode(), $e);
×
113
            }
114
        }
115

116
        // ItemUriTemplate is defined on a collection and we read the identifier alghough the PHP class may be different
UNCOV
117
        if (isset($context['item_uri_template']) && $operation->getClass() === $class) {
50✔
118
            try {
UNCOV
119
                return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, $property), $parameterName);
2✔
120
            } catch (NoSuchPropertyException $e) {
×
121
                throw new RuntimeException(\sprintf('Could not retrieve identifier "%s" for class "%s" using itemUriTemplate "%s". Check that the property exists and is accessible.', $property, $class, $context['item_uri_template']), $e->getCode(), $e);
×
122
            }
123
        }
124

UNCOV
125
        if ($toProperty) {
48✔
UNCOV
126
            return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$toProperty.$property"), $parameterName);
25✔
127
        }
128

UNCOV
129
        $resourceClass = $this->getResourceClass($item, true);
28✔
130

UNCOV
131
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
28✔
UNCOV
132
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
28✔
133

134
            // TODO: remove in 5.x
UNCOV
135
            if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
28✔
136
                $types = $propertyMetadata->getBuiltinTypes();
×
137
                if (null === ($type = $types[0] ?? null)) {
×
138
                    continue;
×
139
                }
140

141
                try {
142
                    if ($type->isCollection()) {
×
143
                        $collectionValueType = $type->getCollectionValueTypes()[0] ?? null;
×
144

145
                        if (null !== $collectionValueType && $collectionValueType->getClassName() === $class) {
×
146
                            return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, \sprintf('%s[0].%s', $propertyName, $property)), $parameterName);
×
147
                        }
148
                    }
149

150
                    if ($type->getClassName() === $class) {
×
151
                        return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$propertyName.$property"), $parameterName);
×
152
                    }
153
                } catch (NoSuchPropertyException $e) {
×
154
                    throw new RuntimeException('Not able to retrieve identifiers.', $e->getCode(), $e);
×
155
                }
156
            }
157

UNCOV
158
            if (null === $type = $propertyMetadata->getNativeType()) {
28✔
159
                continue;
×
160
            }
161

162
            try {
UNCOV
163
                $collectionValueType = TypeHelper::getCollectionValueType($type);
28✔
164

UNCOV
165
                if ($collectionValueType?->isIdentifiedBy($class)) {
28✔
166
                    return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, \sprintf('%s[0].%s', $propertyName, $property)), $parameterName);
4✔
167
                }
168

UNCOV
169
                if ($type->isIdentifiedBy($class)) {
28✔
UNCOV
170
                    return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$propertyName.$property"), $parameterName);
28✔
171
                }
172
            } catch (NoSuchPropertyException $e) {
2✔
173
                throw new RuntimeException('Not able to retrieve identifiers.', $e->getCode(), $e);
2✔
174
            }
175
        }
176

UNCOV
177
        throw new RuntimeException('Not able to retrieve identifiers.');
8✔
178
    }
179

180
    /**
181
     * TODO: in 3.0 this method just uses $identifierValue instanceof \Stringable and we remove the weird behavior.
182
     *
183
     * @param mixed|\Stringable $identifierValue
184
     */
185
    private function resolveIdentifierValue(mixed $identifierValue, string $parameterName): float|bool|int|string
186
    {
UNCOV
187
        if (null === $identifierValue) {
1,935✔
UNCOV
188
            throw new RuntimeException('No identifier value found, did you forget to persist the entity?');
166✔
189
        }
190

UNCOV
191
        if (\is_scalar($identifierValue)) {
1,920✔
UNCOV
192
            return $identifierValue;
1,873✔
193
        }
194

UNCOV
195
        if ($identifierValue instanceof \Stringable) {
73✔
UNCOV
196
            return (string) $identifierValue;
73✔
197
        }
198

199
        if ($identifierValue instanceof \BackedEnum) {
×
200
            return (string) $identifierValue->value;
×
201
        }
202

203
        throw new RuntimeException(\sprintf('We were not able to resolve the identifier matching parameter "%s".', $parameterName));
×
204
    }
205
}
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

© 2026 Coveralls, Inc