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

api-platform / core / 10729306835

05 Sep 2024 10:46PM UTC coverage: 7.655% (-0.01%) from 7.665%
10729306835

push

github

web-flow
Merge pull request #6586 from soyuka/merge-342

Merge 3.4

0 of 54 new or added lines in 12 files covered. (0.0%)

8760 existing lines in 277 files now uncovered.

12505 of 163357 relevant lines covered (7.66%)

22.84 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
    {
UNCOV
37
        if ($this->isPropertyNested($property, $resourceClass)) {
791✔
UNCOV
38
            $propertyParts = $this->splitPropertyParts($property, $resourceClass);
701✔
UNCOV
39
            $metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']);
701✔
UNCOV
40
            $property = $propertyParts['field'];
701✔
41
        } else {
UNCOV
42
            $metadata = $this->getClassMetadata($resourceClass);
778✔
43
        }
44

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

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

UNCOV
58
        return $this->getClassMetadata($resourceClass)->hasAssociation(substr($property, 0, $pos));
706✔
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
    {
UNCOV
78
        $parts = explode('.', $property);
765✔
79

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

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

UNCOV
90
        if (\count($parts) === $slice) {
765✔
UNCOV
91
            --$slice;
662✔
92
        }
93

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

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

UNCOV
108
        return $metadata->getTypeOfField($propertyParts['field']);
755✔
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
    {
UNCOV
118
        $metadata = $this->getClassMetadata($resourceClass);
763✔
119

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

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

UNCOV
128
        return $metadata;
763✔
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