|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bonfim\ActiveRecord; |
|
4
|
|
|
|
|
5
|
|
|
final class Table |
|
6
|
|
|
{ |
|
7
|
|
|
private $columns; |
|
8
|
|
|
private $last; |
|
9
|
|
|
|
|
10
|
|
|
private $pk; |
|
11
|
|
|
private $fk; |
|
12
|
|
|
|
|
13
|
|
|
private $references = ""; |
|
14
|
|
|
private $onupdate = ""; |
|
15
|
|
|
private $ondelete = ""; |
|
16
|
|
|
|
|
17
|
30 |
|
public function __set($name, $value) |
|
18
|
|
|
{ |
|
19
|
30 |
|
$this->columns[$name] = $value; |
|
20
|
30 |
|
} |
|
21
|
|
|
|
|
22
|
30 |
|
public function __get($name) |
|
23
|
|
|
{ |
|
24
|
30 |
|
return $this->columns[$name]; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
30 |
|
public function __call($name, $arguments) |
|
28
|
|
|
{ |
|
29
|
30 |
|
$name = strtolower(str_replace('get', '', $name)); |
|
30
|
30 |
|
return $this->$name; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
30 |
|
public function increments(string $name = 'id') |
|
34
|
|
|
{ |
|
35
|
30 |
|
$this->last = $name; |
|
36
|
30 |
|
$this->$name = "INT AUTO_INCREMENT"; |
|
37
|
30 |
|
$this->pk = $name; |
|
38
|
30 |
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
30 |
|
public function timestamps(string $name = 'created_at') |
|
42
|
|
|
{ |
|
43
|
30 |
|
$this->$name = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP"; |
|
44
|
30 |
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
30 |
|
public function string(string $name, int $len = 255) |
|
48
|
|
|
{ |
|
49
|
30 |
|
$this->last = $name; |
|
50
|
30 |
|
$this->$name = "VARCHAR($len)"; |
|
51
|
30 |
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function integer($name) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->last = $name; |
|
57
|
|
|
$this->$name = "INT"; |
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function unsigned() |
|
62
|
|
|
{ |
|
63
|
|
|
$this->{$this->last} .= " unsigned"; |
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
30 |
|
public function notNull() |
|
68
|
|
|
{ |
|
69
|
30 |
|
$this->{$this->last} .= " NOT NULL"; |
|
70
|
30 |
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function references($table, $name) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->fk = "FOREIGN KEY ($this->last)"; |
|
76
|
|
|
$this->references = "REFERENCES $table($name)"; |
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function onUpdate($method) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->onupdate = "ON UPDATE $method"; |
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function onDelete($method) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->ondelete = "ON DELETE $method"; |
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
30 |
|
public function getColumns() |
|
93
|
|
|
{ |
|
94
|
30 |
|
return $this->columns; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|