-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRelationship.php
218 lines (189 loc) · 4.67 KB
/
Relationship.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
<?php
namespace Darya\ORM;
use Darya\Storage\Query;
/**
* Darya's entity relationship class.
*
* Represents a relationship between two entities.
*
* Used to load related entities for parent entities.
*
* @property-read string $name The relationship name.
* @property-read EntityMap $parentMap The parent entity map.
* @property-read EntityMap $relatedMap The related entity map.
* @property-read string $foreignKey The foreign key.
*
* @author Chris Andrew <[email protected]>
*/
abstract class Relationship extends Query
{
/**
* The relationship name.
*
* This should match the corresponding attribute on the parent entity.
*
* @var string
*/
protected string $name;
/**
* The parent entity map.
*/
protected EntityMap $parentMap;
/**
* The related entity map.
*/
protected EntityMap $relatedMap;
/**
* The foreign key attribute.
*/
protected string $foreignKey;
/**
* Create a new relationship.
*
* @param string $name The relationship name.
* @param EntityMap $parentMap The parent entity map.
* @param EntityMap $relatedMap The related entity map.
* @param string $foreignKey The foreign key attribute.
*/
public function __construct(string $name, EntityMap $parentMap, EntityMap $relatedMap, string $foreignKey = '')
{
parent::__construct($relatedMap->getName());
$this->name = $name;
$this->parentMap = $parentMap;
$this->relatedMap = $relatedMap;
$this->foreignKey = $foreignKey;
}
/**
* Build a new instance of this relationship query for a given parent entity.
*
* @param mixed $entity The parent entity.
* @param EntityManager $orm
* @return Relationship The new relationship query.
*/
abstract public function forParent($entity, EntityManager $orm): Relationship;
/**
* Build a new eager-loading instance of this relationship query for the given parent entities.
*
* @param mixed[] $entities The parent entities.
* @param EntityManager $orm
* @return Relationship The new relationship query.
*/
abstract public function forParents(array $entities, EntityManager $orm): Relationship;
/**
* Match a set of eagerly-loaded related entities to the given parent entities.
*
* @param mixed[] $parentEntities The parent entities to match.
* @param mixed[] $relatedEntities The related entities to match.
* @param EntityManager $orm
* @return mixed[] The parent entities with their related entities matched.
*/
abstract public function match(array $parentEntities, array $relatedEntities, EntityManager $orm): array;
/**
* Get the relationship name.
*
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* Get the parent entity map.
*
* @return EntityMap
*/
public function getParentMap(): EntityMap
{
return $this->parentMap;
}
/**
* Get the related entity map.
*
* @return EntityMap
*/
public function getRelatedMap(): EntityMap
{
return $this->relatedMap;
}
/**
* Get the parent entity's primary key attribute(s).
*
* @return string|string[]
*/
public function getParentKey()
{
return $this->getParentMap()->getKey();
}
/**
* Get the related entity's primary key attribute(s).
*
* @return string|string[]
*/
public function getRelatedKey()
{
return $this->getRelatedMap()->getKey();
}
/**
* Get the relationship's foreign key attribute.
*
* @return string
*/
public function getForeignKey(): string
{
return $this->foreignKey;
}
/**
* Get the parent entity's ID.
*
* @param mixed $parent
* @return mixed
*/
public function getParentId($parent)
{
$ids = $this->getParentIds([$parent]);
return $ids[0] ?? null;
}
/**
* Get the IDs of the given parent entities.
*
* @param mixed[] $parents
* @return mixed[]
*/
public function getParentIds(array $parents): array
{
$parentMap = $this->getParentMap();
$parentKey = $parentMap->getKey();
$ids = [];
foreach ($parents as $parent) {
$ids[] = $parentMap->readAttribute($parent, $parentKey);
}
return $ids;
}
/**
* Get the related entity's foreign key.
*
* @param mixed $related
* @return mixed
*/
public function getRelatedId($related)
{
$ids = $this->getRelatedIds([$related]);
return $ids[0] ?? null;
}
/**
* Get the foreign key values of the given related entities.
*
* @param mixed[] $related Related entities to load the foreign key values from.
* @return mixed[]
*/
public function getRelatedIds(array $related): array
{
$relatedMap = $this->getRelatedMap();
$relatedKey = $relatedMap->getKey();
$ids = [];
foreach ($related as $entity) {
$ids[] = $relatedMap->readAttribute($entity, $relatedKey);
}
return $ids;
}
}