Completed
Push — master ( e358da...25d877 )
by Neomerx
03:44
created

SchemaStorage::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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
     * @var array
69
     */
70
    private $attributes;
71
72
    /**
73
     * @inheritdoc
74
     */
75 5
    public function getData()
76
    {
77
        $result = [
78 5
            $this->foreignKeys,
79 5
            $this->belongsToMany,
80 5
            $this->relationshipTypes,
81 5
            $this->reversedRelationships,
82 5
            $this->tableNames,
83 5
            $this->primaryKeys,
84 5
            $this->attributeTypes,
85 5
            $this->attributeLengths,
86 5
            $this->attributes,
87 5
        ];
88
89 5
        return $result;
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95 5
    public function setData(array $data)
96
    {
97 5
        list($this->foreignKeys, $this->belongsToMany, $this->relationshipTypes,
98 5
            $this->reversedRelationships,$this->tableNames, $this->primaryKeys,
99 5
            $this->attributeTypes, $this->attributeLengths, $this->attributes) = $data;
100 5
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105 6
    public function registerClass($class, $tableName, $primaryKey, array $attributeTypes, array $attributeLengths)
106
    {
107 6
        $this->tableNames[$class]       = $tableName;
108 6
        $this->primaryKeys[$class]      = $primaryKey;
109 6
        $this->attributeTypes[$class]   = $attributeTypes;
110 6
        $this->attributeLengths[$class] = $attributeLengths;
111 6
        $this->attributes[$class]       = array_keys($attributeTypes);
112
113 6
        return $this;
114
    }
115
116
    /**
117
     * @inheritdoc
118
     */
119 1
    public function hasClass($class)
120
    {
121 1
        $result = array_key_exists($class, $this->tableNames);
122
123 1
        return $result;
124
    }
125
126
    /**
127
     * @inheritdoc
128
     */
129 1
    public function getTable($class)
130
    {
131 1
        $result = $this->tableNames[$class];
132
133 1
        return $result;
134
    }
135
136
    /**
137
     * @inheritdoc
138
     */
139 2
    public function getPrimaryKey($class)
140
    {
141 2
        $result = $this->primaryKeys[$class];
142
143 2
        return $result;
144
    }
145
146
    /**
147
     * @inheritdoc
148
     */
149 1
    public function getAttributeTypes($class)
150
    {
151 1
        $result = $this->attributeTypes[$class];
152
153 1
        return $result;
154
    }
155
156
    /**
157
     * @inheritdoc
158
     */
159 1
    public function getAttributeType($class, $name)
160
    {
161 1
        $result = $this->attributeTypes[$class][$name];
162
163 1
        return $result;
164
    }
165
166
    /**
167
     * @inheritdoc
168
     */
169 1
    public function hasAttributeType($class, $name)
170
    {
171 1
        $result = isset($this->attributeTypes[$class][$name]);
172
173 1
        return $result;
174
    }
175
176
    /**
177
     * @inheritdoc
178
     */
179 1
    public function getAttributeLengths($class)
180
    {
181 1
        $result = $this->attributeLengths[$class];
182
183 1
        return $result;
184
    }
185
186
    /**
187
     * @inheritdoc
188
     */
189 1
    public function hasAttributeLength($class, $name)
190
    {
191 1
        $result = isset($this->attributeLengths[$class][$name]);
192
193 1
        return $result;
194
    }
195
196
    /**
197
     * @inheritdoc
198
     */
199 1
    public function getAttributeLength($class, $name)
200
    {
201 1
        $result = $this->attributeLengths[$class][$name];
202
203 1
        return $result;
204
    }
205
206
    /**
207
     * @inheritdoc
208
     */
209 1
    public function getAttributes($class)
210
    {
211 1
        $result = $this->attributes[$class];
212
213 1
        return $result;
214
    }
215
216
    /**
217
     * @inheritdoc
218
     */
219 2
    public function hasRelationship($class, $name)
220
    {
221 2
        $result = isset($this->relationshipTypes[$class][$name]);
222
223 2
        return $result;
224
    }
225
226
    /**
227
     * @inheritdoc
228
     */
229 2
    public function getRelationshipType($class, $name)
230
    {
231 2
        $result = $this->relationshipTypes[$class][$name];
232
233 2
        return $result;
234
    }
235
236
    /**
237
     * @inheritdoc
238
     */
239 2
    public function getReverseRelationship($class, $name)
240
    {
241 2
        $result = $this->reversedRelationships[$class][$name];
242
243 2
        return $result;
244
    }
245
246
    /**
247
     * @inheritdoc
248
     */
249 1
    public function getForeignKey($class, $name)
250
    {
251 1
        $result = $this->foreignKeys[$class][$name];
252
253 1
        return $result;
254
    }
255
256
    /**
257
     * @inheritdoc
258
     */
259 1
    public function getBelongsToManyRelationship($class, $name)
260
    {
261 1
        $result = $this->belongsToMany[$class][$name];
262
263 1
        return $result;
264
    }
265
266
    /**
267
     * @inheritdoc
268
     */
269 5
    public function registerBelongsToOneRelationship($class, $name, $foreignKey, $reverseClass, $reverseName)
270
    {
271 5
        $this->registerRelationshipType(RelationshipTypes::BELONGS_TO, $class, $name);
272 5
        $this->registerRelationshipType(RelationshipTypes::HAS_MANY, $reverseClass, $reverseName);
273
274 5
        $this->registerReversedRelationship($class, $name, $reverseClass, $reverseName);
275 5
        $this->registerReversedRelationship($reverseClass, $reverseName, $class, $name);
276
277 5
        $this->foreignKeys[$class][$name] = $foreignKey;
278
279 5
        return $this;
280
    }
281
282
    /**
283
     * @inheritdoc
284
     */
285 5
    public function registerBelongsToManyRelationship(
286
        $class,
287
        $name,
288
        $table,
289
        $foreignKey,
290
        $reverseForeignKey,
291
        $reverseClass,
292
        $reverseName
293
    ) {
294 5
        $this->registerRelationshipType(RelationshipTypes::BELONGS_TO_MANY, $class, $name);
295 5
        $this->registerRelationshipType(RelationshipTypes::BELONGS_TO_MANY, $reverseClass, $reverseName);
296
297
        // NOTE:
298
        // `registerReversedRelationship` relies on duplicate registration check in `registerRelationshipType`
299
        // so it must be called afterwards
300 5
        $this->registerReversedRelationship($class, $name, $reverseClass, $reverseName);
301 5
        $this->registerReversedRelationship($reverseClass, $reverseName, $class, $name);
302
303 5
        $this->belongsToMany[$class][$name]               = [$table, $foreignKey, $reverseForeignKey];
304 5
        $this->belongsToMany[$reverseClass][$reverseName] = [$table, $reverseForeignKey, $foreignKey];
305
306 5
        return $this;
307
    }
308
309
    /**
310
     * @param int    $type
311
     * @param string $class
312
     * @param string $name
313
     */
314 5
    private function registerRelationshipType($type, $class, $name)
315
    {
316 5
        if (isset($this->relationshipTypes[$class][$name]) === true) {
317
            // already registered
318 2
            throw new LogicException();
319
        }
320
321 5
        $this->relationshipTypes[$class][$name] = $type;
322 5
    }
323
324
    /**
325
     * @param string $class
326
     * @param string $name
327
     * @param string $reverseClass
328
     * @param string $reverseName
329
     */
330 5
    private function registerReversedRelationship($class, $name, $reverseClass, $reverseName)
331
    {
332
        // NOTE:
333
        // this function relies it would be called after
334
        // `registerRelationshipType` which prevents duplicate registrations
335
336 5
        $this->reversedRelationships[$class][$name] = [$reverseClass, $reverseName];
337 5
    }
338
}
339