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 $properties = []; |
35
|
|
|
|
36
|
469 |
|
public function __construct(string $className, ClassMetadataBuildingContext $metadataBuildingContext) |
37
|
|
|
{ |
38
|
469 |
|
$reflectionService = $metadataBuildingContext->getReflectionService(); |
39
|
|
|
|
40
|
469 |
|
$this->reflectionClass = $reflectionService->getClass($className); |
41
|
469 |
|
$this->className = $this->reflectionClass ? $this->reflectionClass->getName() : $className; |
42
|
469 |
|
} |
43
|
|
|
|
44
|
2029 |
|
public function getClassName() : string |
45
|
|
|
{ |
46
|
2029 |
|
return $this->className; |
47
|
|
|
} |
48
|
|
|
|
49
|
98 |
|
public function setParent(ComponentMetadata $parent) : void |
50
|
|
|
{ |
51
|
98 |
|
$this->parent = $parent; |
52
|
98 |
|
} |
53
|
|
|
|
54
|
1217 |
|
public function getParent() : ?ComponentMetadata |
55
|
|
|
{ |
56
|
1217 |
|
return $this->parent; |
57
|
|
|
} |
58
|
|
|
|
59
|
543 |
|
public function getReflectionClass() : ?ReflectionClass |
60
|
|
|
{ |
61
|
543 |
|
return $this->reflectionClass; |
62
|
|
|
} |
63
|
|
|
|
64
|
25 |
|
public function setCache(?CacheMetadata $cache = null) : void |
65
|
|
|
{ |
66
|
25 |
|
$this->cache = $cache; |
67
|
25 |
|
} |
68
|
|
|
|
69
|
140 |
|
public function getCache() : ?CacheMetadata |
70
|
|
|
{ |
71
|
140 |
|
return $this->cache; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return iterable|Property[] |
76
|
|
|
*/ |
77
|
1487 |
|
public function getPropertiesIterator() : iterable |
78
|
|
|
{ |
79
|
1487 |
|
foreach ($this->properties as $name => $property) { |
80
|
1484 |
|
yield $name => $property; |
|
|
|
|
81
|
|
|
} |
82
|
1486 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @throws ReflectionException |
86
|
|
|
* @throws MappingException |
87
|
|
|
*/ |
88
|
428 |
|
public function addProperty(Property $property) : void |
89
|
|
|
{ |
90
|
428 |
|
$className = $this->getClassName(); |
91
|
428 |
|
$propertyName = $property->getName(); |
92
|
|
|
|
93
|
428 |
|
if ($this->hasProperty($propertyName)) { |
94
|
2 |
|
throw MappingException::duplicateProperty($className, $this->getProperty($propertyName)); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
428 |
|
$property->setDeclaringClass($this); |
98
|
|
|
|
99
|
428 |
|
if ($this->reflectionClass) { |
100
|
426 |
|
$reflectionProperty = new ReflectionProperty($className, $propertyName); |
101
|
|
|
|
102
|
426 |
|
$reflectionProperty->setAccessible(true); |
103
|
|
|
|
104
|
426 |
|
$property->setReflectionProperty($reflectionProperty); |
105
|
|
|
} |
106
|
|
|
|
107
|
428 |
|
$this->properties[$propertyName] = $property; |
108
|
428 |
|
} |
109
|
|
|
|
110
|
428 |
|
public function hasProperty(string $propertyName) : bool |
111
|
|
|
{ |
112
|
428 |
|
return isset($this->properties[$propertyName]); |
113
|
|
|
} |
114
|
|
|
|
115
|
1818 |
|
public function getProperty(string $propertyName) : ?Property |
116
|
|
|
{ |
117
|
1818 |
|
if (isset($this->properties[$propertyName])) { |
118
|
1811 |
|
return $this->properties[$propertyName]; |
119
|
|
|
} |
120
|
|
|
|
121
|
522 |
|
return null; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return ArrayIterator|ColumnMetadata[] |
126
|
|
|
*/ |
127
|
|
|
public function getColumnsIterator() : ArrayIterator |
128
|
|
|
{ |
129
|
|
|
$iterator = new ArrayIterator(); |
130
|
|
|
|
131
|
|
|
foreach ($this->getPropertiesIterator() as $property) { |
132
|
|
|
switch (true) { |
133
|
|
|
case $property instanceof FieldMetadata: |
134
|
|
|
$iterator->offsetSet($property->getColumnName(), $property); |
|
|
|
|
135
|
|
|
break; |
136
|
|
|
|
137
|
|
|
case $property instanceof ToOneAssociationMetadata && $property->isOwningSide(): |
138
|
|
|
foreach ($property->getJoinColumns() as $joinColumn) { |
|
|
|
|
139
|
|
|
/** @var JoinColumnMetadata $joinColumn */ |
140
|
|
|
$iterator->offsetSet($joinColumn->getColumnName(), $joinColumn); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
break; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $iterator; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|