-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKMColumn.php
130 lines (112 loc) · 2.96 KB
/
KMColumn.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* @author kofimokome
*/
if ( ! class_exists( 'KMColumn' ) ) {
#[AllowDynamicProperties]
class KMColumn {
private $name;
private $attributes;
private $is_update;
private $is_delete;
private $is_change;
private $is_rename;
private $new_name;
private $placeholders;
public function __construct( string $name, array $attributes = [], array $extras = [] ) {
$this->name = $name;
$default_extras = array(
'is_update' => false,
'is_delete' => false,
'is_change' => false,
'is_rename' => false,
'new_name' => ''
);
$extras = array_merge( $default_extras, $extras );
$this->is_update = $extras['is_update'];
$this->is_rename = $extras['is_rename'];
$this->is_delete = $extras['is_delete'];
$this->is_change = $extras['is_change'];
$this->new_name = $extras['new_name'];
array_push( $attributes, 'NOT NULL' );
$this->attributes = $attributes;
}
/**
* @author kofimokome
* @since 1.0.0
*/
public function nullable(): KMColumn {
if ( ( $key = array_search( 'NOT NULL', $this->attributes ) ) !== false ) {
unset( $this->attributes[ $key ] );
$this->attributes = array_values( $this->attributes );
}
array_push( $this->attributes, 'NULL' );
return $this;
}
/**
* @author kofimokome
* @since 1.0.0
*/
public function unsigned(): KMColumn {
if ( ( $key = array_search( 'SIGNED', $this->attributes ) ) !== false ) {
unset( $this->attributes[ $key ] );
$this->attributes = array_values( $this->attributes );
}
array_splice( $this->attributes, 1, 0, 'UNSIGNED' );
return $this;
}
/**
* @author kofimokome
* @since 1.0.0
*/
public function primary(): KMColumn {
array_push( $this->attributes, 'PRIMARY KEY' );
return $this;
}
/**
* @author kofimokome
* @since 1.0.0
*/
public function autoIncrement(): KMColumn {
array_push( $this->attributes, 'AUTO_INCREMENT' );
return $this;
}
/**
* @author kofimokome
* @since 1.0.0
*/
public function default( $value ): KMColumn {
if ( ! is_numeric( $value ) && strlen( trim( $value ) ) == 0 ) {
return $this;
}
$this->attributes[] = 'DEFAULT';
$this->attributes[] = "'$value'";
return $this;
}
/**
* @author kofimokome
* @since 1.0.0
*/
public function toString(): string {
$attributes = implode( ' ', $this->attributes );
if ( $this->is_delete ) {
return ' DROP COLUMN `' . $this->name . '`';
} else if ( $this->is_update ) {
return ' ADD `' . $this->name . '` ' . $attributes;
} else if ( $this->is_change ) {
return ' CHANGE `' . $this->name . '` `' . $this->new_name . '` ' . $attributes;
} else if ( $this->is_rename ) {
return ' RENAME COLUMN `' . $this->name . '` TO `' . $this->new_name . '`';
} else {
return '`' . $this->name . '` ' . $attributes;
}
}
/**
* @author kofimokome
* @since 1.0.0
*/
public function getName(): string {
return $this->name;
}
}
}