-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPropertyHelperTrait.php
130 lines (109 loc) · 4.04 KB
/
PropertyHelperTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Doctrine\Common;
use Doctrine\Persistence\Mapping\ClassMetadata;
/**
* Helper trait for getting information regarding a property using the resource metadata.
*
* @author Kévin Dunglas <[email protected]>
* @author Théo FIDRY <[email protected]>
* @author Alan Poulain <[email protected]>
*/
trait PropertyHelperTrait
{
/**
* Gets class metadata for the given resource.
*/
abstract protected function getClassMetadata(string $resourceClass): ClassMetadata;
/**
* Determines whether the given property is mapped.
*/
protected function isPropertyMapped(string $property, string $resourceClass, bool $allowAssociation = false): bool
{
if ($this->isPropertyNested($property, $resourceClass)) {
$propertyParts = $this->splitPropertyParts($property, $resourceClass);
$metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']);
$property = $propertyParts['field'];
} else {
$metadata = $this->getClassMetadata($resourceClass);
}
return $metadata->hasField($property) || ($allowAssociation && $metadata->hasAssociation($property));
}
/**
* Determines whether the given property is nested.
*/
protected function isPropertyNested(string $property, string $resourceClass): bool
{
$pos = strpos($property, '.');
if (false === $pos) {
return false;
}
return $this->getClassMetadata($resourceClass)->hasAssociation(substr($property, 0, $pos));
}
/**
* Determines whether the given property is embedded.
*/
protected function isPropertyEmbedded(string $property, string $resourceClass): bool
{
return str_contains($property, '.') && $this->getClassMetadata($resourceClass)->hasField($property);
}
/**
* Splits the given property into parts.
*
* Returns an array with the following keys:
* - associations: array of associations according to nesting order
* - field: string holding the actual field (leaf node)
*/
protected function splitPropertyParts(string $property, string $resourceClass): array
{
$parts = explode('.', $property);
$metadata = $this->getClassMetadata($resourceClass);
$slice = 0;
foreach ($parts as $part) {
if ($metadata->hasAssociation($part)) {
$metadata = $this->getClassMetadata($metadata->getAssociationTargetClass($part));
++$slice;
}
}
if (\count($parts) === $slice) {
--$slice;
}
return [
'associations' => \array_slice($parts, 0, $slice),
'field' => implode('.', \array_slice($parts, $slice)),
];
}
/**
* Gets the Doctrine Type of a given property/resourceClass.
*/
protected function getDoctrineFieldType(string $property, string $resourceClass): ?string
{
$propertyParts = $this->splitPropertyParts($property, $resourceClass);
$metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']);
return $metadata->getTypeOfField($propertyParts['field']);
}
/**
* Gets nested class metadata for the given resource.
*
* @param string[] $associations
*/
protected function getNestedMetadata(string $resourceClass, array $associations): ClassMetadata
{
$metadata = $this->getClassMetadata($resourceClass);
foreach ($associations as $association) {
if ($metadata->hasAssociation($association)) {
$associationClass = $metadata->getAssociationTargetClass($association);
$metadata = $this->getClassMetadata($associationClass);
}
}
return $metadata;
}
}