Passed
Branch dev (c7b1a7)
by 世昌
03:16
created

TableStruct   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 51
c 2
b 0
f 0
dl 0
loc 229
rs 9.92
wmc 31

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getFieldsName() 0 3 1
A newField() 0 3 1
A getIterator() 0 3 1
A getField() 0 3 1
A outputName() 0 6 2
A field() 0 8 2
A isSubOf() 0 10 4
A __construct() 0 5 1
A inputName() 0 6 2
A setName() 0 3 1
A all() 0 3 1
A getName() 0 3 1
A getRealTableName() 0 5 2
A setIsRawName() 0 3 1
A addField() 0 8 2
A fields() 0 9 4
A hasField() 0 12 4
1
<?php
2
3
namespace suda\database\struct;
4
5
use suda\database\connection\Connection;
6
use function array_key_exists;
7
use function array_search;
8
use ArrayIterator;
9
use IteratorAggregate;
10
use Traversable;
11
12
/**
13
 * Class TableStruct
14
 * @package suda\database\struct
15
 */
16
class TableStruct implements IteratorAggregate
17
{
18
    /**
19
     * 数据表名
20
     *
21
     * @var string
22
     */
23
    protected $name;
24
25
    /**
26
     * 是否为原始表名
27
     *
28
     * @var bool
29
     */
30
    protected $isRawName;
31
32
    /**
33
     * 字段集合
34
     *
35
     * @var Field[]
36
     */
37
    protected $fields;
38
39
    /**
40
     * 键值对映射
41
     *
42
     * @var array
43
     */
44
    protected $alias;
45
46
    /**
47
     * 创建字段集合
48
     *
49
     * @param string $name
50
     * @param bool $raw
51
     */
52
    public function __construct(string $name, bool $raw = false)
53
    {
54
        $this->name = $name;
55
        $this->isRawName = $raw;
56
        $this->fields = [];
57
    }
58
59
    /**
60
     * 新建表列
61
     *
62
     * @param string $name
63
     * @param string $type
64
     * @param int|array $length
65
     * @return Field
66
     */
67
    public function field(string $name, string $type, $length = null)
68
    {
69
        if ($length === null) {
70
            $this->fields[$name] = new Field($this->name, $name, $type);
71
        } else {
72
            $this->fields[$name] = new Field($this->name, $name, $type, $length);
73
        }
74
        return $this->fields[$name];
75
    }
76
77
    /**
78
     * @param string $name
79
     * @param string $type
80
     * @param mixed $length
81
     * @return Field
82
     */
83
    public function newField(string $name, string $type, $length = null)
84
    {
85
        return $this->field($name, $type, $length);
86
    }
87
88
    /**
89
     * @param string $name
90
     * @return Field|null
91
     */
92
    public function getField(string $name)
93
    {
94
        return $this->fields[$name] ?? null;
95
    }
96
97
    /**
98
     * 添加表结构字段
99
     *
100
     * @param array|Field $fields
101
     * @return $this
102
     */
103
    public function fields($fields)
104
    {
105
        if (!is_array($fields) && $fields instanceof Field) {
106
            $fields = func_get_args();
107
        }
108
        foreach ($fields as $field) {
109
            $this->addField($field);
110
        }
111
        return $this;
112
    }
113
114
    /**
115
     * @param string $name
116
     * @param string|null $type
117
     * @param int|string|array|null $length
118
     * @return bool
119
     */
120
    public function hasField(string $name, string $type = null, $length = null)
121
    {
122
        // 检查字段
123
        if (array_key_exists($name, $this->fields) === false) {
124
            return false;
125
        }
126
        // 检查类型
127
        if ($type !== null
128
            && $this->fields[$name]->equals(new Field($this->getName(), $name, $type, $length)) === false) {
129
            return false;
130
        }
131
        return true;
132
    }
133
134
    /**
135
     * @param Field $field
136
     */
137
    public function addField(Field $field)
138
    {
139
        if ($field->getTableName() != $this->name) {
140
            return;
141
        }
142
        $name = $field->getName();
143
        $this->fields[$name] = $field;
144
        $this->alias[$name] = $field->getAlias();
145
    }
146
147
    /**
148
     * @param string $name
149
     * @return string
150
     */
151
    public function outputName(string $name): string
152
    {
153
        if (array_key_exists($name, $this->alias)) {
154
            return $this->alias[$name];
155
        }
156
        return $name;
157
    }
158
159
    /**
160
     * @param string $name
161
     * @return string
162
     */
163
    public function inputName(string $name): string
164
    {
165
        if ($key = array_search($name, $this->alias)) {
166
            return $key;
167
        }
168
        return $name;
169
    }
170
171
    /**
172
     * @return array
173
     */
174
    public function getFieldsName()
175
    {
176
        return array_keys($this->fields);
177
    }
178
179
    /**
180
     * Get the value of name
181
     */
182
    public function getName()
183
    {
184
        return $this->name;
185
    }
186
187
    /**
188
     * @param string $name
189
     */
190
    public function setName(string $name): void
191
    {
192
        $this->name = $name;
193
    }
194
195
    /**
196
     * @param bool $isRawName
197
     */
198
    public function setIsRawName(bool $isRawName): void
199
    {
200
        $this->isRawName = $isRawName;
201
    }
202
203
    /**
204
     * Get the value of fields
205
     */
206
    public function all()
207
    {
208
        return $this->fields;
209
    }
210
211
    /**
212
     * @return ArrayIterator|Traversable
213
     */
214
    public function getIterator()
215
    {
216
        return new ArrayIterator($this->fields);
217
    }
218
219
    /**
220
     * @param TableStruct $some
221
     * @param TableStruct $struct
222
     * @return bool
223
     */
224
    public static function isSubOf(TableStruct $some, TableStruct $struct)
225
    {
226
        foreach ($struct->fields as $field) {
227
            $name = $field->getName();
228
            if (array_key_exists($name, $some->fields) === false
229
                || $some->fields[$name]->equals($field) === false) {
230
                return false;
231
            }
232
        }
233
        return true;
234
    }
235
236
    /**
237
     * @param Connection $connection
238
     * @return string
239
     */
240
    public function getRealTableName(Connection $connection): string  {
241
        if ($this->isRawName) {
242
            return $this->getName();
243
        }
244
        return $connection->rawTableName($this->getName());
245
    }
246
}
247