Completed
Push — master ( c5c126...29bff4 )
by Edson
01:37
created

Table::__set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
    public function __set($name, $value)
18
    {
19
        $this->columns[$name] = $value;
20
    }
21
22
    public function __get($name)
23
    {
24
        return $this->columns[$name];
25
    }
26
27
    public function __call($name, $arguments)
28
    {
29
        $name = strtolower(str_replace('get', '', $name));
30
        return $this->$name;
31
    }
32
33
    public function increments(string $name = 'id')
34
    {
35
        $this->last = $name;
36
        $this->$name = "INT AUTO_INCREMENT";
37
        $this->pk = $name;
38
        return $this;
39
    }
40
41
    public function timestamps(string $name = 'created_at')
42
    {
43
        $this->$name = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP";
44
        return $this;
45
    }
46
47
    public function string(string $name, int $len = 255)
48
    {
49
        $this->last = $name;
50
        $this->$name = "VARCHAR($len)";
51
        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
    public function notNull()
68
    {
69
        $this->{$this->last} .= " NOT NULL";
70
        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
    public function getColumns()
93
    {
94
        return $this->columns;
95
    }
96
}
97