Skip to content

Commit 45b0d60

Browse files
authored
Update MigrateClass.php
1 parent 8a5e323 commit 45b0d60

File tree

1 file changed

+167
-38
lines changed

1 file changed

+167
-38
lines changed

src/MigrateClass.php

Lines changed: 167 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,235 @@
11
<?php
22
namespace Furkifor\MigrateClass;
33

4+
use mysqli;
5+
use Exception;
6+
47
/**
58
* Class MigrateClass
69
* @package Furkifor\SqlDumper
7-
* @author furkanunsal69@gmail.con
10+
* Handles database migrations.
811
*/
912
class MigrateClass
1013
{
1114
private $conn;
1215
private $table;
13-
private $columns = array();
16+
private $columns = [];
1417

1518
public function __construct($databaseType)
19+
{
20+
$this->connect($databaseType);
21+
}
22+
23+
/**
24+
* Establish a database connection based on the provided type.
25+
*
26+
* @param string $databaseType
27+
* @throws Exception
28+
*/
29+
private function connect($databaseType)
1630
{
1731
$config = parse_ini_file('config.ini');
1832
$servername = $config['servername'];
1933
$username = $config['username'];
2034
$password = $config['password'];
2135
$dbname = $config['dbname'];
22-
if ($databaseType == 'mysql') {
23-
$this->conn = new mysqli($servername, $username, $password, $dbname);
24-
} elseif ($databaseType == 'mongodb') {
25-
// Connect to MongoDB
26-
} elseif ($databaseType == 'sqlserver') {
27-
// Connect to SQL Server
36+
37+
switch ($databaseType) {
38+
case 'mysql':
39+
$this->conn = new mysqli($servername, $username, $password, $dbname);
40+
if ($this->conn->connect_error) {
41+
throw new Exception("MySQL Connection failed: " . $this->conn->connect_error);
42+
}
43+
break;
44+
case 'mongodb':
45+
// Connect to MongoDB
46+
break;
47+
case 'sqlserver':
48+
// Connect to SQL Server
49+
break;
50+
default:
51+
throw new Exception("Unsupported database type: $databaseType");
2852
}
2953
}
3054

31-
public function name($table)
55+
/**
56+
* Set the table name and initialize with an ID column.
57+
*
58+
* @param string $table
59+
* @return self
60+
*/
61+
public function name($table): self
3262
{
3363
$this->table = $table;
34-
$this->columns[] = new Column("id", "INT(11)",["AUTO_INCREMENT","PRIMARY KEY","NOT NULL"]);
64+
$this->columns[] = new Column("id", "INT(11)", ["AUTO_INCREMENT", "PRIMARY KEY", "NOT NULL"]);
65+
return $this;
66+
}
67+
68+
/**
69+
* Define a string column.
70+
*
71+
* @param string $name
72+
* @param int $length
73+
* @param array $constraints
74+
* @return self
75+
*/
76+
public function string($name, $length = 255, $constraints = []): self
77+
{
78+
$this->columns[] = new Column($name, "VARCHAR($length)", $constraints);
79+
return $this;
80+
}
81+
82+
/**
83+
* Define an integer column.
84+
*
85+
* @param string $name
86+
* @param array $constraints
87+
* @return self
88+
*/
89+
public function int($name, $constraints = []): self
90+
{
91+
$this->columns[] = new Column($name, "INT", $constraints);
3592
return $this;
3693
}
3794

38-
public function string($name, $length, $constraints=[])
95+
/**
96+
* Define a boolean column.
97+
*
98+
* @param string $name
99+
* @param array $constraints
100+
* @return self
101+
*/
102+
public function boolean($name, $constraints = []): self
39103
{
40-
$column = new Column($name, "VARCHAR($length)",$constraints);
41-
array_push($this->columns, $column);
42-
return $column;
104+
$this->columns[] = new Column($name, "TINYINT(1)", $constraints);
105+
return $this;
43106
}
44-
public function int($name, $constraints=[])
107+
108+
/**
109+
* Define a datetime column.
110+
*
111+
* @param string $name
112+
* @param array $constraints
113+
* @return self
114+
*/
115+
public function datetime($name, $constraints = []): self
45116
{
46-
$column = new Column($name, "INT",$constraints);
47-
array_push($this->columns, $column);
48-
return $column;
117+
$this->columns[] = new Column($name, "DATETIME", $constraints);
118+
return $this;
49119
}
50120

51-
public function datetime($name, $constraints=[])
121+
/**
122+
* Define a text column.
123+
*
124+
* @param string $name
125+
* @param array $constraints
126+
* @return self
127+
*/
128+
public function text($name, $constraints = []): self
52129
{
53-
$column = new Column($name, "DATETIME",$constraints);
54-
array_push($this->columns, $column);
55-
return $column;
130+
$this->columns[] = new Column($name, "TEXT", $constraints);
131+
return $this;
56132
}
57-
public function foreignKey($name,$referenceTable,$referenceColumn,$constraints=[])
133+
134+
/**
135+
* Define a foreign key column.
136+
*
137+
* @param string $name
138+
* @param string $referenceTable
139+
* @param string $referenceColumn
140+
* @param array $constraints
141+
* @return self
142+
*/
143+
public function foreignKey($name, $referenceTable, $referenceColumn, $constraints = []): self
58144
{
59-
$column = new Column($name, "INT", array_merge($constraints,["FOREIGN KEY ($name) REFERENCES $referenceTable($referenceColumn)"]));
60-
array_push($this->columns, $column);
61-
return $column;
145+
$constraint = "FOREIGN KEY ($name) REFERENCES $referenceTable($referenceColumn)";
146+
$this->columns[] = new Column($name, "INT", array_merge($constraints, [$constraint]));
147+
return $this;
62148
}
63149

64-
public function check($check)
150+
/**
151+
* Add a CHECK constraint to the last added column.
152+
*
153+
* @param string $check
154+
* @return self
155+
*/
156+
public function check($check): self
65157
{
66-
$this->columns[count($this->columns)-1]->addConstraint("CHECK ($check)");
158+
$this->columns[count($this->columns) - 1]->addConstraint("CHECK ($check)");
159+
return $this;
67160
}
68-
public function default($default)
161+
162+
/**
163+
* Add a DEFAULT constraint to the last added column.
164+
*
165+
* @param string $default
166+
* @return self
167+
*/
168+
public function default($default): self
69169
{
70-
$this->columns[count($this->columns)-1]->addConstraint("DEFAULT $default");
170+
$this->columns[count($this->columns) - 1]->addConstraint("DEFAULT $default");
171+
return $this;
71172
}
72173

73-
public function createTable()
174+
/**
175+
* Create the table based on the defined columns.
176+
*
177+
* @throws Exception
178+
*/
179+
public function createTable(): void
74180
{
75181
$columnsString = implode(',', $this->columns);
76182
$sql = "CREATE TABLE {$this->table} ($columnsString)";
183+
77184
if ($this->conn->query($sql) === TRUE) {
78185
echo "Table {$this->table} created successfully";
79186
} else {
80-
echo "Error creating table: " . $this->conn->error;
187+
throw new Exception("Error creating table: " . $this->conn->error);
81188
}
82189
}
83190
}
84191

192+
/**
193+
* Class representing a database column.
194+
*/
85195
class Column
86196
{
87197
private $name;
88198
private $type;
89-
private $constraints = array();
199+
private $constraints = [];
90200

91-
public function __construct($name, $type,$constraints)
201+
/**
202+
* Column constructor.
203+
*
204+
* @param string $name
205+
* @param string $type
206+
* @param array $constraints
207+
*/
208+
public function __construct($name, $type, $constraints = [])
92209
{
93210
$this->name = $name;
94211
$this->type = $type;
95212
$this->constraints = $constraints;
96213
}
97214

98-
public function addConstraint($constraint)
215+
/**
216+
* Add a constraint to the column.
217+
*
218+
* @param string $constraint
219+
* @return void
220+
*/
221+
public function addConstraint($constraint): void
99222
{
100-
array_push($this->constraints, $constraint);
223+
$this->constraints[] = $constraint;
101224
}
102-
public function __toString()
225+
226+
/**
227+
* Convert the column to a string representation.
228+
*
229+
* @return string
230+
*/
231+
public function __toString(): string
103232
{
104233
return "{$this->name} {$this->type} " . implode(' ', $this->constraints);
105234
}
106-
}
235+
}

0 commit comments

Comments
 (0)