Completed
Push — master ( 625aec...8c64ad )
by Guillaume
03:33
created

MysqlDatabaseColumn::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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