This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
RelationshipMetadata.php
293 lines (256 loc) · 7.03 KB
/
RelationshipMetadata.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/*
* This file is part of the GraphAware Neo4j PHP OGM package.
*
* (c) GraphAware Ltd <info@graphaware.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace GraphAware\Neo4j\OGM\Metadata;
use Doctrine\Common\Collections\ArrayCollection;
use GraphAware\Neo4j\OGM\Annotations\OrderBy;
use GraphAware\Neo4j\OGM\Annotations\Relationship;
use GraphAware\Neo4j\OGM\Common\Collection;
use GraphAware\Neo4j\OGM\Exception\MappingException;
use GraphAware\Neo4j\OGM\Lazy\LazyRelationshipCollection;
use GraphAware\Neo4j\OGM\Util\ClassUtils;
final class RelationshipMetadata
{
/**
* @var string
*/
private $className;
/**
* @var string
*/
private $propertyName;
/**
* @var \ReflectionProperty
*/
private $reflectionProperty;
/**
* @var \GraphAware\Neo4j\OGM\Annotations\Relationship
*/
private $relationshipAnnotation;
/**
* @var bool
*/
private $isLazy;
/**
* @var bool
*/
private $isFetch;
/**
* @var OrderBy
*/
private $orderBy;
/**
* @param string $className
* @param \ReflectionProperty $reflectionProperty
* @param \GraphAware\Neo4j\OGM\Annotations\Relationship $relationshipAnnotation
* @param bool $isLazy
* @param OrderBy $orderBy
*/
public function __construct($className, \ReflectionProperty $reflectionProperty, Relationship $relationshipAnnotation, $isLazy = false, $isFetch = false, OrderBy $orderBy = null)
{
$this->className = $className;
$this->propertyName = $reflectionProperty->getName();
$this->reflectionProperty = $reflectionProperty;
$this->relationshipAnnotation = $relationshipAnnotation;
$this->isLazy = $isLazy;
$this->isFetch = $isFetch;
$this->orderBy = $orderBy;
if (null !== $orderBy) {
if (!in_array($orderBy->order, ['ASC', 'DESC'], true)) {
throw new MappingException(sprintf('The order "%s" is not valid', $orderBy->order));
}
}
}
/**
* @return string
*/
public function getPropertyName()
{
return $this->propertyName;
}
/**
* @return \ReflectionProperty
*/
public function getReflectionProperty()
{
return $this->reflectionProperty;
}
/**
* @return string
*/
public function getType()
{
return $this->relationshipAnnotation->type;
}
/**
* @return bool
*/
public function isRelationshipEntity()
{
return null !== $this->relationshipAnnotation->relationshipEntity;
}
/**
* @return bool
*/
public function isCollection()
{
return true === $this->relationshipAnnotation->collection;
}
/**
* @return bool
*/
public function isLazy()
{
return $this->isLazy;
}
/**
* @return bool
*/
public function isFetch()
{
return $this->isFetch;
}
/**
* @return string
*/
public function getDirection()
{
return $this->relationshipAnnotation->direction;
}
/**
* @return string
*/
public function getTargetEntity()
{
return ClassUtils::getFullClassName($this->relationshipAnnotation->targetEntity, $this->className);
}
/**
* @return string
*/
public function getRelationshipEntityClass()
{
return ClassUtils::getFullClassName($this->relationshipAnnotation->relationshipEntity, $this->className);
}
/**
* @return bool
*/
public function hasMappedByProperty()
{
return null !== $this->relationshipAnnotation->mappedBy;
}
/**
* @return string
*/
public function getMappedByProperty()
{
return $this->relationshipAnnotation->mappedBy;
}
/**
* @return bool
*/
public function hasOrderBy()
{
return null !== $this->orderBy;
}
/**
* @return string
*/
public function getOrderByPropery()
{
return $this->orderBy->property;
}
/**
* @return string
*/
public function getOrder()
{
return $this->orderBy->order;
}
/**
* @param $object
*/
public function initializeCollection($object)
{
if (!$this->isCollection()) {
throw new \LogicException(sprintf('The property mapping this relationship is not of collection type in "%s"', $this->className));
}
if (is_array($this->getValue($object)) && !empty($this->getValue($object))) {
$this->setValue($object, new ArrayCollection($this->getValue($object)));
return;
}
if ($this->getValue($object) instanceof ArrayCollection || $this->getValue($object) instanceof LazyRelationshipCollection) {
return;
}
$this->setValue($object, new Collection());
}
/**
* @param object $object
* @param mixed $value
*/
public function addToCollection($object, $value)
{
if (!$this->isCollection()) {
throw new \LogicException(sprintf('The property mapping of this relationship is not of collection type in "%s"', $this->className));
}
/** @var Collection $coll */
$coll = $this->getValue($object);
$toAdd = true;
$oid2 = spl_object_hash($value);
foreach ($coll->toArray() as $el) {
$oid1 = spl_object_hash($el);
if ($oid1 === $oid2) {
$toAdd = false;
}
}
if ($toAdd) {
$coll->add($value);
}
}
public function addToCollectionAdvanced($object, $value, NodeEntityMetadata $valueMetadata)
{
if (!$this->isCollection()) {
throw new \LogicException(sprintf('The property mapping of this relationship is not of collection type in "%s"', $this->className));
}
/** @var Collection $coll */
$coll = $this->getValue($object);
foreach ($coll->toArray() as $el) {
$eid = $valueMetadata->getIdValue($valueMetadata);
$vid = $valueMetadata->getIdValue($valueMetadata);
if ($eid !== $vid) {
$coll->add($value);
}
}
}
/**
* @param $object
*
* @return mixed
*/
public function getValue($object)
{
$this->reflectionProperty->setAccessible(true);
return $this->reflectionProperty->getValue($object);
}
/**
* @param $object
* @param $value
*/
public function setValue($object, $value)
{
$this->reflectionProperty->setAccessible(true);
$this->reflectionProperty->setValue($object, $value);
}
/**
* @return string
*/
public function getAlias()
{
return strtolower(str_replace('\\', '_', $this->className).'_'.$this->propertyName);
}
}