Completed
Push — master ( 0ff8e8...37635b )
by Ivan
03:55
created

TableColumn::getLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace vakata\database\schema;
3
4
/**
5
 * A column definition
6
 */
7
class TableColumn
8
{
9
    protected $name;
10
    protected $type;
11
    protected $btype = 'text';
12
    protected $values = [];
13
    protected $default = null;
14
    protected $comment = null;
15
    protected $nullable = false;
16
    protected $length = null;
17
18 1
    public function __construct(string $name)
19
    {
20 1
        $this->setName($name);
21 1
    }
22
23 1
    public static function fromArray(string $name, array $data = [])
24
    {
25 1
        $instance = new static($name);
26 1
        if (isset($data['Type'])) {
27 1
            $instance->setType($data['Type']);
28
        }
29 1
        if (isset($data['type'])) {
30
            $instance->setType($data['type']);
31
        }
32 1
        if (isset($data['Comment'])) {
33 1
            $instance->setComment($data['Comment']);
34
        }
35 1
        if (isset($data['comment'])) {
36
            $instance->setComment($data['comment']);
37
        }
38 1
        if (isset($data['Null']) && $data['Null'] === 'YES') {
39
            $instance->setNullable(true);
40
        }
41 1
        if (isset($data['nullable']) && is_bool($data['nullable'])) {
42
            $instance->setNullable($data['nullable']);
43
        }
44 1
        if (isset($data['Default'])) {
45
            $instance->setDefault($data['Default']);
46
        }
47 1
        if (isset($data['default'])) {
48
            $instance->setDefault($data['default']);
49
        }
50 1
        if (isset($data['length'])) {
51 1
            $instance->setLength($data['length']);
52
        }
53 1
        if ($instance->getBasicType() === 'enum' && strpos($instance->getType(), 'enum(') === 0) {
54
            $temp = array_map(function ($v) {
55
                return str_replace("''", "'", $v);
56
            }, explode("','", substr($instance->getType(), 6, -2)));
57
            $instance->setValues($temp);
58
        }
59 1
        if (isset($data['values']) && is_array($data['values'])) {
60
            $instance->setValues($data['values']);
61
        }
62 1
        if (isset($data['DATA_TYPE'])) {
63
            $instance->setType($data['DATA_TYPE']);
64
        }
65 1
        if (isset($data['NULLABLE']) && $data['NULLABLE'] !== 'N') {
66
            $instance->setNullable(true);
67
        }
68 1
        if (isset($data['DATA_DEFAULT'])) {
69
            $instance->setDefault($data['DATA_DEFAULT']);
70
        }
71 1
        return $instance;
72
    }
73
74 1
    public function getName()
75
    {
76 1
        return $this->name;
77
    }
78
    public function getType()
79
    {
80
        return $this->type;
81
    }
82
    public function getValues()
83
    {
84
        return $this->values;
85
    }
86
    public function getDefault()
87
    {
88
        return $this->default;
89
    }
90
    public function isNullable()
91
    {
92
        return $this->nullable;
93
    }
94
    public function getComment()
95
    {
96
        return $this->comment;
97
    }
98 10
    public function getBasicType()
99
    {
100 10
        return $this->btype;
101
    }
102 1
    public function setName(string $name)
103
    {
104 1
        $this->name = $name;
105 1
        return $this;
106
    }
107 1
    public function setType(string $type)
108
    {
109 1
        $this->type = $type;
110 1
        $type = strtolower($type);
111 1
        if (strpos($type, 'enum') !== false || strpos($type, 'set') !== false) {
112
            $this->btype = 'enum';
113 1
        } elseif (strpos($type, 'text') !== false || strpos($type, 'char') !== false) {
114 1
            $this->btype = 'text';
115 1
        } elseif (strpos($type, 'int') !== false || strpos($type, 'bit') !== false) {
116 1
            $this->btype = 'int';
117
        } elseif (strpos($type, 'float') !== false || strpos($type, 'double') !== false || strpos($type, 'decimal') !== false) {
118
            $this->btype = 'float';
119
        } elseif (strpos($type, 'datetime') !== false || strpos($type, 'timestamp') !== false) {
120
            $this->btype = 'datetime';
121
        } elseif (strpos($type, 'date') !== false) {
122
            $this->btype = 'date';
123
        } elseif (strpos($type, 'time') !== false) {
124
            $this->btype = 'time';
125
        } elseif (strpos($type, 'lob') !== false || strpos($type, 'binary') !== false) {
126
            $this->btype = 'blob';
127
        }
128 1
        return $this;
129
    }
130
    public function setValues(array $values)
131
    {
132
        $this->values = $values;
133
        return $this;
134
    }
135
    public function setDefault($default = null)
136
    {
137
        $this->default = $default;
138
        return $this;
139
    }
140
    public function setNullable(bool $nullable)
141
    {
142
        $this->nullable = $nullable;
143
        return $this;
144
    }
145 1
    public function setComment(string $comment)
146
    {
147 1
        $this->comment = $comment;
148 1
        return $this;
149
    }
150 6
    public function hasLength()
151
    {
152 6
        return $this->length !== null;
153
    }
154 6
    public function getLength()
155
    {
156 6
        return $this->length;
157
    }
158 1
    public function setLength($length)
159
    {
160 1
        $this->length = $length;
161 1
        return $this;
162
    }
163
}