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

api-platform / core / 19190417533

08 Nov 2025 08:16AM UTC coverage: 0.0%. Remained the same
19190417533

push

github

soyuka
Merge 4.2

0 of 18 new or added lines in 6 files covered. (0.0%)

2 existing lines in 1 file now uncovered.

0 of 56768 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/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
    {
41
        $this->resourceMetadataFactory = $resourceMetadataFactory;
×
42
        $this->resourceClassResolver = $resourceClassResolver;
×
43
        $this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
×
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
    {
53
        if (!$this->isResourceClass($this->getObjectClass($item))) {
×
54
            return ['id' => $this->propertyAccessor->getValue($item, 'id')];
×
55
        }
56

57
        if ($operation && $operation->getClass()) {
×
58
            return $this->getIdentifiersFromOperation($item, $operation, $context);
×
59
        }
60

61
        $resourceClass = $this->getResourceClass($item, true);
×
62
        $operation ??= $this->resourceMetadataFactory->create($resourceClass)->getOperation(null, false, true);
×
63

64
        return $this->getIdentifiersFromOperation($item, $operation, $context);
×
65
    }
66

67
    /**
68
     * @param array<string, mixed> $context
69
     */
70
    private function getIdentifiersFromOperation(object $item, Operation $operation, array $context = []): array
71
    {
72
        if ($operation instanceof HttpOperation) {
×
73
            $links = $operation->getUriVariables();
×
74
        } elseif ($operation instanceof GraphQlOperation) {
×
75
            $links = $operation->getLinks();
×
76
        }
77

78
        $identifiers = [];
×
79
        foreach ($links ?? [] as $k => $link) {
×
80
            if (1 < (is_countable($link->getIdentifiers()) ? \count($link->getIdentifiers()) : 0)) {
×
81
                $compositeIdentifiers = [];
×
82
                foreach ($link->getIdentifiers() as $identifier) {
×
NEW
83
                    $compositeIdentifiers[$identifier] = $this->getIdentifierValue($item, $link->getFromClass() ?? $operation->getClass(), $identifier, $link->getParameterName(), null, $context, $operation);
×
84
                }
85

86
                $identifiers[$link->getParameterName()] = CompositeIdentifierParser::stringify($compositeIdentifiers);
×
87
                continue;
×
88
            }
89

90
            $parameterName = $link->getParameterName();
×
NEW
91
            $identifiers[$parameterName] = $this->getIdentifierValue($item, $link->getFromClass() ?? $operation->getClass(), $link->getIdentifiers()[0] ?? $k, $parameterName, $link->getToProperty(), $context, $operation);
×
92
        }
93

94
        return $identifiers;
×
95
    }
96

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

112
        // ItemUriTemplate is defined on a collection and we read the identifier alghough the PHP class may be different
NEW
113
        if (isset($context['item_uri_template']) && $operation->getClass() === $class) {
×
114
            try {
NEW
115
                return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, $property), $parameterName);
×
NEW
116
            } catch (NoSuchPropertyException $e) {
×
NEW
117
                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);
×
118
            }
119
        }
120

121
        if ($toProperty) {
×
122
            return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$toProperty.$property"), $parameterName);
×
123
        }
124

125
        $resourceClass = $this->getResourceClass($item, true);
×
126

127
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
×
128
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
×
129

130
            // TODO: remove in 5.x
131
            if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
132
                $types = $propertyMetadata->getBuiltinTypes();
×
133
                if (null === ($type = $types[0] ?? null)) {
×
134
                    continue;
×
135
                }
136

137
                try {
138
                    if ($type->isCollection()) {
×
139
                        $collectionValueType = $type->getCollectionValueTypes()[0] ?? null;
×
140

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

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

154
            if (null === $type = $propertyMetadata->getNativeType()) {
×
155
                continue;
×
156
            }
157

158
            try {
159
                $collectionValueType = TypeHelper::getCollectionValueType($type);
×
160

161
                if ($collectionValueType?->isIdentifiedBy($class)) {
×
162
                    return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, \sprintf('%s[0].%s', $propertyName, $property)), $parameterName);
×
163
                }
164

165
                if ($type->isIdentifiedBy($class)) {
×
166
                    return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$propertyName.$property"), $parameterName);
×
167
                }
168
            } catch (NoSuchPropertyException $e) {
×
169
                throw new RuntimeException('Not able to retrieve identifiers.', $e->getCode(), $e);
×
170
            }
171
        }
172

173
        throw new RuntimeException('Not able to retrieve identifiers.');
×
174
    }
175

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

187
        if (\is_scalar($identifierValue)) {
×
188
            return $identifierValue;
×
189
        }
190

191
        if ($identifierValue instanceof \Stringable) {
×
192
            return (string) $identifierValue;
×
193
        }
194

195
        if ($identifierValue instanceof \BackedEnum) {
×
196
            return (string) $identifierValue->value;
×
197
        }
198

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