Table::getColumn()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
crap 12
1
<?php
2
3
/*
4
 * This file is part of the "RocketORM" package.
5
 *
6
 * https://github.com/RocketORM/ORM
7
 *
8
 * For the full license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Rocket\ORM\Generator\Schema;
13
14
/**
15
 * @author Sylvain Lorinet <[email protected]>
16
 */
17
class Table
18
{
19
    /**
20
     * @var string
21
     */
22
    public $name;
23
24
    /**
25
     * @var string
26
     */
27
    public $phpName;
28
29
    /**
30
     * @var string
31
     */
32
    public $type;
33
34
    /**
35
     * @var array|Column[]
36
     */
37
    protected $columns = [];
38
39
    /**
40
     * @var array|Relation[]
41
     */
42
    protected $relations = [];
43
44
    /**
45
     * @var array|Column[]
46
     */
47
    protected $primaryKeys = [];
48
49
    /**
50
     * @var Schema
51
     */
52
    protected $schema;
53
54
55
    /**
56
     * @param string $name
57
     * @param array  $data
58
     * @param array  $classes
59
     */
60
    public function __construct($name, array $data, array $classes)
61
    {
62
        $this->name    = $name;
63
        $this->phpName = $data['phpName'];
64
        $this->type    = $data['type'];
65
66
        $columnClass = $classes['column'];
67
        $relationClass = $classes['relation'];
68
69
        foreach ($data['columns'] as $columnName => $columnData) {
70
            /** @var Column $column */
71
            $column = new $columnClass($columnName, $columnData);
72
            $column->setTable($this);
73
74
            $this->columns[] = $column;
75
        }
76
77
        foreach ($data['relations'] as $with => $relationData) {
78
            /** @var Relation $relation */
79
            $relation = new $relationClass($with, $relationData);
80
            $relation->setLocalTable($this);
81
82
            $this->relations[] = $relation;
83
        }
84
    }
85
86
    /**
87
     * @return Schema
88
     */
89
    public function getSchema()
90
    {
91
        return $this->schema;
92
    }
93
94
    /**
95
     * @param Schema $schema
96
     */
97
    public function setSchema($schema)
98
    {
99
        $this->schema = $schema;
100
    }
101
102
    /**
103
     * @return Column[]
104
     */
105
    public function getColumns()
106
    {
107
        return $this->columns;
108
    }
109
110
    /**
111
     * @return array|Relation[]
112
     */
113
    public function getRelations()
114
    {
115
        return $this->relations;
116
    }
117
118
    /**
119
     * @param Column $column
120
     */
121
    public function addPrimaryKey(Column $column)
122
    {
123
        $this->primaryKeys[$column->name] = $column;
124
    }
125
126
    /**
127
     * @return array|Column[]
128
     */
129
    public function getPrimaryKeys()
130
    {
131
        return $this->primaryKeys;
132
    }
133
134
    /**
135
     * @return int
136
     */
137
    public function getPrimaryKeyCount()
138
    {
139
        $count = 0;
140
        foreach ($this->columns as $column) {
141
            if ($column->isPrimaryKey) {
142
                ++$count;
143
            }
144
        }
145
146
        return $count;
147
    }
148
149
    /**
150
     * @param Relation $relation
151
     */
152
    public function addRelation(Relation $relation)
153
    {
154
        $this->relations[] = $relation;
155
    }
156
157
    /**
158
     * @param string $name
159
     *
160
     * @return bool
161
     */
162
    public function hasRelation($name)
163
    {
164
        foreach ($this->relations as $relation) {
165
            if ($name == $relation->with) {
166
                return true;
167
            }
168
        }
169
170
        return false;
171
    }
172
173
    /**
174
     * @param string $name
175
     *
176
     * @return Column
177
     */
178
    public function getColumn($name)
179
    {
180
        foreach ($this->columns as $column) {
181
            if ($name == $column->name) {
182
                return $column;
183
            }
184
        }
185
186
        return null;
187
    }
188
189
    /**
190
     * @return array
191
     */
192
    public function getRelationsByTable()
193
    {
194
        $relations = [];
195
        foreach ($this->relations as $relation) {
196
            $relations[$relation->getLocalTable()->name][] = $relation;
197
        }
198
199
        return $relations;
200
    }
201
202
    /**
203
     * @return bool
204
     */
205
    public function hasForeignKey()
206
    {
207
        foreach ($this->relations as $relation) {
208
            if ($relation->isForeignKey()) {
209
                return true;
210
            }
211
        }
212
213
        return false;
214
    }
215
216
    /**
217
     * @return array
218
     */
219
    public function getForeignKeys()
220
    {
221
        $foreignKeys = [];
222
        foreach ($this->relations as $relation) {
223
            if ($relation->isForeignKey()) {
224
                $foreignKeys[$relation->getRelatedTable()->name][] = $relation;
225
            }
226
        }
227
228
        return $foreignKeys;
229
    }
230
231
    /**
232
     * @return bool
233
     */
234
    public function hasRelations()
235
    {
236
        return isset($this->relations[0]);
237
    }
238
239
    /**
240
     * @return bool
241
     */
242
    public function hasDefaultColumn()
243
    {
244
        foreach ($this->columns as $column) {
245
            if (null != $column->getDefault(true)) {
246
                return true;
247
            }
248
        }
249
250
        return false;
251
    }
252
253
    /**
254
     * @param bool $firstUpper
255
     *
256
     * @return string
257
     */
258
    public function getPhpName($firstUpper = true)
259
    {
260
        if ($firstUpper) {
261
            return $this->phpName;
262
        }
263
264
        return lcfirst($this->phpName);
265
    }
266
267
    /**
268
     * @return string
269
     */
270
    public function getNamespace()
271
    {
272
        return $this->getSchema()->namespace . '\\' . $this->phpName;
273
    }
274
}
275