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

api-platform / core / 16050929464

03 Jul 2025 12:51PM UTC coverage: 22.065% (+0.2%) from 21.821%
16050929464

push

github

soyuka
chore: todo improvement

11516 of 52192 relevant lines covered (22.06%)

22.08 hits per line

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

96.97
/src/Doctrine/Common/PropertyHelperTrait.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\Doctrine\Common;
15

16
use Doctrine\Persistence\Mapping\ClassMetadata;
17

18
/**
19
 * Helper trait for getting information regarding a property using the resource metadata.
20
 *
21
 * @author Kévin Dunglas <dunglas@gmail.com>
22
 * @author Théo FIDRY <theo.fidry@gmail.com>
23
 * @author Alan Poulain <contact@alanpoulain.eu>
24
 */
25
trait PropertyHelperTrait
26
{
27
    /**
28
     * Gets class metadata for the given resource.
29
     */
30
    abstract protected function getClassMetadata(string $resourceClass): ClassMetadata;
31

32
    /**
33
     * Determines whether the given property is mapped.
34
     */
35
    protected function isPropertyMapped(string $property, string $resourceClass, bool $allowAssociation = false): bool
36
    {
37
        if ($this->isPropertyNested($property, $resourceClass)) {
168✔
38
            $propertyParts = $this->splitPropertyParts($property, $resourceClass);
12✔
39
            $metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']);
12✔
40
            $property = $propertyParts['field'];
12✔
41
        } else {
42
            $metadata = $this->getClassMetadata($resourceClass);
168✔
43
        }
44

45
        return $metadata->hasField($property) || ($allowAssociation && $metadata->hasAssociation($property));
168✔
46
    }
47

48
    /**
49
     * Determines whether the given property is nested.
50
     */
51
    protected function isPropertyNested(string $property, string $resourceClass): bool
52
    {
53
        $pos = strpos($property, '.');
168✔
54
        if (false === $pos) {
168✔
55
            return false;
168✔
56
        }
57

58
        return $this->getClassMetadata($resourceClass)->hasAssociation(substr($property, 0, $pos));
12✔
59
    }
60

61
    /**
62
     * Determines whether the given property is embedded.
63
     */
64
    protected function isPropertyEmbedded(string $property, string $resourceClass): bool
65
    {
66
        return str_contains($property, '.') && $this->getClassMetadata($resourceClass)->hasField($property);
×
67
    }
68

69
    /**
70
     * Splits the given property into parts.
71
     *
72
     * Returns an array with the following keys:
73
     *   - associations: array of associations according to nesting order
74
     *   - field: string holding the actual field (leaf node)
75
     */
76
    protected function splitPropertyParts(string $property, string $resourceClass): array
77
    {
78
        $parts = explode('.', $property);
110✔
79

80
        $metadata = $this->getClassMetadata($resourceClass);
110✔
81
        $slice = 0;
110✔
82

83
        foreach ($parts as $part) {
110✔
84
            if ($metadata->hasAssociation($part)) {
110✔
85
                $metadata = $this->getClassMetadata($metadata->getAssociationTargetClass($part));
12✔
86
                ++$slice;
12✔
87
            }
88
        }
89

90
        if (\count($parts) === $slice) {
110✔
91
            --$slice;
12✔
92
        }
93

94
        return [
110✔
95
            'associations' => \array_slice($parts, 0, $slice),
110✔
96
            'field' => implode('.', \array_slice($parts, $slice)),
110✔
97
        ];
110✔
98
    }
99

100
    /**
101
     * Gets the Doctrine Type of a given property/resourceClass.
102
     */
103
    protected function getDoctrineFieldType(string $property, string $resourceClass): ?string
104
    {
105
        $propertyParts = $this->splitPropertyParts($property, $resourceClass);
92✔
106
        $metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']);
92✔
107

108
        return $metadata->getTypeOfField($propertyParts['field']);
92✔
109
    }
110

111
    /**
112
     * Gets nested class metadata for the given resource.
113
     *
114
     * @param string[] $associations
115
     */
116
    protected function getNestedMetadata(string $resourceClass, array $associations): ClassMetadata
117
    {
118
        $metadata = $this->getClassMetadata($resourceClass);
110✔
119

120
        foreach ($associations as $association) {
110✔
121
            if ($metadata->hasAssociation($association)) {
12✔
122
                $associationClass = $metadata->getAssociationTargetClass($association);
12✔
123

124
                $metadata = $this->getClassMetadata($associationClass);
12✔
125
            }
126
        }
127

128
        return $metadata;
110✔
129
    }
130
}
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