1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ORM\Mapping; |
6
|
|
|
|
7
|
|
|
use ArrayIterator; |
8
|
|
|
use ReflectionClass; |
9
|
|
|
use ReflectionException; |
10
|
|
|
use ReflectionProperty; |
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 $declaredProperties = []; |
35
|
|
|
|
36
|
462 |
|
public function __construct(string $className, ClassMetadataBuildingContext $metadataBuildingContext) |
37
|
|
|
{ |
38
|
462 |
|
$reflectionService = $metadataBuildingContext->getReflectionService(); |
39
|
|
|
|
40
|
462 |
|
$this->reflectionClass = $reflectionService->getClass($className); |
41
|
462 |
|
$this->className = $this->reflectionClass ? $this->reflectionClass->getName() : $className; |
42
|
462 |
|
} |
43
|
|
|
|
44
|
2021 |
|
public function getClassName() : string |
45
|
|
|
{ |
46
|
2021 |
|
return $this->className; |
47
|
|
|
} |
48
|
|
|
|
49
|
97 |
|
public function setParent(ComponentMetadata $parent) : void |
50
|
|
|
{ |
51
|
97 |
|
$this->parent = $parent; |
52
|
97 |
|
} |
53
|
|
|
|
54
|
1213 |
|
public function getParent() : ?ComponentMetadata |
55
|
|
|
{ |
56
|
1213 |
|
return $this->parent; |
57
|
|
|
} |
58
|
|
|
|
59
|
563 |
|
public function getReflectionClass() : ?ReflectionClass |
60
|
|
|
{ |
61
|
563 |
|
return $this->reflectionClass; |
62
|
|
|
} |
63
|
|
|
|
64
|
25 |
|
public function setCache(?CacheMetadata $cache = null) : void |
65
|
|
|
{ |
66
|
25 |
|
$this->cache = $cache; |
67
|
25 |
|
} |
68
|
|
|
|
69
|
233 |
|
public function getCache() : ?CacheMetadata |
70
|
|
|
{ |
71
|
233 |
|
return $this->cache; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return iterable|Property[] |
76
|
|
|
*/ |
77
|
1480 |
|
public function getDeclaredPropertiesIterator() : iterable |
78
|
|
|
{ |
79
|
1480 |
|
foreach ($this->declaredProperties as $name => $property) { |
80
|
1478 |
|
yield $name => $property; |
|
|
|
|
81
|
|
|
} |
82
|
1479 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @throws ReflectionException |
86
|
|
|
* @throws MappingException |
87
|
|
|
*/ |
88
|
424 |
|
public function addDeclaredProperty(Property $property) : void |
89
|
|
|
{ |
90
|
424 |
|
$className = $this->getClassName(); |
91
|
424 |
|
$propertyName = $property->getName(); |
92
|
|
|
|
93
|
|
|
// @todo guilhermeblanco Switch to hasProperty once inherited properties are not being mapped on child classes |
94
|
424 |
|
if ($this->hasDeclaredProperty($propertyName)) { |
95
|
2 |
|
throw MappingException::duplicateProperty($className, $this->getProperty($propertyName)); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
424 |
|
$property->setDeclaringClass($this); |
99
|
|
|
|
100
|
424 |
|
if ($this->reflectionClass) { |
101
|
422 |
|
$reflectionProperty = new ReflectionProperty($className, $propertyName); |
102
|
|
|
|
103
|
422 |
|
$reflectionProperty->setAccessible(true); |
104
|
|
|
|
105
|
422 |
|
$property->setReflectionProperty($reflectionProperty); |
106
|
|
|
} |
107
|
|
|
|
108
|
424 |
|
$this->declaredProperties[$propertyName] = $property; |
109
|
424 |
|
} |
110
|
|
|
|
111
|
424 |
|
public function hasDeclaredProperty(string $propertyName) : bool |
112
|
|
|
{ |
113
|
424 |
|
return isset($this->declaredProperties[$propertyName]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return iterable|Property[] |
118
|
|
|
*/ |
119
|
|
|
public function getPropertiesIterator() : iterable |
120
|
|
|
{ |
121
|
|
|
if ($this->parent) { |
122
|
|
|
yield from $this->parent->getPropertiesIterator(); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
yield from $this->getDeclaredPropertiesIterator(); |
126
|
|
|
} |
127
|
|
|
|
128
|
1808 |
|
public function getProperty(string $propertyName) : ?Property |
129
|
|
|
{ |
130
|
1808 |
|
if (isset($this->declaredProperties[$propertyName])) { |
131
|
1801 |
|
return $this->declaredProperties[$propertyName]; |
132
|
|
|
} |
133
|
|
|
|
134
|
520 |
|
if ($this->parent) { |
135
|
81 |
|
return $this->parent->getProperty($propertyName); |
136
|
|
|
} |
137
|
|
|
|
138
|
520 |
|
return null; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function hasProperty(string $propertyName) : bool |
142
|
|
|
{ |
143
|
|
|
if (isset($this->declaredProperties[$propertyName])) { |
144
|
|
|
return true; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $this->parent && $this->parent->hasProperty($propertyName); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return ArrayIterator|ColumnMetadata[] |
152
|
|
|
*/ |
153
|
4 |
|
public function getColumnsIterator() : ArrayIterator |
154
|
|
|
{ |
155
|
4 |
|
$iterator = new ArrayIterator(); |
156
|
|
|
|
157
|
|
|
// @todo guilhermeblanco Must be switched to getPropertiesIterator once class only has its declared properties |
158
|
4 |
|
foreach ($this->getDeclaredPropertiesIterator() as $property) { |
159
|
|
|
switch (true) { |
160
|
4 |
|
case $property instanceof FieldMetadata: |
161
|
4 |
|
$iterator->offsetSet($property->getColumnName(), $property); |
|
|
|
|
162
|
4 |
|
break; |
163
|
|
|
|
164
|
2 |
|
case $property instanceof ToOneAssociationMetadata && $property->isOwningSide(): |
165
|
2 |
|
foreach ($property->getJoinColumns() as $joinColumn) { |
|
|
|
|
166
|
|
|
/** @var JoinColumnMetadata $joinColumn */ |
167
|
2 |
|
$iterator->offsetSet($joinColumn->getColumnName(), $joinColumn); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
4 |
|
break; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
4 |
|
return $iterator; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|