1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xsolve\Associate\Metadata; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
6
|
|
|
|
7
|
|
|
class AssociationMetadataWrapper |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var MetadataWrapperProvider |
11
|
|
|
*/ |
12
|
|
|
protected $metadataWrapperProvider; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $associationMapping; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param MetadataWrapperProvider $metadataWrapperProvider |
21
|
|
|
* @param array $associationMapping |
22
|
|
|
*/ |
23
|
|
|
public function __construct( |
24
|
|
|
MetadataWrapperProvider $metadataWrapperProvider, |
25
|
|
|
array $associationMapping |
26
|
|
|
) { |
27
|
|
|
$this->metadataWrapperProvider = $metadataWrapperProvider; |
28
|
|
|
$this->associationMapping = $associationMapping; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function getName(): string |
35
|
|
|
{ |
36
|
|
|
return $this->associationMapping['fieldName']; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
public function isOwningSide(): bool |
43
|
|
|
{ |
44
|
|
|
return $this->associationMapping['isOwningSide']; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
public function isInverseSide(): bool |
51
|
|
|
{ |
52
|
|
|
return !$this->isOwningSide(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
public function isOneToOne(): bool |
59
|
|
|
{ |
60
|
|
|
return ClassMetadataInfo::ONE_TO_ONE === $this->associationMapping['type']; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
public function isOneToMany(): bool |
67
|
|
|
{ |
68
|
|
|
return ClassMetadataInfo::ONE_TO_MANY === $this->associationMapping['type']; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function isManyToOne(): bool |
75
|
|
|
{ |
76
|
|
|
return ClassMetadataInfo::MANY_TO_ONE === $this->associationMapping['type']; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public function isManyToMany(): bool |
83
|
|
|
{ |
84
|
|
|
return ClassMetadataInfo::MANY_TO_MANY === $this->associationMapping['type']; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getSourceClassName(): string |
91
|
|
|
{ |
92
|
|
|
return $this->associationMapping['sourceEntity']; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return ClassMetadataWrapper |
97
|
|
|
* |
98
|
|
|
* @throws \Exception |
99
|
|
|
*/ |
100
|
|
|
public function getSourceClassMetadataWrapper(): ClassMetadataWrapper |
101
|
|
|
{ |
102
|
|
|
$sourceClassName = $this->metadataWrapperProvider->getClassMetadataWrapperByClassName($this->getSourceClassName()); |
103
|
|
|
if (!$sourceClassName instanceof ClassMetadataWrapper) { |
104
|
|
|
throw new \Exception('Source class name not determined.'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $sourceClassName; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function getTargetClassName(): string |
114
|
|
|
{ |
115
|
|
|
return $this->associationMapping['targetEntity']; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return ClassMetadataWrapper |
120
|
|
|
* |
121
|
|
|
* @throws \Exception |
122
|
|
|
*/ |
123
|
|
|
public function getTargetClassMetadataWrapper(): ClassMetadataWrapper |
124
|
|
|
{ |
125
|
|
|
$targetClassName = $this->metadataWrapperProvider->getClassMetadataWrapperByClassName($this->getTargetClassName()); |
126
|
|
|
if (!$targetClassName instanceof ClassMetadataWrapper) { |
127
|
|
|
throw new \Exception('Target class name not determined.'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $targetClassName; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|