Completed
Push — master ( 193b91...6e1dd7 )
by Ivan
03:19
created

TableColumn::setValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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