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

api-platform / core / 15283185369

27 May 2025 06:43PM UTC coverage: 0.0% (-26.4%) from 26.397%
15283185369

Pull #7180

github

web-flow
Merge 9a6703d44 into c022b441b
Pull Request #7180: feat(deps): allow Elasticsearch v9

0 of 51334 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/Property/Factory/AttributePropertyMetadataFactory.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\Property\Factory;
15

16
use ApiPlatform\JsonSchema\Metadata\Property\Factory\SchemaPropertyMetadataFactory;
17
use ApiPlatform\Metadata\ApiProperty;
18
use ApiPlatform\Metadata\Exception\PropertyNotFoundException;
19
use ApiPlatform\Metadata\Util\Reflection;
20
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
21

22
/**
23
 * Creates a property metadata from {@see ApiProperty} attribute.
24
 *
25
 * @author Antoine Bluchet <soyuka@gmail.com>
26
 */
27
final class AttributePropertyMetadataFactory implements PropertyMetadataFactoryInterface
28
{
29
    public function __construct(private readonly ?PropertyMetadataFactoryInterface $decorated = null)
30
    {
31
    }
×
32

33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function create(string $resourceClass, string $property, array $options = []): ApiProperty
37
    {
38
        $parentPropertyMetadata = null;
×
39
        if ($this->decorated) {
×
40
            try {
41
                $parentPropertyMetadata = $this->decorated->create($resourceClass, $property, $options);
×
42
            } catch (PropertyNotFoundException) {
×
43
                // Ignore not found exception from decorated factories
44
            }
45
        }
46

47
        $reflectionClass = null;
×
48
        $reflectionEnum = null;
×
49

50
        try {
51
            $reflectionClass = new \ReflectionClass($resourceClass);
×
52
        } catch (\ReflectionException) {
×
53
        }
54
        try {
55
            $reflectionEnum = new \ReflectionEnum($resourceClass);
×
56
        } catch (\ReflectionException) {
×
57
        }
58

59
        if (!$reflectionClass && !$reflectionEnum) {
×
60
            return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
×
61
        }
62

63
        if ($reflectionEnum && $reflectionEnum->hasCase($property)) {
×
64
            $reflectionCase = $reflectionEnum->getCase($property);
×
65
            if ($attributes = $reflectionCase->getAttributes(ApiProperty::class)) {
×
66
                return $this->createMetadata($attributes[0]->newInstance(), $parentPropertyMetadata);
×
67
            }
68
        }
69

70
        if ($reflectionClass->hasProperty($property)) {
×
71
            $reflectionProperty = $reflectionClass->getProperty($property);
×
72
            if ($attributes = $reflectionProperty->getAttributes(ApiProperty::class)) {
×
73
                return $this->createMetadata($attributes[0]->newInstance(), $parentPropertyMetadata);
×
74
            }
75
        }
76

77
        foreach (array_merge(Reflection::ACCESSOR_PREFIXES, Reflection::MUTATOR_PREFIXES) as $prefix) {
×
78
            $methodName = $prefix.ucfirst($property);
×
79
            if (!$reflectionClass->hasMethod($methodName) && !$reflectionEnum?->hasMethod($methodName)) {
×
80
                continue;
×
81
            }
82

83
            $reflectionMethod = $reflectionClass->hasMethod($methodName) ? $reflectionClass->getMethod($methodName) : $reflectionEnum?->getMethod($methodName);
×
84
            if (!$reflectionMethod->isPublic()) {
×
85
                continue;
×
86
            }
87

88
            if ($attributes = $reflectionMethod->getAttributes(ApiProperty::class)) {
×
89
                return $this->createMetadata($attributes[0]->newInstance(), $parentPropertyMetadata);
×
90
            }
91
        }
92

93
        $attributes = $reflectionClass->getAttributes(ApiProperty::class);
×
94
        foreach ($attributes as $attribute) {
×
95
            $instance = $attribute->newInstance();
×
96
            if ($instance->getProperty() === $property) {
×
97
                return $this->createMetadata($instance, $parentPropertyMetadata);
×
98
            }
99
        }
100

101
        return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
×
102
    }
103

104
    /**
105
     * Returns the metadata from the decorated factory if available or throws an exception.
106
     *
107
     * @throws PropertyNotFoundException
108
     */
109
    private function handleNotFound(?ApiProperty $parentPropertyMetadata, string $resourceClass, string $property): ApiProperty
110
    {
111
        if (null !== $parentPropertyMetadata) {
×
112
            return $parentPropertyMetadata;
×
113
        }
114

115
        throw new PropertyNotFoundException(\sprintf('Property "%s" of class "%s" not found.', $property, $resourceClass));
×
116
    }
117

118
    private function createMetadata(ApiProperty $attribute, ?ApiProperty $propertyMetadata = null): ApiProperty
119
    {
120
        if (null === $propertyMetadata) {
×
121
            return $this->handleUserDefinedSchema($attribute);
×
122
        }
123

124
        foreach (get_class_methods(ApiProperty::class) as $method) {
×
125
            if (preg_match('/^(?:get|is)(.*)/', (string) $method, $matches)) {
×
126
                // BC layer, to remove in 5.0
127
                if ('getBuiltinTypes' === $method) {
×
128
                    if (method_exists(PropertyInfoExtractor::class, 'getType')) {
×
129
                        continue;
×
130
                    }
131

132
                    if ($builtinTypes = $attribute->getBuiltinTypes()) {
×
133
                        $propertyMetadata = $propertyMetadata->withBuiltinTypes($builtinTypes);
×
134
                    }
135

136
                    continue;
×
137
                }
138

139
                if (null !== $val = $attribute->{$method}()) {
×
140
                    $propertyMetadata = $propertyMetadata->{"with{$matches[1]}"}($val);
×
141
                }
142
            }
143
        }
144

145
        return $this->handleUserDefinedSchema($propertyMetadata);
×
146
    }
147

148
    private function handleUserDefinedSchema(ApiProperty $propertyMetadata): ApiProperty
149
    {
150
        // can't know later if the schema has been defined by the user or by API Platform
151
        // store extra key to make this difference
152
        if (null !== $propertyMetadata->getSchema()) {
×
153
            $extraProperties = $propertyMetadata->getExtraProperties() ?? [];
×
154
            $propertyMetadata = $propertyMetadata->withExtraProperties([SchemaPropertyMetadataFactory::JSON_SCHEMA_USER_DEFINED => true] + $extraProperties);
×
155
        }
156

157
        return $propertyMetadata;
×
158
    }
159
}
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

© 2026 Coveralls, Inc