1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ORM\Mapping; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A <tt>ComponentMetadata</tt> instance holds object-relational property mapping. |
9
|
|
|
*/ |
10
|
|
|
abstract class ComponentMetadata |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $className; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var ComponentMetadata|null |
19
|
|
|
*/ |
20
|
|
|
protected $parent; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The ReflectionClass instance of the component class. |
24
|
|
|
* |
25
|
|
|
* @var \ReflectionClass|null |
26
|
|
|
*/ |
27
|
|
|
protected $reflectionClass; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var CacheMetadata|null |
31
|
|
|
*/ |
32
|
|
|
protected $cache; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Property[] |
36
|
|
|
*/ |
37
|
|
|
protected $declaredProperties = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* ComponentMetadata constructor. |
41
|
|
|
*/ |
42
|
470 |
|
public function __construct(string $className, ClassMetadataBuildingContext $metadataBuildingContext) |
43
|
|
|
{ |
44
|
470 |
|
$reflectionService = $metadataBuildingContext->getReflectionService(); |
45
|
|
|
|
46
|
470 |
|
$this->reflectionClass = $reflectionService->getClass($className); |
47
|
470 |
|
$this->className = $this->reflectionClass ? $this->reflectionClass->getName() : $className; |
48
|
470 |
|
} |
49
|
|
|
|
50
|
2020 |
|
public function getClassName() : string |
51
|
|
|
{ |
52
|
2020 |
|
return $this->className; |
53
|
|
|
} |
54
|
|
|
|
55
|
101 |
|
public function setParent(ComponentMetadata $parent) : void |
56
|
|
|
{ |
57
|
101 |
|
$this->parent = $parent; |
58
|
101 |
|
} |
59
|
|
|
|
60
|
1208 |
|
public function getParent() : ?ComponentMetadata |
61
|
|
|
{ |
62
|
1208 |
|
return $this->parent; |
63
|
|
|
} |
64
|
|
|
|
65
|
561 |
|
public function getReflectionClass() : ?\ReflectionClass |
66
|
|
|
{ |
67
|
561 |
|
return $this->reflectionClass; |
68
|
|
|
} |
69
|
|
|
|
70
|
24 |
|
public function setCache(?CacheMetadata $cache = null) : void |
71
|
|
|
{ |
72
|
24 |
|
$this->cache = $cache; |
73
|
24 |
|
} |
74
|
|
|
|
75
|
235 |
|
public function getCache() : ?CacheMetadata |
76
|
|
|
{ |
77
|
235 |
|
return $this->cache; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return iterable|Property[] |
82
|
|
|
*/ |
83
|
1485 |
|
public function getDeclaredPropertiesIterator() : iterable |
84
|
|
|
{ |
85
|
1485 |
|
foreach ($this->declaredProperties as $name => $property) { |
86
|
1481 |
|
yield $name => $property; |
87
|
|
|
} |
88
|
1484 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @throws \ReflectionException |
92
|
|
|
* @throws MappingException |
93
|
|
|
*/ |
94
|
419 |
|
public function addDeclaredProperty(Property $property) : void |
95
|
|
|
{ |
96
|
419 |
|
$className = $this->getClassName(); |
97
|
419 |
|
$propertyName = $property->getName(); |
98
|
|
|
|
99
|
|
|
// @todo guilhermeblanco Switch to hasProperty once inherited properties are not being mapped on child classes |
100
|
419 |
|
if ($this->hasDeclaredProperty($propertyName)) { |
101
|
2 |
|
throw MappingException::duplicateProperty($className, $this->getProperty($propertyName)); |
102
|
|
|
} |
103
|
|
|
|
104
|
419 |
|
$property->setDeclaringClass($this); |
105
|
|
|
|
106
|
419 |
|
if ($this->reflectionClass) { |
107
|
417 |
|
$reflectionProperty = new \ReflectionProperty($className, $propertyName); |
108
|
|
|
|
109
|
417 |
|
$reflectionProperty->setAccessible(true); |
110
|
|
|
|
111
|
417 |
|
$property->setReflectionProperty($reflectionProperty); |
112
|
|
|
} |
113
|
|
|
|
114
|
419 |
|
$this->declaredProperties[$propertyName] = $property; |
115
|
419 |
|
} |
116
|
|
|
|
117
|
419 |
|
public function hasDeclaredProperty(string $propertyName) : bool |
118
|
|
|
{ |
119
|
419 |
|
return isset($this->declaredProperties[$propertyName]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return iterable|Property[] |
124
|
|
|
*/ |
125
|
|
|
public function getPropertiesIterator() : iterable |
126
|
|
|
{ |
127
|
|
|
if ($this->parent) { |
128
|
|
|
yield from $this->parent->getPropertiesIterator(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
yield from $this->getDeclaredPropertiesIterator(); |
132
|
|
|
} |
133
|
|
|
|
134
|
1798 |
|
public function getProperty(string $propertyName) : ?Property |
135
|
|
|
{ |
136
|
1798 |
|
if (isset($this->declaredProperties[$propertyName])) { |
137
|
1791 |
|
return $this->declaredProperties[$propertyName]; |
138
|
|
|
} |
139
|
|
|
|
140
|
525 |
|
if ($this->parent) { |
141
|
82 |
|
return $this->parent->getProperty($propertyName); |
142
|
|
|
} |
143
|
|
|
|
144
|
525 |
|
return null; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function hasProperty(string $propertyName) : bool |
148
|
|
|
{ |
149
|
|
|
if (isset($this->declaredProperties[$propertyName])) { |
150
|
|
|
return true; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $this->parent && $this->parent->hasProperty($propertyName); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return \ArrayIterator|ColumnMetadata[] |
158
|
|
|
*/ |
159
|
|
|
public function getColumnsIterator() : \ArrayIterator |
160
|
|
|
{ |
161
|
|
|
$iterator = new \ArrayIterator(); |
162
|
|
|
|
163
|
|
|
// @todo guilhermeblanco Must be switched to getPropertiesIterator once class only has its declared properties |
164
|
|
|
foreach ($this->getDeclaredPropertiesIterator() as $property) { |
165
|
|
|
switch (true) { |
166
|
|
|
case ($property instanceof FieldMetadata): |
167
|
|
|
$iterator->offsetSet($property->getColumnName(), $property); |
|
|
|
|
168
|
|
|
break; |
169
|
|
|
|
170
|
|
|
case ($property instanceof ToOneAssociationMetadata && $property->isOwningSide()): |
171
|
|
|
foreach ($property->getJoinColumns() as $joinColumn) { |
|
|
|
|
172
|
|
|
/** @var JoinColumnMetadata $joinColumn */ |
173
|
|
|
$iterator->offsetSet($joinColumn->getColumnName(), $joinColumn); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
break; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $iterator; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|