|
1
|
|
|
<?php namespace Limoncello\Models; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015-2016 [email protected] (www.neomerx.com) |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use Limoncello\Models\Contracts\SchemaStorageInterface; |
|
20
|
|
|
use LogicException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @package Limoncello\Models |
|
24
|
|
|
*/ |
|
25
|
|
|
class SchemaStorage implements SchemaStorageInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
private $relationshipTypes; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
private $reversedRelationships; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
private $foreignKeys; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
private $belongsToMany; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
private $tableNames; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var array |
|
54
|
|
|
*/ |
|
55
|
|
|
private $primaryKeys; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var array |
|
59
|
|
|
*/ |
|
60
|
|
|
private $attributeTypes; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var array |
|
64
|
|
|
*/ |
|
65
|
|
|
private $attributeLengths; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @inheritdoc |
|
69
|
|
|
*/ |
|
70
|
5 |
|
public function getData() |
|
71
|
|
|
{ |
|
72
|
|
|
$result = [ |
|
73
|
5 |
|
$this->foreignKeys, |
|
74
|
5 |
|
$this->belongsToMany, |
|
75
|
5 |
|
$this->relationshipTypes, |
|
76
|
5 |
|
$this->reversedRelationships, |
|
77
|
5 |
|
$this->tableNames, |
|
78
|
5 |
|
$this->primaryKeys, |
|
79
|
5 |
|
$this->attributeTypes, |
|
80
|
5 |
|
$this->attributeLengths, |
|
81
|
5 |
|
]; |
|
82
|
|
|
|
|
83
|
5 |
|
return $result; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @inheritdoc |
|
88
|
|
|
*/ |
|
89
|
5 |
|
public function setData(array $data) |
|
90
|
|
|
{ |
|
91
|
5 |
|
list($this->foreignKeys, $this->belongsToMany, $this->relationshipTypes, $this->reversedRelationships, |
|
92
|
5 |
|
$this->tableNames, $this->primaryKeys, $this->attributeTypes, $this->attributeLengths) = $data; |
|
93
|
5 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @inheritdoc |
|
97
|
|
|
*/ |
|
98
|
6 |
|
public function registerClass($class, $tableName, $primaryKey, array $attributeTypes, array $attributeLengths) |
|
99
|
|
|
{ |
|
100
|
6 |
|
$this->tableNames[$class] = $tableName; |
|
101
|
6 |
|
$this->primaryKeys[$class] = $primaryKey; |
|
102
|
6 |
|
$this->attributeTypes[$class] = $attributeTypes; |
|
103
|
6 |
|
$this->attributeLengths[$class] = $attributeLengths; |
|
104
|
|
|
|
|
105
|
6 |
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @inheritdoc |
|
110
|
|
|
*/ |
|
111
|
1 |
|
public function hasClass($class) |
|
112
|
|
|
{ |
|
113
|
1 |
|
$result = array_key_exists($class, $this->tableNames); |
|
114
|
|
|
|
|
115
|
1 |
|
return $result; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @inheritdoc |
|
120
|
|
|
*/ |
|
121
|
1 |
|
public function getTable($class) |
|
122
|
|
|
{ |
|
123
|
1 |
|
$result = $this->tableNames[$class]; |
|
124
|
|
|
|
|
125
|
1 |
|
return $result; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @inheritdoc |
|
130
|
|
|
*/ |
|
131
|
2 |
|
public function getPrimaryKey($class) |
|
132
|
|
|
{ |
|
133
|
2 |
|
$result = $this->primaryKeys[$class]; |
|
134
|
|
|
|
|
135
|
2 |
|
return $result; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @inheritdoc |
|
140
|
|
|
*/ |
|
141
|
1 |
|
public function getAttributeTypes($class) |
|
142
|
|
|
{ |
|
143
|
1 |
|
$result = $this->attributeTypes[$class]; |
|
144
|
|
|
|
|
145
|
1 |
|
return $result; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @inheritdoc |
|
150
|
|
|
*/ |
|
151
|
1 |
|
public function getAttributeType($class, $name) |
|
152
|
|
|
{ |
|
153
|
1 |
|
$result = $this->attributeTypes[$class][$name]; |
|
154
|
|
|
|
|
155
|
1 |
|
return $result; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @inheritdoc |
|
160
|
|
|
*/ |
|
161
|
1 |
|
public function hasAttributeType($class, $name) |
|
162
|
|
|
{ |
|
163
|
1 |
|
$result = isset($this->attributeTypes[$class][$name]); |
|
164
|
|
|
|
|
165
|
1 |
|
return $result; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @inheritdoc |
|
170
|
|
|
*/ |
|
171
|
1 |
|
public function getAttributeLengths($class) |
|
172
|
|
|
{ |
|
173
|
1 |
|
$result = $this->attributeLengths[$class]; |
|
174
|
|
|
|
|
175
|
1 |
|
return $result; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @inheritdoc |
|
180
|
|
|
*/ |
|
181
|
1 |
|
public function hasAttributeLength($class, $name) |
|
182
|
|
|
{ |
|
183
|
1 |
|
$result = isset($this->attributeLengths[$class][$name]); |
|
184
|
|
|
|
|
185
|
1 |
|
return $result; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @inheritdoc |
|
190
|
|
|
*/ |
|
191
|
1 |
|
public function getAttributeLength($class, $name) |
|
192
|
|
|
{ |
|
193
|
1 |
|
$result = $this->attributeLengths[$class][$name]; |
|
194
|
|
|
|
|
195
|
1 |
|
return $result; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @inheritdoc |
|
200
|
|
|
*/ |
|
201
|
2 |
|
public function hasRelationship($class, $name) |
|
202
|
|
|
{ |
|
203
|
2 |
|
$result = isset($this->relationshipTypes[$class][$name]); |
|
204
|
|
|
|
|
205
|
2 |
|
return $result; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @inheritdoc |
|
210
|
|
|
*/ |
|
211
|
2 |
|
public function getRelationshipType($class, $name) |
|
212
|
|
|
{ |
|
213
|
2 |
|
$result = $this->relationshipTypes[$class][$name]; |
|
214
|
|
|
|
|
215
|
2 |
|
return $result; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @inheritdoc |
|
220
|
|
|
*/ |
|
221
|
2 |
|
public function getReverseRelationship($class, $name) |
|
222
|
|
|
{ |
|
223
|
2 |
|
$result = $this->reversedRelationships[$class][$name]; |
|
224
|
|
|
|
|
225
|
2 |
|
return $result; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @inheritdoc |
|
230
|
|
|
*/ |
|
231
|
1 |
|
public function getForeignKey($class, $name) |
|
232
|
|
|
{ |
|
233
|
1 |
|
$result = $this->foreignKeys[$class][$name]; |
|
234
|
|
|
|
|
235
|
1 |
|
return $result; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* @inheritdoc |
|
240
|
|
|
*/ |
|
241
|
1 |
|
public function getBelongsToManyRelationship($class, $name) |
|
242
|
|
|
{ |
|
243
|
1 |
|
$result = $this->belongsToMany[$class][$name]; |
|
244
|
|
|
|
|
245
|
1 |
|
return $result; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @inheritdoc |
|
250
|
|
|
*/ |
|
251
|
5 |
|
public function registerBelongsToOneRelationship($class, $name, $foreignKey, $reverseClass, $reverseName) |
|
252
|
|
|
{ |
|
253
|
5 |
|
$this->registerRelationshipType(RelationshipTypes::BELONGS_TO, $class, $name); |
|
254
|
5 |
|
$this->registerRelationshipType(RelationshipTypes::HAS_MANY, $reverseClass, $reverseName); |
|
255
|
|
|
|
|
256
|
5 |
|
$this->registerReversedRelationship($class, $name, $reverseClass, $reverseName); |
|
257
|
5 |
|
$this->registerReversedRelationship($reverseClass, $reverseName, $class, $name); |
|
258
|
|
|
|
|
259
|
5 |
|
$this->foreignKeys[$class][$name] = $foreignKey; |
|
260
|
|
|
|
|
261
|
5 |
|
return $this; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @inheritdoc |
|
266
|
|
|
*/ |
|
267
|
5 |
|
public function registerBelongsToManyRelationship( |
|
268
|
|
|
$class, |
|
269
|
|
|
$name, |
|
270
|
|
|
$table, |
|
271
|
|
|
$foreignKey, |
|
272
|
|
|
$reverseForeignKey, |
|
273
|
|
|
$reverseClass, |
|
274
|
|
|
$reverseName |
|
275
|
|
|
) { |
|
276
|
5 |
|
$this->registerRelationshipType(RelationshipTypes::BELONGS_TO_MANY, $class, $name); |
|
277
|
5 |
|
$this->registerRelationshipType(RelationshipTypes::BELONGS_TO_MANY, $reverseClass, $reverseName); |
|
278
|
|
|
|
|
279
|
|
|
// NOTE: |
|
280
|
|
|
// `registerReversedRelationship` relies on duplicate registration check in `registerRelationshipType` |
|
281
|
|
|
// so it must be called afterwards |
|
282
|
5 |
|
$this->registerReversedRelationship($class, $name, $reverseClass, $reverseName); |
|
283
|
5 |
|
$this->registerReversedRelationship($reverseClass, $reverseName, $class, $name); |
|
284
|
|
|
|
|
285
|
5 |
|
$this->belongsToMany[$class][$name] = [$table, $foreignKey, $reverseForeignKey]; |
|
286
|
5 |
|
$this->belongsToMany[$reverseClass][$reverseName] = [$table, $reverseForeignKey, $foreignKey]; |
|
287
|
|
|
|
|
288
|
5 |
|
return $this; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* @param int $type |
|
293
|
|
|
* @param string $class |
|
294
|
|
|
* @param string $name |
|
295
|
|
|
*/ |
|
296
|
5 |
|
private function registerRelationshipType($type, $class, $name) |
|
297
|
|
|
{ |
|
298
|
5 |
|
if (isset($this->relationshipTypes[$class][$name]) === true) { |
|
299
|
|
|
// already registered |
|
300
|
2 |
|
throw new LogicException(); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
5 |
|
$this->relationshipTypes[$class][$name] = $type; |
|
304
|
5 |
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* @param string $class |
|
308
|
|
|
* @param string $name |
|
309
|
|
|
* @param string $reverseClass |
|
310
|
|
|
* @param string $reverseName |
|
311
|
|
|
*/ |
|
312
|
5 |
|
private function registerReversedRelationship($class, $name, $reverseClass, $reverseName) |
|
313
|
|
|
{ |
|
314
|
|
|
// NOTE: |
|
315
|
|
|
// this function relies it would be called after |
|
316
|
|
|
// `registerRelationshipType` which prevents duplicate registrations |
|
317
|
|
|
|
|
318
|
5 |
|
$this->reversedRelationships[$class][$name] = [$reverseClass, $reverseName]; |
|
319
|
5 |
|
} |
|
320
|
|
|
} |
|
321
|
|
|
|