1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ORM\Mapping; |
6
|
|
|
|
7
|
|
|
use ArrayIterator; |
8
|
|
|
use Doctrine\ORM\Reflection\ReflectionService; |
9
|
|
|
use ReflectionClass; |
10
|
|
|
use ReflectionException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A <tt>ComponentMetadata</tt> instance holds object-relational property mapping. |
14
|
|
|
*/ |
15
|
|
|
abstract class ComponentMetadata |
16
|
|
|
{ |
17
|
|
|
/** @var string */ |
18
|
|
|
protected $className; |
19
|
|
|
|
20
|
|
|
/** @var ComponentMetadata|null */ |
21
|
|
|
protected $parent; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The ReflectionClass instance of the component class. |
25
|
|
|
* |
26
|
|
|
* @var ReflectionClass|null |
27
|
|
|
*/ |
28
|
|
|
protected $reflectionClass; |
29
|
|
|
|
30
|
|
|
/** @var CacheMetadata|null */ |
31
|
|
|
protected $cache; |
32
|
|
|
|
33
|
|
|
/** @var Property[] */ |
34
|
|
|
protected $properties = []; |
35
|
|
|
|
36
|
451 |
|
public function __construct(string $className) |
37
|
|
|
{ |
38
|
451 |
|
$this->className = $className; |
39
|
451 |
|
} |
40
|
|
|
|
41
|
2016 |
|
public function getClassName() : string |
42
|
|
|
{ |
43
|
2016 |
|
return $this->className; |
44
|
|
|
} |
45
|
|
|
|
46
|
98 |
|
public function setParent(ComponentMetadata $parent) : void |
47
|
|
|
{ |
48
|
98 |
|
$this->parent = $parent; |
49
|
98 |
|
} |
50
|
|
|
|
51
|
1217 |
|
public function getParent() : ?ComponentMetadata |
52
|
|
|
{ |
53
|
1217 |
|
return $this->parent; |
54
|
|
|
} |
55
|
|
|
|
56
|
1954 |
|
public function wakeupReflection(ReflectionService $reflectionService) : void |
57
|
|
|
{ |
58
|
|
|
// Restore ReflectionClass and properties |
59
|
1954 |
|
$this->reflectionClass = $reflectionService->getClass($this->className); |
60
|
|
|
|
61
|
1954 |
|
if (! $this->reflectionClass) { |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
1954 |
|
$this->className = $this->reflectionClass->getName(); |
66
|
|
|
|
67
|
1954 |
|
foreach ($this->properties as $property) { |
68
|
|
|
/** @var Property $property */ |
69
|
1951 |
|
$property->wakeupReflection($reflectionService); |
70
|
|
|
} |
71
|
1954 |
|
} |
72
|
|
|
|
73
|
540 |
|
public function getReflectionClass() : ?ReflectionClass |
74
|
|
|
{ |
75
|
540 |
|
return $this->reflectionClass; |
76
|
|
|
} |
77
|
|
|
|
78
|
25 |
|
public function setCache(?CacheMetadata $cache = null) : void |
79
|
|
|
{ |
80
|
25 |
|
$this->cache = $cache; |
81
|
25 |
|
} |
82
|
|
|
|
83
|
181 |
|
public function getCache() : ?CacheMetadata |
84
|
|
|
{ |
85
|
181 |
|
return $this->cache; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return iterable|Property[] |
90
|
|
|
*/ |
91
|
1485 |
|
public function getPropertiesIterator() : iterable |
92
|
|
|
{ |
93
|
1485 |
|
foreach ($this->properties as $name => $property) { |
94
|
1484 |
|
yield $name => $property; |
|
|
|
|
95
|
|
|
} |
96
|
1484 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @throws ReflectionException |
100
|
|
|
* @throws MappingException |
101
|
|
|
*/ |
102
|
416 |
|
public function addProperty(Property $property) : void |
103
|
|
|
{ |
104
|
416 |
|
$className = $this->getClassName(); |
105
|
416 |
|
$propertyName = $property->getName(); |
106
|
|
|
|
107
|
416 |
|
if ($this->hasProperty($propertyName)) { |
108
|
2 |
|
throw MappingException::duplicateProperty($className, $this->getProperty($propertyName)); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
416 |
|
$property->setDeclaringClass($this); |
112
|
|
|
|
113
|
416 |
|
$this->properties[$propertyName] = $property; |
114
|
416 |
|
} |
115
|
|
|
|
116
|
416 |
|
public function hasProperty(string $propertyName) : bool |
117
|
|
|
{ |
118
|
416 |
|
return isset($this->properties[$propertyName]); |
119
|
|
|
} |
120
|
|
|
|
121
|
1808 |
|
public function getProperty(string $propertyName) : ?Property |
122
|
|
|
{ |
123
|
1808 |
|
if (isset($this->properties[$propertyName])) { |
124
|
1801 |
|
return $this->properties[$propertyName]; |
125
|
|
|
} |
126
|
|
|
|
127
|
522 |
|
return null; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return ArrayIterator|ColumnMetadata[] |
132
|
|
|
*/ |
133
|
|
|
public function getColumnsIterator() : ArrayIterator |
134
|
|
|
{ |
135
|
|
|
$iterator = new ArrayIterator(); |
136
|
|
|
|
137
|
|
|
foreach ($this->getPropertiesIterator() as $property) { |
138
|
|
|
switch (true) { |
139
|
|
|
case $property instanceof FieldMetadata: |
140
|
|
|
$iterator->offsetSet($property->getColumnName(), $property); |
|
|
|
|
141
|
|
|
break; |
142
|
|
|
|
143
|
|
|
case $property instanceof ToOneAssociationMetadata && $property->isOwningSide(): |
144
|
|
|
foreach ($property->getJoinColumns() as $joinColumn) { |
|
|
|
|
145
|
|
|
/** @var JoinColumnMetadata $joinColumn */ |
146
|
|
|
$iterator->offsetSet($joinColumn->getColumnName(), $joinColumn); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
break; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $iterator; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|