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

api-platform / core / 9869461710

10 Jul 2024 06:43AM UTC coverage: 63.421%. Remained the same
9869461710

push

github

soyuka
cs(jsonld): non-nullable not needed

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

589 existing lines in 21 files now uncovered.

11178 of 17625 relevant lines covered (63.42%)

53.23 hits per line

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

43.64
/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\Api\ResourceClassResolverInterface as LegacyResourceClassResolverInterface;
17
use ApiPlatform\Metadata\Exception\RuntimeException;
18
use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation;
19
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
20
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
21
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
22
use ApiPlatform\Metadata\Util\CompositeIdentifierParser;
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
 * @author Antoine Bluchet <soyuka@gmail.com>
32
 */
33
final class IdentifiersExtractor implements IdentifiersExtractorInterface
34
{
35
    use ResourceClassInfoTrait;
36
    private readonly PropertyAccessorInterface $propertyAccessor;
37

38
    /**
39
     * @param LegacyResourceClassResolverInterface|ResourceClassResolverInterface $resourceClassResolver
40
     */
41
    public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, $resourceClassResolver, private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, ?PropertyAccessorInterface $propertyAccessor = null)
42
    {
43
        $this->resourceMetadataFactory = $resourceMetadataFactory;
271✔
44
        $this->resourceClassResolver = $resourceClassResolver;
271✔
45
        $this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
271✔
46
    }
47

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

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

UNCOV
63
        $resourceClass = $this->getResourceClass($item, true);
3✔
UNCOV
64
        $operation ??= $this->resourceMetadataFactory->create($resourceClass)->getOperation(null, false, true);
3✔
65

UNCOV
66
        return $this->getIdentifiersFromOperation($item, $operation, $context);
3✔
67
    }
68

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

77
        $identifiers = [];
167✔
78
        foreach ($links ?? [] as $k => $link) {
167✔
79
            if (1 < (is_countable($link->getIdentifiers()) ? \count($link->getIdentifiers()) : 0)) {
167✔
80
                $compositeIdentifiers = [];
×
81
                foreach ($link->getIdentifiers() 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();
167✔
90
            $identifiers[$parameterName] = $this->getIdentifierValue($item, $link->getFromClass() ?? $operation->getClass(), $link->getIdentifiers()[0] ?? $k, $parameterName, $link->getToProperty());
167✔
91
        }
92

93
        return $identifiers;
167✔
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) {
167✔
102
            try {
103
                return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, $property), $parameterName);
167✔
104
            } catch (NoSuchPropertyException $e) {
12✔
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) {
167✔
150
            throw new RuntimeException('No identifier value found, did you forget to persist the entity?');
12✔
151
        }
152

153
        if (\is_scalar($identifierValue)) {
167✔
154
            return $identifierValue;
167✔
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