1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Malef\Associate\DoctrineOrm\Metadata; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
6
|
|
|
|
7
|
|
|
class AssociationMetadataAdapter |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var MetadataAdapterProvider |
11
|
|
|
*/ |
12
|
|
|
protected $metadataAdapterProvider; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $associationMapping; |
18
|
|
|
|
19
|
|
|
public function __construct( |
20
|
|
|
MetadataAdapterProvider $metadataAdapterProvider, |
21
|
|
|
array $associationMapping |
22
|
|
|
) { |
23
|
|
|
$this->metadataAdapterProvider = $metadataAdapterProvider; |
24
|
|
|
$this->associationMapping = $associationMapping; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getName(): string |
28
|
|
|
{ |
29
|
|
|
return $this->associationMapping['fieldName']; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function isOwningSide(): bool |
33
|
|
|
{ |
34
|
|
|
return $this->associationMapping['isOwningSide']; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function isInverseSide(): bool |
38
|
|
|
{ |
39
|
|
|
return !$this->isOwningSide(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function isOneToOne(): bool |
43
|
|
|
{ |
44
|
|
|
return ClassMetadataInfo::ONE_TO_ONE === $this->associationMapping['type']; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function isOneToMany(): bool |
48
|
|
|
{ |
49
|
|
|
return ClassMetadataInfo::ONE_TO_MANY === $this->associationMapping['type']; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function isManyToOne(): bool |
53
|
|
|
{ |
54
|
|
|
return ClassMetadataInfo::MANY_TO_ONE === $this->associationMapping['type']; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function isManyToMany(): bool |
58
|
|
|
{ |
59
|
|
|
return ClassMetadataInfo::MANY_TO_MANY === $this->associationMapping['type']; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function isToMany(): bool |
63
|
|
|
{ |
64
|
|
|
return $this->isManyToMany() || $this->isOneToMany(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getSourceClassName(): string |
68
|
|
|
{ |
69
|
|
|
return $this->associationMapping['sourceEntity']; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @throws \Exception |
74
|
|
|
*/ |
75
|
|
|
public function getSourceClassMetadataAdapter(): ClassMetadataAdapter |
76
|
|
|
{ |
77
|
|
|
$sourceClassName = $this->metadataAdapterProvider->getClassMetadataAdapterByClassName($this->getSourceClassName()); |
78
|
|
|
if (!$sourceClassName instanceof ClassMetadataAdapter) { |
79
|
|
|
throw new \Exception('Source class name not determined.'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $sourceClassName; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getTargetClassName(): string |
86
|
|
|
{ |
87
|
|
|
return $this->associationMapping['targetEntity']; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @throws \Exception |
92
|
|
|
*/ |
93
|
|
|
public function getTargetClassMetadataAdapter(): ClassMetadataAdapter |
94
|
|
|
{ |
95
|
|
|
$targetClassName = $this->metadataAdapterProvider->getClassMetadataAdapterByClassName($this->getTargetClassName()); |
96
|
|
|
if (!$targetClassName instanceof ClassMetadataAdapter) { |
97
|
|
|
throw new \Exception('Target class name not determined.'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $targetClassName; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|