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

api-platform / core / 13203240131

07 Feb 2025 03:47PM UTC coverage: 7.286%. Remained the same
13203240131

push

github

soyuka
docs: v4.0.17

12429 of 170587 relevant lines covered (7.29%)

11.98 hits per line

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

89.66
/src/Metadata/Property/Factory/ExtractorPropertyMetadataFactory.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\Extractor\PropertyExtractorInterface;
20
use Symfony\Component\PropertyInfo\Type;
21

22
/**
23
 * Creates properties's metadata using an extractor.
24
 *
25
 * @author Kévin Dunglas <dunglas@gmail.com>
26
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
27
 */
28
final class ExtractorPropertyMetadataFactory implements PropertyMetadataFactoryInterface
29
{
30
    public function __construct(private readonly PropertyExtractorInterface $extractor, private readonly ?PropertyMetadataFactoryInterface $decorated = null)
31
    {
32
    }
1,177✔
33

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

48
        if (
49
            !property_exists($resourceClass, $property) && !interface_exists($resourceClass)
337✔
50
            || null === ($propertyMetadata = $this->extractor->getProperties()[$resourceClass][$property] ?? null)
337✔
51
        ) {
52
            return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
337✔
53
        }
54

55
        if ($parentPropertyMetadata) {
13✔
56
            return $this->handleUserDefinedSchema($this->update($parentPropertyMetadata, $propertyMetadata));
12✔
57
        }
58

59
        $apiProperty = new ApiProperty();
4✔
60

61
        foreach ($propertyMetadata as $key => $value) {
4✔
62
            if ('builtinTypes' === $key && null !== $value) {
4✔
63
                $value = array_map(fn (string $builtinType): Type => new Type($builtinType), $value);
4✔
64
            }
65

66
            $methodName = 'with'.ucfirst($key);
4✔
67

68
            if (method_exists($apiProperty, $methodName) && null !== $value) {
4✔
69
                $apiProperty = $apiProperty->{$methodName}($value);
4✔
70
            }
71
        }
72

73
        return $this->handleUserDefinedSchema($apiProperty);
4✔
74
    }
75

76
    /**
77
     * Returns the metadata from the decorated factory if available or throws an exception.
78
     *
79
     * @throws PropertyNotFoundException
80
     */
81
    private function handleNotFound(?ApiProperty $parentPropertyMetadata, string $resourceClass, string $property): ApiProperty
82
    {
83
        if ($parentPropertyMetadata) {
337✔
84
            return $parentPropertyMetadata;
337✔
85
        }
86

87
        throw new PropertyNotFoundException(\sprintf('Property "%s" of the resource class "%s" not found.', $property, $resourceClass));
336✔
88
    }
89

90
    /**
91
     * Creates a new instance of metadata if the property is not already set.
92
     */
93
    private function update(ApiProperty $propertyMetadata, array $metadata): ApiProperty
94
    {
95
        foreach (get_class_methods(ApiProperty::class) as $method) {
12✔
96
            if (preg_match('/^(?:get|is)(.*)/', (string) $method, $matches) && null !== ($val = $metadata[lcfirst($matches[1])] ?? null) && method_exists($propertyMetadata, "with{$matches[1]}")) {
12✔
97
                $propertyMetadata = $propertyMetadata->{"with{$matches[1]}"}($val);
12✔
98
            }
99
        }
100

101
        return $propertyMetadata;
12✔
102
    }
103

104
    private function handleUserDefinedSchema(ApiProperty $propertyMetadata): ApiProperty
105
    {
106
        // can't know later if the schema has been defined by the user or by API Platform
107
        // store extra key to make this difference
108
        if (null !== $propertyMetadata->getSchema()) {
13✔
109
            $extraProperties = $propertyMetadata->getExtraProperties() ?? [];
×
110
            $propertyMetadata = $propertyMetadata->withExtraProperties([SchemaPropertyMetadataFactory::JSON_SCHEMA_USER_DEFINED => true] + $extraProperties);
×
111
        }
112

113
        return $propertyMetadata;
13✔
114
    }
115
}
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