Passed
Pull Request — master (#2144)
by Alan
04:51
created

PropertyHelper::getNestedMetadata()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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\Core\Bridge\Doctrine\ClassMetadata;
15
16
use Doctrine\Common\Persistence\ManagerRegistry;
17
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
18
use Doctrine\DBAL\Types\Type;
19
20
/**
21
 * Helper for getting information regarding a property using the resource metadata.
22
 *
23
 * @author Kévin Dunglas <[email protected]>
24
 * @author Théo FIDRY <[email protected]>
25
 * @author Alan Poulain <[email protected]>
26
 */
27
final class PropertyHelper
28
{
29
    private $managerRegistry;
30
31
    public function __construct(ManagerRegistry $managerRegistry)
32
    {
33
        $this->managerRegistry = $managerRegistry;
34
    }
35
36
    /**
37
     * Determines whether the given property is mapped.
38
     */
39
    public function isPropertyMapped(string $property, string $resourceClass, bool $allowAssociation = false): bool
40
    {
41
        if ($this->isPropertyNested($property, $resourceClass)) {
42
            $propertyParts = $this->splitPropertyParts($property, $resourceClass);
43
            $metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']);
44
            $property = $propertyParts['field'];
45
        } else {
46
            $metadata = $this->getClassMetadata($resourceClass);
47
        }
48
49
        return $metadata->hasField($property) || ($allowAssociation && $metadata->hasAssociation($property));
50
    }
51
52
    /**
53
     * Determines whether the given property is nested.
54
     */
55
    public function isPropertyNested(string $property, ?string $resourceClass): bool
56
    {
57
        $pos = strpos($property, '.');
58
        if (false === $pos) {
59
            return false;
60
        }
61
62
        return null !== $resourceClass && $this->getClassMetadata($resourceClass)->hasAssociation(substr($property, 0, $pos));
63
    }
64
65
    /**
66
     * Determines whether the given property is embedded.
67
     */
68
    public function isPropertyEmbedded(string $property, string $resourceClass): bool
69
    {
70
        return false !== strpos($property, '.') && $this->getClassMetadata($resourceClass)->hasField($property);
71
    }
72
73
    /**
74
     * Splits the given property into parts.
75
     *
76
     * Returns an array with the following keys:
77
     *   - associations: array of associations according to nesting order
78
     *   - field: string holding the actual field (leaf node)
79
     */
80
    public function splitPropertyParts(string $property, ?string $resourceClass): array
81
    {
82
        $parts = explode('.', $property);
83
84
        if (null === $resourceClass) {
85
            return [
86
                'associations' => \array_slice($parts, 0, -1),
87
                'field' => end($parts),
88
            ];
89
        }
90
91
        $metadata = $this->getClassMetadata($resourceClass);
92
        $slice = 0;
93
94
        foreach ($parts as $part) {
95
            if ($metadata->hasAssociation($part)) {
96
                $metadata = $this->getClassMetadata($metadata->getAssociationTargetClass($part));
97
                ++$slice;
98
            }
99
        }
100
101
        if (\count($parts) === $slice) {
102
            --$slice;
103
        }
104
105
        return [
106
            'associations' => \array_slice($parts, 0, $slice),
107
            'field' => implode('.', \array_slice($parts, $slice)),
108
        ];
109
    }
110
111
    /**
112
     * Gets the Doctrine Type of a given property/resourceClass.
113
     *
114
     * @return Type|string|null
115
     */
116
    public function getDoctrineFieldType(string $property, string $resourceClass)
117
    {
118
        $propertyParts = $this->splitPropertyParts($property, $resourceClass);
119
        $metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']);
120
121
        return $metadata->getTypeOfField($propertyParts['field']);
122
    }
123
124
    /**
125
     * Gets nested class metadata for the given resource.
126
     *
127
     * @param string[] $associations
128
     */
129
    public function getNestedMetadata(string $resourceClass, array $associations): ClassMetadata
130
    {
131
        $metadata = $this->getClassMetadata($resourceClass);
132
133
        foreach ($associations as $association) {
134
            if ($metadata->hasAssociation($association)) {
135
                $associationClass = $metadata->getAssociationTargetClass($association);
136
137
                $metadata = $this->getClassMetadata($associationClass);
138
            }
139
        }
140
141
        return $metadata;
142
    }
143
144
    /**
145
     * Gets class metadata for the given resource.
146
     */
147
    public function getClassMetadata(string $resourceClass): ClassMetadata
148
    {
149
        return $this
150
            ->managerRegistry
151
            ->getManagerForClass($resourceClass)
152
            ->getClassMetadata($resourceClass);
153
    }
154
}
155