1 | <?php |
||
2 | |||
3 | namespace CodeWdev\TableGenerator; |
||
4 | |||
5 | |||
6 | /** |
||
7 | * |
||
8 | */ |
||
9 | class TableGenerator |
||
10 | { |
||
11 | /** |
||
12 | * @var string |
||
13 | */ |
||
14 | private $table; |
||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | private $columns; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $params; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $keys; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $value; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $id; |
||
39 | |||
40 | |||
41 | private $fail; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
42 | |||
43 | |||
44 | /** |
||
45 | * @param string $table |
||
46 | * @param array $columns |
||
47 | */ |
||
48 | public function __construct(string $table, array $columns, string $id = "id") |
||
49 | { |
||
50 | $this->table = $table; |
||
51 | $this->id = $id; |
||
52 | $this->columns = $columns; |
||
53 | $this->params = $this->createParams(); |
||
54 | } |
||
55 | |||
56 | |||
57 | /** |
||
58 | * |
||
59 | */ |
||
60 | public function create():void |
||
61 | { |
||
62 | Connect::getInstance()->query("CREATE TABLE IF NOT EXISTS {$this->table} ( |
||
63 | {$this->id} INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, |
||
64 | {$this->params}, |
||
65 | created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
||
66 | updated_at TIMESTAMP ON UPDATE CURRENT_TIMESTAMP |
||
67 | ) ENGINE=INNODB, COLLATE='UTF8_GENERAL_CI';"); |
||
68 | |||
69 | return; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * |
||
74 | */ |
||
75 | public function drop():void |
||
76 | { |
||
77 | Connect::getInstance()->query("DROP TABLE IF EXISTS {$this->table}"); |
||
78 | return; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param array $columns |
||
83 | */ |
||
84 | public function addColumn(array $columns):void |
||
85 | { |
||
86 | $this->columns = $columns; |
||
87 | $this->params = $this->createParams(); |
||
88 | Connect::getInstance()->query("ALTER TABLE {$this->table} ADD COLUMN ({$this->params})"); |
||
89 | return; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param string $column |
||
94 | */ |
||
95 | public function dropColumn(string $column):void |
||
96 | { |
||
97 | $this->params = $this->createParams(); |
||
98 | Connect::getInstance()->query("ALTER TABLE {$this->table} DROP COLUMN {$column}"); |
||
99 | return; |
||
100 | } |
||
101 | |||
102 | |||
103 | /** |
||
104 | * @return string |
||
105 | */ |
||
106 | private function createParams(): string |
||
107 | { |
||
108 | $this->keys = implode(", ", array_keys($this->columns)); |
||
109 | $this->value = implode(", ", array_values($this->columns)); |
||
110 | $key = explode(",", $this->keys ); |
||
111 | $value = explode(",", $this->value); |
||
112 | |||
113 | $paramns = []; |
||
114 | for ($i=0; $i<count($key); $i++){ |
||
0 ignored issues
–
show
It seems like you are calling the size function
count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}
// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
![]() |
|||
115 | $list = "{$key[$i]} {$value[$i]}"; |
||
116 | $paramns[] = $list; |
||
117 | } |
||
118 | |||
119 | return implode(", ", array_values($paramns)); |
||
120 | } |
||
121 | |||
122 | } |