@@ -25,15 +25,15 @@ class Record implements \ArrayAccess
25
25
private $ state ;
26
26
27
27
/** @var Record[][] */
28
- private $ relations ;
28
+ private $ references ;
29
29
30
30
public function __construct (Schema $ schema )
31
31
{
32
32
$ this ->schema = $ schema ;
33
33
$ this ->values = array_fill_keys ($ schema ->getFields (), null );
34
34
$ this ->state = self ::STATE_INSERT ;
35
35
$ this ->changed = [];
36
- $ this ->relations = [];
36
+ $ this ->references = [];
37
37
}
38
38
39
39
public function getPrimaryKey (): array
@@ -82,9 +82,71 @@ public function getModel(): Model
82
82
return $ this ->schema ->getModel ($ this );
83
83
}
84
84
85
+ public function getReferredModel (string $ name ): Model
86
+ {
87
+ $ reference = $ this ->getSchema ()->getReference ($ name );
88
+
89
+ if (!$ reference ->isSingleRelationship ()) {
90
+ throw new \RuntimeException ('Can only refer to single models in a single relationship ' );
91
+ }
92
+
93
+ $ records = $ this ->getReference ($ name );
94
+
95
+ if (empty ($ records )) {
96
+ throw new \UnexpectedValueException ('The single relationship does not refer to any record ' );
97
+ }
98
+
99
+ return $ records [0 ]->getModel ();
100
+ }
101
+
102
+ public function getReferredModels (string $ name ): array
103
+ {
104
+ $ reference = $ this ->getSchema ()->getReference ($ name );
105
+
106
+ if ($ reference ->isSingleRelationship ()) {
107
+ throw new \RuntimeException ('Cannot refer to multiple models in a single relationship ' );
108
+ }
109
+
110
+ $ models = [];
111
+
112
+ foreach ($ this ->getReference ($ name ) as $ record ) {
113
+ $ models [] = $ record ->getModel ();
114
+ }
115
+
116
+ return $ models ;
117
+ }
118
+
119
+ public function getReferredProxyModels (string $ proxy , string $ name ): array
120
+ {
121
+ $ proxyReference = $ this ->getSchema ()->getReference ($ proxy );
122
+ $ reference = $ proxyReference ->getReferencedSchema ()->getReference ($ name );
123
+
124
+ if ($ proxyReference ->isSingleRelationship ()) {
125
+ throw new \RuntimeException ('Cannot refer to multiple models in a single relationship ' );
126
+ }
127
+
128
+ if (!$ reference ->isSingleRelationship ()) {
129
+ throw new \RuntimeException ('Can only refer to single models in a single relationship ' );
130
+ }
131
+
132
+ $ models = [];
133
+
134
+ foreach ($ this ->getReference ($ proxy ) as $ record ) {
135
+ $ records = $ record ->getReference ($ name );
136
+
137
+ if (empty ($ records )) {
138
+ throw new \UnexpectedValueException ('The single relationship does not refer to any record ' );
139
+ }
140
+
141
+ $ models [] = $ records [0 ]->getModel ();
142
+ }
143
+
144
+ return $ models ;
145
+ }
146
+
85
147
public function isReferenceLoaded (string $ name ): bool
86
148
{
87
- return isset ($ this ->relations [$ name ]);
149
+ return isset ($ this ->references [$ name ]);
88
150
}
89
151
90
152
/**
@@ -93,11 +155,11 @@ public function isReferenceLoaded(string $name): bool
93
155
*/
94
156
public function getReference (string $ name ): array
95
157
{
96
- if (!isset ($ this ->relations [$ name ])) {
158
+ if (!isset ($ this ->references [$ name ])) {
97
159
throw new \RuntimeException ("Cannot access relation ' $ name' that has not been provided " );
98
160
}
99
161
100
- return $ this ->relations [$ name ];
162
+ return $ this ->references [$ name ];
101
163
}
102
164
103
165
public function fillReference (string $ name , array $ records ): void
@@ -114,7 +176,7 @@ public function fillReference(string $name, array $records): void
114
176
throw new \InvalidArgumentException ('The relationship cannot reference more than a single record ' );
115
177
}
116
178
117
- $ this ->relations [$ name ] = array_values ($ records );
179
+ $ this ->references [$ name ] = array_values ($ records );
118
180
}
119
181
120
182
private function isRelated (Reference $ reference , Record $ record ): bool
@@ -135,13 +197,16 @@ private function isRelated(Reference $reference, Record $record): bool
135
197
return true ;
136
198
}
137
199
200
+ /**
201
+ * @return Record[]
202
+ */
138
203
public function getMappedRecords (): array
139
204
{
140
205
/** @var Record[] $records */
141
206
$ records = [spl_object_id ($ this ) => $ this ];
142
207
143
208
do {
144
- foreach (current ($ records )->relations as $ relation ) {
209
+ foreach (current ($ records )->references as $ relation ) {
145
210
foreach ($ relation as $ record ) {
146
211
$ id = spl_object_id ($ record );
147
212
0 commit comments