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

api-platform / core / 10944992322

19 Sep 2024 04:21PM UTC coverage: 7.678% (+0.2%) from 7.518%
10944992322

push

github

soyuka
doc: changelog 4.0.1

12681 of 165161 relevant lines covered (7.68%)

16.3 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)) {
537✔
38
            $propertyParts = $this->splitPropertyParts($property, $resourceClass);
476✔
39
            $metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']);
476✔
40
            $property = $propertyParts['field'];
476✔
41
        } else {
42
            $metadata = $this->getClassMetadata($resourceClass);
529✔
43
        }
44

45
        return $metadata->hasField($property) || ($allowAssociation && $metadata->hasAssociation($property));
537✔
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, '.');
541✔
54
        if (false === $pos) {
541✔
55
            return false;
532✔
56
        }
57

58
        return $this->getClassMetadata($resourceClass)->hasAssociation(substr($property, 0, $pos));
479✔
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);
519✔
79

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

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

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

94
        return [
519✔
95
            'associations' => \array_slice($parts, 0, $slice),
519✔
96
            'field' => implode('.', \array_slice($parts, $slice)),
519✔
97
        ];
519✔
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);
513✔
106
        $metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']);
513✔
107

108
        return $metadata->getTypeOfField($propertyParts['field']);
513✔
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);
518✔
119

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

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

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