@@ -25,7 +25,7 @@ class Record implements \ArrayAccess
25
25
private $ state ;
26
26
27
27
/** @var Record[][] */
28
- private $ references ;
28
+ private $ referencedRecords ;
29
29
30
30
private $ model ;
31
31
@@ -35,7 +35,7 @@ public function __construct(Schema $schema)
35
35
$ this ->values = array_fill_keys ($ schema ->getFields (), null );
36
36
$ this ->state = self ::STATE_INSERT ;
37
37
$ this ->changed = [];
38
- $ this ->references = [];
38
+ $ this ->referencedRecords = [];
39
39
}
40
40
41
41
public function getPrimaryKey (): array
@@ -88,12 +88,43 @@ public function getModel(): Model
88
88
return $ this ->model ;
89
89
}
90
90
91
- public function getReferencedModel (string $ name ): Model
91
+ public function hasReferencedRecords (string $ name ): bool
92
+ {
93
+ $ name = $ this ->getSchema ()->getRelationship ($ name )->getName ();
94
+
95
+ return isset ($ this ->referencedRecords [$ name ]);
96
+ }
97
+
98
+ public function setReferencedRecords (string $ name , array $ records ): void
99
+ {
100
+ $ name = $ this ->getSchema ()->getRelationship ($ name )->getName ();
101
+
102
+ (function (Record ... $ records ) use ($ name ): void {
103
+ $ this ->referencedRecords [$ name ] = $ records ;
104
+ })(... $ records );
105
+ }
106
+
107
+ /**
108
+ * @param string $name
109
+ * @return Record[]
110
+ */
111
+ public function getReferencedRecords (string $ name ): array
112
+ {
113
+ $ name = $ this ->getSchema ()->getRelationship ($ name )->getName ();
114
+
115
+ if (!isset ($ this ->referencedRecords [$ name ])) {
116
+ throw new \RuntimeException ("The referenced records for the relationship ' $ name' have not been provided " );
117
+ }
118
+
119
+ return $ this ->referencedRecords [$ name ];
120
+ }
121
+
122
+ public function getRelatedModel (string $ name ): ?Model
92
123
{
93
124
$ relationship = $ this ->getSchema ()->getRelationship ($ name );
94
125
95
126
if (!$ relationship ->isUniqueRelationship ()) {
96
- throw new \RuntimeException ('Cannot fetch a single model a non- unique relationship ' );
127
+ throw new \RuntimeException ('A single related model can only be fetched for an unique relationship ' );
97
128
}
98
129
99
130
$ records = $ this ->getReferencedRecords ($ name );
@@ -105,12 +136,12 @@ public function getReferencedModel(string $name): Model
105
136
return $ this ->getReferencedRecords ($ name )[0 ]->getModel ();
106
137
}
107
138
108
- public function getReferredModels (string $ name ): array
139
+ public function getRelatedModels (string $ name ): array
109
140
{
110
- $ reference = $ this ->getSchema ()->getRelationship ($ name );
141
+ $ relationship = $ this ->getSchema ()->getRelationship ($ name );
111
142
112
- if ($ reference ->isUniqueRelationship ()) {
113
- throw new \RuntimeException ('Cannot refer to multiple models in a single relationship ' );
143
+ if ($ relationship ->isUniqueRelationship ()) {
144
+ throw new \RuntimeException ('Cannot fetch multiple models for an unique relationship ' );
114
145
}
115
146
116
147
$ models = [];
@@ -122,17 +153,17 @@ public function getReferredModels(string $name): array
122
153
return $ models ;
123
154
}
124
155
125
- public function getReferredProxyModels (string $ proxy , string $ name ): array
156
+ public function getRelatedModelsByProxy (string $ proxy , string $ name ): array
126
157
{
127
- $ proxyReference = $ this ->getSchema ()->getRelationship ($ proxy );
128
- $ reference = $ proxyReference ->getReferencedSchema ()->getRelationship ($ name );
158
+ $ proxyRelationship = $ this ->getSchema ()->getRelationship ($ proxy );
159
+ $ relationship = $ proxyRelationship ->getReferencedSchema ()->getRelationship ($ name );
129
160
130
- if ($ proxyReference ->isUniqueRelationship ()) {
131
- throw new \RuntimeException ('Cannot refer to multiple models in a single relationship ' );
161
+ if ($ proxyRelationship ->isUniqueRelationship ()) {
162
+ throw new \RuntimeException ('Cannot fetch related models via an unique proxy relationship ' );
132
163
}
133
164
134
- if (!$ reference ->isUniqueRelationship ()) {
135
- throw new \RuntimeException ('Can only refer to single models in a single relationship ' );
165
+ if (!$ relationship ->isUniqueRelationship ()) {
166
+ throw new \RuntimeException ('Related models can only be fetched via proxy with an unique relationship ' );
136
167
}
137
168
138
169
$ models = [];
@@ -141,7 +172,7 @@ public function getReferredProxyModels(string $proxy, string $name): array
141
172
$ records = $ record ->getReferencedRecords ($ name );
142
173
143
174
if (empty ($ records )) {
144
- throw new \ UnexpectedValueException ( ' The single relationship does not refer to any record ' ) ;
175
+ continue ;
145
176
}
146
177
147
178
$ models [] = $ records [0 ]->getModel ();
@@ -150,82 +181,17 @@ public function getReferredProxyModels(string $proxy, string $name): array
150
181
return $ models ;
151
182
}
152
183
153
- public function hasReferencedRecords (string $ name ): bool
154
- {
155
- return isset ($ this ->references [$ name ]);
156
- }
157
-
158
- /**
159
- * @param string $name
160
- * @return Record[]
161
- */
162
- public function getReferencedRecords (string $ name ): array
163
- {
164
- if (!isset ($ this ->references [$ name ])) {
165
- throw new \RuntimeException ("The referenced records for the relationship ' $ name' have not been filled " );
166
- }
167
-
168
- return $ this ->references [$ name ];
169
- }
170
-
171
- /**
172
- * @param string $name
173
- * @param Record[] $records
174
- */
175
- public function fillReferencedRecords (string $ name , array $ records ): void
176
- {
177
- $ relationship = $ this ->getSchema ()->getRelationship ($ name );
178
-
179
- if (\count ($ records ) > 1 && $ relationship ->isUniqueRelationship ()) {
180
- throw new \InvalidArgumentException ('A unique relationship cannot reference more than a single record ' );
181
- }
182
-
183
- foreach ($ records as $ record ) {
184
- if (!$ this ->isRelated ($ relationship , $ record )) {
185
- throw new \InvalidArgumentException ('The provided records are not related to this record ' );
186
- }
187
- }
188
-
189
- if ($ relationship ->getReverseRelationship ()->isUniqueRelationship ()) {
190
- $ reverse = $ relationship ->getReverseRelationship ()->getName ();
191
-
192
- foreach ($ records as $ record ) {
193
- $ record ->references [$ reverse ] = [$ this ];
194
- }
195
- }
196
-
197
- $ this ->references [$ name ] = array_values ($ records );
198
- }
199
-
200
- private function isRelated (Relationship $ reference , Record $ record ): bool
201
- {
202
- if ($ reference ->getReferencedSchema () !== $ record ->getSchema ()) {
203
- return false ;
204
- }
205
-
206
- $ keys = $ reference ->getFields ();
207
- $ fields = $ reference ->getReferencedFields ();
208
-
209
- foreach ($ keys as $ index => $ key ) {
210
- if ((string ) $ this ->values [$ key ] !== (string ) $ record ->values [$ fields [$ index ]]) {
211
- return false ;
212
- }
213
- }
214
-
215
- return true ;
216
- }
217
-
218
184
/**
219
185
* @return Record[]
220
186
*/
221
- public function getMappedRecords (): array
187
+ public function getAllReferencedRecords (): array
222
188
{
223
189
/** @var Record[] $records */
224
190
$ records = [spl_object_id ($ this ) => $ this ];
225
191
226
192
do {
227
- foreach (current ($ records )->references as $ relation ) {
228
- foreach ($ relation as $ record ) {
193
+ foreach (current ($ records )->referencedRecords as $ recordList ) {
194
+ foreach ($ recordList as $ record ) {
229
195
$ id = spl_object_id ($ record );
230
196
231
197
if (!isset ($ records [$ id ])) {
@@ -288,6 +254,4 @@ public function offsetUnset($offset)
288
254
{
289
255
$ this ->offsetSet ($ offset , null );
290
256
}
291
-
292
-
293
257
}
0 commit comments