2
2
3
3
namespace orm \DataBase ;
4
4
5
-
6
- use orm \DataBase \fields \Number ;
7
5
use orm \DataBase \fields \DateTime ;
8
- use orm \DataBase \fields \PrimaryKey ;
9
6
use orm \DataBase \fields \ForeignKey ;
7
+ use orm \DataBase \fields \Number ;
8
+ use orm \DataBase \fields \PrimaryKey ;
10
9
use orm \DataBase \fields \StringField ;
11
10
12
-
13
11
/**
14
12
* Class Field
15
13
* Contains methods for describing fields and types in database
16
- * for migrations and controlling types and references in database
17
- * @package orm\DataBase
14
+ * for migrations and controlling types and references in database.
18
15
*/
19
16
class Field
20
17
{
21
-
22
18
/**
23
- * @param string $type type of number field (int, float, double)
24
- * @param int $size size of field
25
- * @param string $attribute attribute of field (binary, unsigned)
26
- * @param bool $auto_increment, auto_increment flag
19
+ * @param string $type type of number field (int, float, double)
20
+ * @param int $size size of field
21
+ * @param string $attribute attribute of field (binary, unsigned)
22
+ * @param bool $auto_increment, auto_increment flag
23
+ *
27
24
* @return \orm\DataBase\fields\Number
28
25
*/
29
- public static function number ($ type = " int " , $ size = 10 , $ attribute = "" , $ auto_increment = false )
26
+ public static function number ($ type = ' int ' , $ size = 10 , $ attribute = '' , $ auto_increment = false )
30
27
{
31
28
$ obj = new Number ();
32
29
$ obj ->type = $ type ;
33
30
$ obj ->size = $ size ;
34
31
$ obj ->attribute = $ attribute ;
35
32
$ obj ->auto_increment = $ auto_increment ;
33
+
36
34
return $ obj ;
37
35
}
38
36
39
37
/**
40
- * describe PRIMARY KEY with AUTO_INCREMENT
38
+ * describe PRIMARY KEY with AUTO_INCREMENT.
39
+ *
41
40
* @return PrimaryKey
42
41
*/
43
42
public static function primaryKey ()
44
43
{
45
44
$ obj = new PrimaryKey ();
46
- $ obj ->type = " int " ;
45
+ $ obj ->type = ' int ' ;
47
46
$ obj ->size = 10 ;
48
47
$ obj ->auto_increment = true ;
49
48
$ obj ->primary_key = true ;
49
+
50
50
return $ obj ;
51
51
}
52
52
53
53
/**
54
54
* REFERENCES:
55
55
* ON DELETE (RESTRICT, CASCADE, NO ACTION, SET NULL)
56
- * ON UPDATE (RESTRICT, CASCADE, NO ACTION, SET NULL)
56
+ * ON UPDATE (RESTRICT, CASCADE, NO ACTION, SET NULL).
57
+ *
58
+ * @param string $table -- name of table for foreign key
59
+ * @param string $field -- field in table for foreign key
60
+ * @param array $references -- array with settings of references for foreign key.
61
+ * If it not sent, will be set from default values (restrict)
57
62
*
58
- * @param string $table -- name of table for foreign key
59
- * @param string $field -- field in table for foreign key
60
- * @param array $references -- array with settings of references for foreign key.
61
- * If it not sent, will be set from default values (restrict)
62
63
* @return ForeignKey
63
64
*/
64
- public static function foreignKey ($ table , $ field , $ references = [" on_delete " => " restrict " , " on_update " => " restrict " ])
65
+ public static function foreignKey ($ table , $ field , $ references = [' on_delete ' => ' restrict ' , ' on_update ' => ' restrict ' ])
65
66
{
66
67
$ obj = new ForeignKey ();
67
68
$ obj ->table = $ table ;
68
69
$ obj ->field = $ field ;
69
- $ obj ->on_delete = $ references ["on_delete " ];
70
- $ obj ->on_update = $ references ["on_update " ];
70
+ $ obj ->on_delete = $ references ['on_delete ' ];
71
+ $ obj ->on_update = $ references ['on_update ' ];
72
+
71
73
return $ obj ;
72
74
}
73
75
74
76
/**
75
- * String field with type varchar
77
+ * String field with type varchar.
78
+ *
76
79
* @param int $length
80
+ *
77
81
* @return StringField
78
82
*/
79
83
public static function varchar ($ length = 255 )
80
84
{
81
- return self ::stringField (" varchar " , $ length );
85
+ return self ::stringField (' varchar ' , $ length );
82
86
}
83
87
84
88
/**
85
- * String field with type varchar
89
+ * String field with type varchar.
90
+ *
86
91
* @param int $length
92
+ *
87
93
* @return StringField
88
94
*/
89
95
public static function text ($ length = 65535 )
90
96
{
91
- return self ::stringField (" text " , $ length );
97
+ return self ::stringField (' text ' , $ length );
92
98
}
93
99
94
100
/**
95
- * datetime field
101
+ * datetime field.
102
+ *
96
103
* @param string $format
104
+ *
97
105
* @return DateTime
98
106
*/
99
- public static function dateTime ($ format = " %Y-%M-%d %h:%m:%s " )
107
+ public static function dateTime ($ format = ' %Y-%M-%d %h:%m:%s ' )
100
108
{
101
- return self ::dateTimeUniversal (" datetime " , $ format );
109
+ return self ::dateTimeUniversal (' datetime ' , $ format );
102
110
}
103
111
104
112
/**
105
- * date field
113
+ * date field.
114
+ *
106
115
* @param string $format
116
+ *
107
117
* @return DateTime
108
118
*/
109
- public static function date ($ format = " %Y-%M-%d " )
119
+ public static function date ($ format = ' %Y-%M-%d ' )
110
120
{
111
- return self ::dateTimeUniversal (" date " , $ format );
121
+ return self ::dateTimeUniversal (' date ' , $ format );
112
122
}
113
123
114
124
/**
115
- * time field
125
+ * time field.
126
+ *
116
127
* @param string $format
128
+ *
117
129
* @return DateTime
118
130
*/
119
- public static function time ($ format = " %h:%m:%s " )
131
+ public static function time ($ format = ' %h:%m:%s ' )
120
132
{
121
- return self ::dateTimeUniversal (" time " , $ format );
133
+ return self ::dateTimeUniversal (' time ' , $ format );
122
134
}
123
135
124
136
/**
125
- * describe universal field of datetime
137
+ * describe universal field of datetime.
138
+ *
126
139
* @param $type
127
140
* @param $format
141
+ *
128
142
* @return DateTime
129
143
*/
130
144
private static function dateTimeUniversal ($ type , $ format )
131
145
{
132
146
$ obj = new DateTime ();
133
147
$ obj ->type = $ type ;
134
148
$ obj ->format = $ format ;
149
+
135
150
return $ obj ;
136
151
}
137
152
138
153
/**
139
- * describe universal string field
154
+ * describe universal string field.
155
+ *
140
156
* @param $type
141
157
* @param $size
158
+ *
142
159
* @return StringField
143
160
*/
144
161
private static function stringField ($ type , $ size )
145
162
{
146
163
$ obj = new StringField ();
147
164
$ obj ->type = $ type ;
148
165
$ obj ->size = $ size ;
149
- $ obj ->encoding = "utf8_general_ci " ;
166
+ $ obj ->encoding = 'utf8_general_ci ' ;
167
+
150
168
return $ obj ;
151
169
}
152
-
153
- }
170
+ }
0 commit comments