Completed
Push — master ( 36726b...e4d56c )
by Ivan
09:54 queued 08:29
created

TableColumn::setComment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
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 4
    public function __construct(string $name)
19
    {
20 4
        $this->setName($name);
21 4
    }
22
23 4
    public static function fromArray(string $name, array $data = [])
24
    {
25 4
        $instance = new self($name);
26 4
        if (isset($data['Type'])) {
27 2
            $instance->setType($data['Type']);
28
        }
29 4
        if (isset($data['type'])) {
30
            $instance->setType($data['type']);
31
        }
32 4
        if (isset($data['Comment'])) {
33 2
            $instance->setComment($data['Comment']);
34
        }
35 4
        if (isset($data['comment'])) {
36
            $instance->setComment($data['comment']);
37
        }
38 4
        if (isset($data['Null']) && $data['Null'] === 'YES') {
39
            $instance->setNullable(true);
40
        }
41 4
        if (isset($data['nullable']) && is_bool($data['nullable'])) {
42
            $instance->setNullable($data['nullable']);
43
        }
44 4
        if (isset($data['Default'])) {
45
            $instance->setDefault($data['Default']);
46
        }
47 4
        if (isset($data['default'])) {
48
            $instance->setDefault($data['default']);
49
        }
50 4
        if (isset($data['length'])) {
51 4
            $instance->setLength($data['length']);
52
        }
53 4
        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 4
        if (isset($data['values']) && is_array($data['values'])) {
60
            $instance->setValues($data['values']);
61
        }
62 4
        if (isset($data['DATA_TYPE'])) {
63
            $instance->setType($data['DATA_TYPE']);
64
        }
65 4
        if (isset($data['NULLABLE']) && $data['NULLABLE'] !== 'N') {
66
            $instance->setNullable(true);
67
        }
68 4
        if (isset($data['DATA_DEFAULT'])) {
69
            $instance->setDefault($data['DATA_DEFAULT']);
70
        }
71 4
        return $instance;
72
    }
73
74 4
    public function getName()
75
    {
76 4
        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 80
    public function getBasicType()
99
    {
100 80
        return $this->btype;
101
    }
102 4
    public function setName(string $name)
103
    {
104 4
        $this->name = $name;
105 4
        return $this;
106
    }
107 2
    public function setType(string $type)
108
    {
109 2
        $this->type = $type;
110 2
        $type = strtolower($type);
111 2
        if (strpos($type, 'enum') !== false || strpos($type, 'set') !== false) {
112
            $this->btype = 'enum';
113 2
        } elseif (strpos($type, 'text') !== false || strpos($type, 'char') !== false) {
114 2
            $this->btype = 'text';
115 2
        } elseif (strpos($type, 'int') !== false || strpos($type, 'bit') !== false) {
116 2
            $this->btype = 'int';
117
        } elseif (strpos($type, 'float') !== false ||
118
            strpos($type, 'double') !== false ||
119
            strpos($type, 'decimal') !== false
120
        ) {
121
            $this->btype = 'float';
122
        } elseif (strpos($type, 'datetime') !== false || strpos($type, 'timestamp') !== false) {
123
            $this->btype = 'datetime';
124
        } elseif (strpos($type, 'date') !== false) {
125
            $this->btype = 'date';
126
        } elseif (strpos($type, 'time') !== false) {
127
            $this->btype = 'time';
128
        } elseif (strpos($type, 'lob') !== false || strpos($type, 'binary') !== false) {
129
            $this->btype = 'blob';
130
        }
131 2
        return $this;
132
    }
133
    public function setValues(array $values)
134
    {
135
        $this->values = $values;
136
        return $this;
137
    }
138
    public function setDefault($default = null)
139
    {
140
        $this->default = $default;
141
        return $this;
142
    }
143
    public function setNullable(bool $nullable)
144
    {
145
        $this->nullable = $nullable;
146
        return $this;
147
    }
148 2
    public function setComment(string $comment)
149
    {
150 2
        $this->comment = $comment;
151 2
        return $this;
152
    }
153 62
    public function hasLength()
154
    {
155 62
        return $this->length !== null;
156
    }
157 48
    public function getLength()
158
    {
159 48
        return $this->length;
160
    }
161 4
    public function setLength($length)
162
    {
163 4
        $this->length = $length;
164 4
        return $this;
165
    }
166
}
167