|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Analogue\ORM\System\Wrappers; |
|
4
|
|
|
|
|
5
|
|
|
use Analogue\ORM\System\InternallyMappable; |
|
6
|
|
|
use Analogue\ORM\System\Proxies\ProxyFactory; |
|
7
|
|
|
use Analogue\ORM\System\Proxies\CollectionProxy; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* The Wrapper Class provides a single interface access several Entity types |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class Wrapper implements InternallyMappable |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Original Entity Object |
|
16
|
|
|
* |
|
17
|
|
|
* @var mixed |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $entity; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Corresponding EntityMap |
|
23
|
|
|
* |
|
24
|
|
|
* @var \Analogue\ORM\EntityMap |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $entityMap; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var \Analogue\ORM\System\Proxirs\ProxyFactory |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $proxyFactory; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Wrapper constructor. |
|
35
|
|
|
* @param $entity |
|
36
|
|
|
* @param $entityMap |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct($entity, $entityMap) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->entity = $entity; |
|
41
|
|
|
$this->entityMap = $entityMap; |
|
42
|
|
|
$this->proxyFactory = new ProxyFactory; |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Return the wrapped entity class |
|
47
|
|
|
* |
|
48
|
|
|
* @return mixed |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getEntityClass() |
|
51
|
|
|
{ |
|
52
|
|
|
return get_class($this->entity); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Returns the wrapped entity |
|
57
|
|
|
* |
|
58
|
|
|
* @return mixed |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getObject() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->entity; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Returns the wrapped entity's map |
|
67
|
|
|
* |
|
68
|
|
|
* @return mixed |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getMap() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->entityMap; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Set the lazyloading proxies on the wrapped entity objet |
|
77
|
|
|
* |
|
78
|
|
|
* @param array $relations list of relations to be lazy loaded |
|
79
|
|
|
* |
|
80
|
|
|
* @return void |
|
81
|
|
|
*/ |
|
82
|
|
|
public function setProxies(array $relations = null) |
|
83
|
|
|
{ |
|
84
|
|
|
$attributes = $this->getEntityAttributes(); |
|
85
|
|
|
$proxies = []; |
|
86
|
|
|
|
|
87
|
|
|
if(is_null($relations)) { |
|
88
|
|
|
$relations = $this->getRelationsToProxy(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
foreach ($relations as $relation) { |
|
92
|
|
|
// We first look if we need to build the proxy on the relationship. |
|
93
|
|
|
// If the key is handled locally and we know it not to be set, |
|
94
|
|
|
// we'll set the relationship to null |
|
95
|
|
|
if (! $this->relationNeedsProxy($relation, $attributes)) { |
|
96
|
|
|
$proxies[$relation] = null; |
|
97
|
|
|
} |
|
98
|
|
|
else { |
|
99
|
|
|
$targetClass = $this->entityMap->getTargettedClass($relation); |
|
100
|
|
|
$proxies[$relation] = $this->proxyFactory->make($this->getObject(), $relation, $targetClass); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
foreach ($proxies as $key => $value) { |
|
105
|
|
|
$this->setEntityAttribute($key, $value); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Determine which relations we have to build proxy for, by parsing |
|
111
|
|
|
* attributes and finding methods that aren't set. |
|
112
|
|
|
* |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function getRelationsToProxy() |
|
116
|
|
|
{ |
|
117
|
|
|
$proxies = []; |
|
118
|
|
|
$attributes = $this->getEntityAttributes(); |
|
119
|
|
|
|
|
120
|
|
|
foreach ($this->entityMap->getRelationships() as $relation) { |
|
121
|
|
|
if (!array_key_exists($relation, $attributes)) { |
|
122
|
|
|
$proxies[] = $relation; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $proxies; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Determine if the relation needs a proxy or not |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $relation |
|
133
|
|
|
* @param array $attributes |
|
134
|
|
|
* @return boolean |
|
135
|
|
|
*/ |
|
136
|
|
|
protected function relationNeedsProxy($relation, $attributes) |
|
137
|
|
|
{ |
|
138
|
|
|
if(in_array($relation, $this->entityMap->getRelationshipsWithoutProxy())) { |
|
139
|
|
|
return false; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$localKey = $this->entityMap->getLocalKeys($relation); |
|
143
|
|
|
|
|
144
|
|
|
if(is_null($localKey)) return true; |
|
145
|
|
|
|
|
146
|
|
|
if(is_array($localKey)) { |
|
147
|
|
|
$localKey = $localKey['id']; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
if(! isset($attributes[$localKey])) { |
|
151
|
|
|
return false; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
if(is_null($attributes[$localKey])) { |
|
155
|
|
|
return false; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return true; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @param string $key |
|
163
|
|
|
* @param string $value |
|
164
|
|
|
* @return mixed |
|
165
|
|
|
*/ |
|
166
|
|
|
abstract public function setEntityAttribute($key, $value); |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param string $key |
|
170
|
|
|
* @return mixed |
|
171
|
|
|
*/ |
|
172
|
|
|
abstract public function getEntityAttribute($key); |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @param array $attributes |
|
176
|
|
|
* @return mixed |
|
177
|
|
|
*/ |
|
178
|
|
|
abstract public function setEntityAttributes(array $attributes); |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @return mixed |
|
182
|
|
|
*/ |
|
183
|
|
|
abstract public function getEntityAttributes(); |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param string $key |
|
187
|
|
|
* @return mixed |
|
188
|
|
|
*/ |
|
189
|
|
|
abstract public function hasAttribute($key); |
|
190
|
|
|
} |
|
191
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..