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

api-platform / core / 15023181448

14 May 2025 02:19PM UTC coverage: 0.0% (-8.4%) from 8.418%
15023181448

Pull #7139

github

web-flow
Merge 9f45709da into 1862d03b7
Pull Request #7139: refactor(symfony): remove obsolete option `validator.query-parameter-validation`

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

11266 existing lines in 366 files now uncovered.

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

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

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

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

67
    private function getIdentifiersFromOperation(object $item, Operation $operation, array $context = []): array
68
    {
UNCOV
69
        if ($operation instanceof HttpOperation) {
×
UNCOV
70
            $links = $operation->getUriVariables();
×
71
        } elseif ($operation instanceof GraphQlOperation) {
×
72
            $links = $operation->getLinks();
×
73
        }
74

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

83
                $identifiers[$link->getParameterName()] = CompositeIdentifierParser::stringify($compositeIdentifiers);
×
84
                continue;
×
85
            }
86

UNCOV
87
            $parameterName = $link->getParameterName();
×
UNCOV
88
            $identifiers[$parameterName] = $this->getIdentifierValue($item, $link->getFromClass() ?? $operation->getClass(), $link->getIdentifiers()[0] ?? $k, $parameterName, $link->getToProperty());
×
89
        }
90

UNCOV
91
        return $identifiers;
×
92
    }
93

94
    /**
95
     * Gets the value of the given class property.
96
     */
97
    private function getIdentifierValue(object $item, string $class, string $property, string $parameterName, ?string $toProperty = null): float|bool|int|string
98
    {
UNCOV
99
        if ($item instanceof $class) {
×
100
            try {
UNCOV
101
                return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, $property), $parameterName);
×
UNCOV
102
            } catch (NoSuchPropertyException $e) {
×
103
                throw new RuntimeException('Not able to retrieve identifiers.', $e->getCode(), $e);
×
104
            }
105
        }
106

107
        if ($toProperty) {
×
108
            return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$toProperty.$property"), $parameterName);
×
109
        }
110

111
        $resourceClass = $this->getResourceClass($item, true);
×
112

113
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
×
114
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
×
115

116
            // TODO: remove in 5.x
117
            if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
118
                $types = $propertyMetadata->getBuiltinTypes();
×
119
                if (null === ($type = $types[0] ?? null)) {
×
120
                    continue;
×
121
                }
122

123
                try {
124
                    if ($type->isCollection()) {
×
125
                        $collectionValueType = $type->getCollectionValueTypes()[0] ?? null;
×
126

127
                        if (null !== $collectionValueType && $collectionValueType->getClassName() === $class) {
×
128
                            return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, \sprintf('%s[0].%s', $propertyName, $property)), $parameterName);
×
129
                        }
130
                    }
131

132
                    if ($type->getClassName() === $class) {
×
133
                        return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$propertyName.$property"), $parameterName);
×
134
                    }
135
                } catch (NoSuchPropertyException $e) {
×
136
                    throw new RuntimeException('Not able to retrieve identifiers.', $e->getCode(), $e);
×
137
                }
138
            }
139

140
            if (null === $type = $propertyMetadata->getNativeType()) {
×
141
                continue;
×
142
            }
143

144
            try {
145
                $collectionValueType = TypeHelper::getCollectionValueType($type);
×
146

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

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

159
        throw new RuntimeException('Not able to retrieve identifiers.');
×
160
    }
161

162
    /**
163
     * TODO: in 3.0 this method just uses $identifierValue instanceof \Stringable and we remove the weird behavior.
164
     *
165
     * @param mixed|\Stringable $identifierValue
166
     */
167
    private function resolveIdentifierValue(mixed $identifierValue, string $parameterName): float|bool|int|string
168
    {
UNCOV
169
        if (null === $identifierValue) {
×
UNCOV
170
            throw new RuntimeException('No identifier value found, did you forget to persist the entity?');
×
171
        }
172

UNCOV
173
        if (\is_scalar($identifierValue)) {
×
UNCOV
174
            return $identifierValue;
×
175
        }
176

177
        if ($identifierValue instanceof \Stringable) {
×
178
            return (string) $identifierValue;
×
179
        }
180

181
        if ($identifierValue instanceof \BackedEnum) {
×
182
            return (string) $identifierValue->value;
×
183
        }
184

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