Passed
Push — master ( 30c626...e66a06 )
by Lucas
01:23
created

MethodType::noAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Luke\Migration;
4
5
6
7
class MethodType implements  IMethodType
8
{
9
10
    private string $query = '';
11
12
    public function createRow(): string
13
    {
14
        return preg_replace('/\s+/',' ',trim($this->query));
15
    }
16
17
    public function id($type = "int"): static
18
    {
19
        if($type == "int"){
20
            $this->integer('id')->unsigned()->notNull()->primaryKey();
21
        }else{
22
            $this->bigint('id')->unsigned()->notNull()->primaryKey();
23
        }
24
25
        return $this;
26
    }
27
28
    public function primaryKey(): static
29
    {
30
        $this->query .= "PRIMARY KEY";
31
        return $this;
32
    }
33
34
    public function string(string $name,int $limit = 255): static
35
    {
36
        $this->query = "`$name` varchar(" . $limit . ")";
37
        return $this;
38
    }
39
40
    public function integer(string $name): static
41
    {
42
        $this->query = "`$name` INT";
43
        return $this;
44
    }
45
46
    public function timestamp(string $name): static
47
    {
48
        $this->query =  "`$name` timestamp";
49
        return $this;
50
    }
51
52
    public function bigInt(string $name): static
53
    {
54
        $this->query =  "`$name` bigInt";
55
        return $this;
56
    }
57
58
    public function datetime(string $name): static
59
    {
60
        $this->query = "`$name` datetime";
61
        return $this;
62
    }
63
64
    public function createAt(): static
65
    {
66
        $this->timestamp('created_at')->notNull()->default('CURRENT_TIMESTAMP');
67
        return $this;
68
    }
69
70
    public function updateAt(): static
71
    {
72
        $this->timestamp('updated_at')->notNull()->default('CURRENT_TIMESTAMP  ON UPDATE CURRENT_TIMESTAMP');
73
        return $this;
74
    }
75
76
    /**
77
     * @param mixed|null $value
78
     * @return $this
79
     */
80
    public function default(mixed $value = null): static
81
    {
82
        $this->query .= "DEFAULT " . $value;
83
        return $this;
84
    }
85
86
    public function notNull(): static
87
    {
88
        $this->query .= " NOT NULL ";
89
        return $this;
90
    }
91
92
    public function unique(): static
93
    {
94
        $this->query .= ' UNIQUE ';
95
        return $this;
96
    }
97
98
    public function unsigned(): static
99
    {
100
        $this->query .= ' unsigned ';
101
        return $this;
102
    }
103
104
    public function foreignKey(string $name): static
105
    {
106
        $rand = rand();
107
        $constrain = "CONSTRAINT  `".$name ."_foreignKey_" .$rand . "` ";
108
        $foreignKey = "FOREIGN KEY (`" . $name . "`)";
109
        $this->query = $constrain . $foreignKey;
110
        return $this;
111
    }
112
113
    public function references(string $name,string $row = 'id'): static
114
    {
115
        $this->query .= " REFERENCES `$name` (`$row`)";
116
        return $this;
117
    }
118
119
    public function cascadeDelete(): static
120
    {
121
        $this->query .= " ON DELETE CASCADE ";
122
        return $this;
123
    }
124
125
    public function cascadeUpdate(): static
126
    {
127
        $this->query .= " ON UPDATE CASCADE ";
128
        return $this;
129
    }
130
131
    public function cascade(): static
132
    {
133
        $this->cascadeDelete();
134
        $this->cascadeUpdate();
135
        return $this;
136
    }
137
138
    public function noAction(): static
139
    {
140
        $this->noActionDelete();
141
        $this->noActionUpdate();
142
        return $this;
143
    }
144
145
    public function noActionDelete(): static
146
    {
147
        $this->query .= " ON DELETE NO ACTION ";
148
        return $this;
149
    }
150
151
    public function noActionUpdate(): static
152
    {
153
        $this->query .= " ON UPDATE NO ACTION ";
154
        return $this;
155
    }
156
157
    /**
158
     * @param string $name
159
     * @return mixed
160
     */
161
    public function float(string $name)
162
    {
163
        $this->query = "`$name` FLOAT";
164
        return $this;
165
    }
166
167
    public function text(string $name)
168
    {
169
        $this->query = "`$name` TEXT";
170
        return $this;
171
    }
172
173
    public function autIncrement()
174
    {
175
        $this->query .= " AUTO_INCREMENT ";
176
        return $this;
177
    }
178
}