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

api-platform / core / 6418833342

05 Oct 2023 12:11PM UTC coverage: 37.157% (+0.4%) from 36.79%
6418833342

push

github

web-flow
chore: move namespace Api Metadata (#5857)

57 of 57 new or added lines in 8 files covered. (100.0%)

10191 of 27427 relevant lines covered (37.16%)

17.94 hits per line

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

0.0
/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\Metadata\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\Metadata\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
 * @deprecated use ApiPlatform\Metadata\IdentifiersExtractor instead
32
 *
33
 * @author Antoine Bluchet <soyuka@gmail.com>
34
 */
35
final class IdentifiersExtractor implements IdentifiersExtractorInterface
36
{
37
    use ResourceClassInfoTrait;
38
    private readonly PropertyAccessorInterface $propertyAccessor;
39

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

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

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

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

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

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

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

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

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

93
        return $identifiers;
×
94
    }
95

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

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

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

117
            $types = $propertyMetadata->getBuiltinTypes();
×
118
            if (null === ($type = $types[0] ?? null)) {
×
119
                continue;
×
120
            }
121

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

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

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

139
        throw new RuntimeException('Not able to retrieve identifiers.');
×
140
    }
141

142
    /**
143
     * TODO: in 3.0 this method just uses $identifierValue instanceof \Stringable and we remove the weird behavior.
144
     *
145
     * @param mixed|\Stringable $identifierValue
146
     */
147
    private function resolveIdentifierValue(mixed $identifierValue, string $parameterName): float|bool|int|string
148
    {
149
        if (null === $identifierValue) {
×
150
            throw new RuntimeException('No identifier value found, did you forget to persist the entity?');
×
151
        }
152

153
        if (\is_scalar($identifierValue)) {
×
154
            return $identifierValue;
×
155
        }
156

157
        if ($identifierValue instanceof \Stringable) {
×
158
            return (string) $identifierValue;
×
159
        }
160

161
        if ($identifierValue instanceof \BackedEnum) {
×
162
            return (string) $identifierValue->value;
×
163
        }
164

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