1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Doctrine\ORM\Mapping; |
||
6 | |||
7 | use ArrayIterator; |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
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 | 450 | public function __construct(string $className) |
|
37 | { |
||
38 | 450 | $this->className = $className; |
|
39 | 450 | } |
|
40 | |||
41 | 2021 | public function getClassName() : string |
|
42 | { |
||
43 | 2021 | return $this->className; |
|
44 | } |
||
45 | |||
46 | 99 | public function setParent(ComponentMetadata $parent) : void |
|
47 | { |
||
48 | 99 | $this->parent = $parent; |
|
49 | 99 | } |
|
50 | |||
51 | 1218 | public function getParent() : ?ComponentMetadata |
|
52 | { |
||
53 | 1218 | return $this->parent; |
|
54 | } |
||
55 | |||
56 | 1959 | public function wakeupReflection(ReflectionService $reflectionService) : void |
|
57 | { |
||
58 | // Restore ReflectionClass and properties |
||
59 | 1959 | $this->reflectionClass = $reflectionService->getClass($this->className); |
|
60 | |||
61 | 1959 | if (! $this->reflectionClass) { |
|
62 | return; |
||
63 | } |
||
64 | |||
65 | 1959 | $this->className = $this->reflectionClass->getName(); |
|
66 | |||
67 | 1959 | foreach ($this->properties as $property) { |
|
68 | /** @var Property $property */ |
||
69 | 1956 | $property->wakeupReflection($reflectionService); |
|
70 | } |
||
71 | 1959 | } |
|
72 | |||
73 | 541 | public function getReflectionClass() : ?ReflectionClass |
|
74 | { |
||
75 | 541 | 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 | 1488 | public function getPropertiesIterator() : iterable |
|
92 | { |
||
93 | 1488 | foreach ($this->properties as $name => $property) { |
|
94 | 1487 | yield $name => $property; |
|
95 | } |
||
96 | 1487 | } |
|
97 | |||
98 | /** |
||
99 | * @throws ReflectionException |
||
100 | * @throws MappingException |
||
101 | */ |
||
102 | 417 | public function addProperty(Property $property) : void |
|
103 | { |
||
104 | 417 | $className = $this->getClassName(); |
|
105 | 417 | $propertyName = $property->getName(); |
|
106 | |||
107 | 417 | if ($this->hasProperty($propertyName)) { |
|
108 | 2 | throw MappingException::duplicateProperty($className, $this->getProperty($propertyName)); |
|
109 | } |
||
110 | |||
111 | 417 | $property->setDeclaringClass($this); |
|
112 | |||
113 | 417 | $this->properties[$propertyName] = $property; |
|
114 | 417 | } |
|
115 | |||
116 | 417 | public function hasProperty(string $propertyName) : bool |
|
117 | { |
||
118 | 417 | return isset($this->properties[$propertyName]); |
|
119 | } |
||
120 | |||
121 | 1811 | public function getProperty(string $propertyName) : ?Property |
|
122 | { |
||
123 | 1811 | return $this->properties[$propertyName] ?? null; |
|
124 | } |
||
125 | } |
||
126 |