Passed
Push — master ( 566a3a...fbef46 )
by 世昌
02:19
created

Field::getAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace suda\database\struct;
3
4
/**
5
 * 数据表字段创建工具
6
 * 用于创建数据表字段
7
 */
8
class Field
9
{
10
    /**
11
     * 自增 AUTO_INCREMENT
12
     *
13
     * @var bool
14
     */
15
    protected $auto;
16
    // COMMENT
17
    /**
18
     * @var
19
     */
20
    protected $comment;
21
    /**
22
     * @var
23
     */
24
    protected $key; //primary unique index
25
    // foreign key
26
    /**
27
     * @var
28
     */
29
    protected $foreign;
30
31
    /**
32
     * @var string
33
     */
34
    protected $name;
35
    /**
36
     * @var string
37
     */
38
    protected $type;
39
    /**
40
     * @var int|string|array|null
41
     */
42
    protected $length;
43
    /**
44
     * @var
45
     */
46
    protected $default;
47
    /**
48
     * @var bool
49
     */
50
    protected $hasDefault;
51
52
    /**
53
     * @var bool
54
     */
55
    protected $null = true; // isNullable
56
    /**
57
     * @var
58
     */
59
    protected $attribute; // binary unsigned
60
    /**
61
     * @var
62
     */
63
    protected $collation;
64
    /**
65
     * @var string
66
     */
67
    protected $tableName;
68
    /**
69
     * @var
70
     */
71
    protected $charset;
72
    /**
73
     * @var string
74
     */
75
    protected $alias;
76
77
    /**
78
     *
79
     */
80
    const BINARY = 'BINARY';
81
    /**
82
     *
83
     */
84
    const UNSIGNED = 'UNSIGNED';
85
86
    /**
87
     *
88
     */
89
    const UNIQUE = 'UNIQUE';
90
    /**
91
     *
92
     */
93
    const PRIMARY = 'PRIMARY';
94
    /**
95
     *
96
     */
97
    const INDEX = 'INDEX';
98
    /**
99
     *
100
     */
101
    const KEY = 'KEY';
102
103
    /**
104
     * Field constructor.
105
     * @param string $tableName
106
     * @param string $name
107
     * @param string $type
108
     * @param int|string|array|null $length
109
     */
110
    public function __construct(string $tableName, string $name, string $type, $length = null)
111
    {
112
        $this->tableName = $tableName;
113
        $this->name = $name;
114
        $this->type = strtoupper($type);
115
        $this->length = $length;
116
        $this->hasDefault = false;
117
        $this->alias = $name;
118
    }
119
120
    /**
121
     * @param string $name
122
     * @return $this
123
     */
124
    public function alias(string $name) {
125
        $this->alias = $name;
126
        return $this;
127
    }
128
129
    /**
130
     * @param string $charset
131
     * @return $this
132
     */
133
    public function charset(string $charset)
134
    {
135
        $this->charset = 'CHARACTER SET ' . $charset;
136
        return $this;
137
    }
138
139
    /**
140
     * @param string $comment
141
     * @return $this
142
     */
143
    public function comment(string $comment)
144
    {
145
        $this->comment = $comment;
146
        return $this;
147
    }
148
149
    /**
150
     * @param $length
151
     * @return $this
152
     */
153
    public function length($length)
154
    {
155
        $this->length = $length;
156
        return $this;
157
    }
158
159
    /**
160
     * @return $this
161
     */
162
    public function key()
163
    {
164
        $this->key = self::KEY;
165
        return $this;
166
    }
167
168
    /**
169
     * @return $this
170
     */
171
    public function primary()
172
    {
173
        $this->key = self::PRIMARY;
174
        return $this;
175
    }
176
177
    /**
178
     * @return $this
179
     */
180
    public function index()
181
    {
182
        $this->key = self::INDEX;
183
        return $this;
184
    }
185
186
    /**
187
     * @return $this
188
     */
189
    public function unique()
190
    {
191
        $this->key = self::UNIQUE;
192
        return $this;
193
    }
194
195
    /**
196
     * @param string $collate
197
     * @return $this
198
     */
199
    public function collate(string $collate)
200
    {
201
        $this->collation = $collate;
202
        return $this;
203
    }
204
205
    /**
206
     * @return $this
207
     */
208
    public function auto()
209
    {
210
        $this->auto = true;
211
        return $this;
212
    }
213
214
    /**
215
     * @param Field $field
216
     * @return $this
217
     */
218
    public function foreign(Field $field)
219
    {
220
        $this->foreign = $field;
221
        $this->type = $field->type;
222
        $this->length = $field->length;
223
        $this->default = null;
224
        if ($field->attribute) {
225
            $this->attribute = $field->attribute;
226
        }
227
        return $this;
228
    }
229
230
    /**
231
     * @param bool $set
232
     * @return $this
233
     */
234
    public function null(bool $set = true)
235
    {
236
        $this->null = $set;
237
        return $this;
238
    }
239
240
    /**
241
     * @param $value
242
     * @return $this
243
     */
244
    public function default($value)
245
    {
246
        $this->hasDefault = true;
247
        $this->default = $value;
248
        if (null === $value) {
249
            $this->null = true;
250
        }
251
        return $this;
252
    }
253
254
    /**
255
     * @return $this
256
     */
257
    public function binary()
258
    {
259
        $this->attribute = self::BINARY;
260
        return $this;
261
    }
262
263
    /**
264
     * @return $this
265
     */
266
    public function unsigned()
267
    {
268
        $this->attribute = self::UNSIGNED;
269
        return $this;
270
    }
271
272
273
    /**
274
     * Get the value of auto
275
     */
276
    public function getAuto()
277
    {
278
        return $this->auto;
279
    }
280
281
    /**
282
     * Get the value of comment
283
     */
284
    public function getComment()
285
    {
286
        return $this->comment;
287
    }
288
289
    /**
290
     * Get the value of key
291
     */
292
    public function getType()
293
    {
294
        return $this->key;
295
    }
296
297
    /**
298
     * Get the value of foreign
299
     */
300
    public function getForeignKey()
301
    {
302
        return $this->foreign;
303
    }
304
305
    /**
306
     * Get the value of name
307
     */
308
    public function getName()
309
    {
310
        return $this->name;
311
    }
312
313
    /**
314
     * Get the value of type
315
     */
316
    public function getValueType()
317
    {
318
        return $this->type;
319
    }
320
321
    /**
322
     * Get the value of length
323
     */
324
    public function getLength()
325
    {
326
        return $this->length;
327
    }
328
329
    /**
330
     * Get the value of default
331
     */
332
    public function getDefault()
333
    {
334
        return $this->default;
335
    }
336
337
    /**
338
     * Get the value of hasDefault
339
     */
340
    public function hasDefault()
341
    {
342
        return $this->hasDefault;
343
    }
344
345
    /**
346
     * 是否为组建
347
     *
348
     * @return boolean
349
     */
350
    public function isPrimary():bool {
351
        return  $this->key == self::PRIMARY;
352
    }
353
354
    /**
355
     * 是否为可空
356
     *
357
     * @return boolean
358
     */
359
    public function isNullable(): bool
360
    {
361
        return $this->null;
362
    }
363
364
    /**
365
     * 是否支持
366
     *
367
     * @return boolean
368
     */
369
    public function isAutoIncrement(): bool
370
    {
371
        return $this->auto === true;
372
    }
373
    
374
    /**
375
     * Get the value of attribute
376
     */
377
    public function getAttribute()
378
    {
379
        return $this->attribute;
380
    }
381
382
    /**
383
     * Get the value of collation
384
     */
385
    public function getCollation()
386
    {
387
        return $this->collation;
388
    }
389
390
    /**
391
     * Get the value of tableName
392
     */
393
    public function getTableName()
394
    {
395
        return $this->tableName;
396
    }
397
398
    /**
399
     * Get the value of charset
400
     */
401
    public function getCharset()
402
    {
403
        return $this->charset;
404
    }
405
406
    /**
407
     * Get the value of alias
408
     */ 
409
    public function getAlias()
410
    {
411
        return $this->alias ?? $this->name;
412
    }
413
}
414