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

api-platform / core / 3713134090

pending completion
3713134090

Pull #5254

github

GitHub
Merge b2ec54b3c into ac711530f
Pull Request #5254: [OpenApi] Add ApiResource::openapi and deprecate openapiContext

197 of 197 new or added lines in 5 files covered. (100.0%)

7493 of 12362 relevant lines covered (60.61%)

67.56 hits per line

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

68.63
/src/Api/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\Api;
15

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

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

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

45
    /**
46
     * {@inheritdoc}
47
     *
48
     * TODO: 3.0 identifiers should be stringable?
49
     */
50
    public function getIdentifiersFromItem(object $item, Operation $operation = null, array $context = []): array
51
    {
52
        if (!$this->isResourceClass($this->getObjectClass($item))) {
467✔
53
            return ['id' => $this->propertyAccessor->getValue($item, 'id')];
3✔
54
        }
55

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

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

63
        return $this->getIdentifiersFromOperation($item, $operation, $context);
84✔
64
    }
65

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

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

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

86
            $parameterName = $link->getParameterName();
461✔
87
            $identifiers[$parameterName] = $this->getIdentifierValue($item, $link->getFromClass(), $link->getIdentifiers()[0], $parameterName);
461✔
88
        }
89

90
        return $identifiers;
464✔
91
    }
92

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

106
        $resourceClass = $this->getResourceClass($item, true);
7✔
107
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
7✔
108
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
7✔
109

110
            $types = $propertyMetadata->getBuiltinTypes();
7✔
111
            if (null === ($type = $types[0] ?? null)) {
7✔
112
                continue;
×
113
            }
114

115
            try {
116
                if ($type->isCollection()) {
7✔
117
                    $collectionValueType = $type->getCollectionValueTypes()[0] ?? null;
5✔
118

119
                    if (null !== $collectionValueType && $collectionValueType->getClassName() === $class) {
5✔
120
                        return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, sprintf('%s[0].%s', $propertyName, $property)), $parameterName);
1✔
121
                    }
122
                }
123

124
                if ($type->getClassName() === $class) {
7✔
125
                    return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$propertyName.$property"), $parameterName);
7✔
126
                }
127
            } catch (NoSuchPropertyException $e) {
×
128
                throw new RuntimeException('Not able to retrieve identifiers.', $e->getCode(), $e);
×
129
            }
130
        }
131

132
        throw new RuntimeException('Not able to retrieve identifiers.');
3✔
133
    }
134

135
    /**
136
     * TODO: in 3.0 this method just uses $identifierValue instanceof \Stringable and we remove the weird behavior.
137
     *
138
     * @param mixed|\Stringable $identifierValue
139
     */
140
    private function resolveIdentifierValue(mixed $identifierValue, string $parameterName): float|bool|int|string
141
    {
142
        if (null === $identifierValue) {
460✔
143
            throw new RuntimeException('No identifier value found, did you forget to persist the entity?');
×
144
        }
145

146
        if (\is_scalar($identifierValue)) {
460✔
147
            return $identifierValue;
460✔
148
        }
149

150
        if ($identifierValue instanceof \Stringable) {
×
151
            return (string) $identifierValue;
×
152
        }
153

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