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