Completed
Push — master ( 0db1d4...415713 )
by Guillaume
01:51
created

MysqlDatabaseColumn::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Starkerxp\DatabaseChecker\Structure;
4
5
6
use Starkerxp\DatabaseChecker\Exception\TablenameHasNotDefinedException;
7
use Starkerxp\DatabaseChecker\LoggerTrait;
8
9
class MysqlDatabaseColumn implements DatabaseInterface
10
{
11
    use LoggerTrait;
12
13
    private $table;
14
    private $name;
15
    private $type;
16
    private $length;
17
    private $nullable;
18
    private $defaultValue;
19
    private $extra;
20
    private $collate;
21
22
    /**
23
     * DatabaseColumnStructure constructor.
24
     *
25
     * @param $name
26
     *
27
     * @param $type
28
     * @param $length
29
     * @param $nullable
30
     * @param $defaultValue
31
     * @param $extra
32
     *
33
     * @throws \RuntimeException
34
     */
35
    public function __construct($name, $type, $length, $nullable, $defaultValue, $extra)
36
    {
37
        if (empty($name)) {
38
            throw new \RuntimeException('');
39
        }
40
        $this->name = $name;
41
        $this->setType($type);
42
        $this->length = $length;
43
        $this->nullable = $nullable;
44
        $this->defaultValue = $defaultValue;
45
        $this->extra = $extra;
46
    }
47
48
    private function setType($type)
49
    {
50
        $type = strtolower($type);
51
        $this->type = $type;
52
    }
53
54
    public function optimizeType()
55
    {
56
        $isEnum = explode('enum', $this->type);
57
        if (!empty($isEnum)) {
58
            $numberElements = substr_count(str_replace(['(', ')', "'",], '', $isEnum[1]), ',') + 1;
59
            if ($numberElements == 2) {
60
                $this->type = 'tinyint';
61
                $this->length = 1;
62
            }
63
        }
64
    }
65
66
    public function toArray()
67
    {
68
        $tmp = get_object_vars($this);
69
        unset($tmp['logger']);
70
        return $tmp;
71
    }
72
73
    /**
74
     * @return array
75
     *
76
     * @throws TablenameHasNotDefinedException
77
     */
78
    public function createStatement()
79
    {
80
        $null = $this->getNullable() ? '' : 'NOT';
81
        $default = $this->getDefaultValue() == false ? '' : ' DEFAULT ' . $this->getDefaultValue();
82
        $collate = $this->getCollate() == '' ? '' : sprintf("COLLATE '%s'", $this->getCollate());
83
        $modification = sprintf('ALTER TABLE `%s` ADD COLUMN `%s` %s %s NULL %s %s %s;', $this->getTable(), $this->getName(), $this->getColonneType(), $null, $default, $this->getExtra(), $collate);
84
85
        return [str_replace(['   ', '  ',], ' ', $modification)];
86
    }
87
88
    /**
89
     * @return mixed
90
     */
91
    public function getNullable()
92
    {
93
        return $this->nullable;
94
    }
95
96
    /**
97
     * @return mixed
98
     */
99
    public function getDefaultValue()
100
    {
101
        return $this->defaultValue;
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function getCollate()
108
    {
109
        $type = $this->getType();
110
        if (!in_array($type, ['char', 'varchar', 'enum', 'longtext', 'mediumtext', 'text', 'tinytext', 'varchar'], false)) {
111
            return '';
112
        }
113
114
        return $this->collate;
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function getType()
121
    {
122
        return $this->type;
123
    }
124
125
    /**
126
     * @return mixed
127
     *
128
     * @throws TablenameHasNotDefinedException
129
     */
130
    public function getTable()
131
    {
132
        if (!$this->table) {
133
            $this->critical('You need to define name of your table');
134
            throw new TablenameHasNotDefinedException('table not defined');
135
        }
136
137
        return $this->table;
138
    }
139
140
    public function getName()
141
    {
142
        return $this->name;
143
    }
144
145
    public function getColonneType()
146
    {
147
        $baseType = $this->type;
148
        if (in_array($baseType, ['int', 'mediumint', 'tinyint', 'smallint', 'binary', 'varchar', 'bigint', 'char', 'float'], false)) {
149
            $baseType = $baseType . '(' . $this->length . ')';
150
        }
151
152
        return strtoupper($baseType);
153
    }
154
155
    /**
156
     * @return mixed
157
     */
158
    public function getExtra()
159
    {
160
        return $this->extra;
161
    }
162
163
    /**
164
     * @param string $collate
165
     */
166
    public function setCollate($collate)
167
    {
168
        $this->collate = $collate;
169
    }
170
171
    /**
172
     * @param mixed $table
173
     */
174
    public function setTable($table)
175
    {
176
        $this->table = $table;
177
    }
178
179
    /**
180
     * @return array
181
     *
182
     * @throws TablenameHasNotDefinedException
183
     */
184
    public function alterStatement()
185
    {
186
        $null = $this->getNullable() ? '' : 'NOT';
187
        $default = $this->getDefaultValue() == false ? '' : ' DEFAULT ' . $this->getDefaultValue();
188
        $columnName = '`' . $this->getName() . '`';
189
        $collate = $this->getCollate() == '' ? '' : sprintf("COLLATE '%s'", $this->getCollate());
190
        $modification = sprintf('ALTER TABLE `%s` CHANGE COLUMN %s %s %s %s NULL %s %s %s;', $this->getTable(), $columnName, $columnName, $this->getColonneType(), $null, $default, $this->getExtra(), $collate);
191
192
        return [str_replace(['   ', '  ',], ' ', $modification)];
193
    }
194
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
195