1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Milkmeowo\Framework\Repository\Generators\Migrations; |
4
|
|
|
|
5
|
|
|
class NameParser |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* The migration name. |
9
|
|
|
* |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
protected $name; |
13
|
|
|
/** |
14
|
|
|
* The array data. |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $data = []; |
19
|
|
|
/** |
20
|
|
|
* The available schema actions. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $actions = [ |
25
|
|
|
'create' => [ |
26
|
|
|
'create', |
27
|
|
|
'make', |
28
|
|
|
], |
29
|
|
|
'delete' => [ |
30
|
|
|
'delete', |
31
|
|
|
'remove', |
32
|
|
|
], |
33
|
|
|
'add' => [ |
34
|
|
|
'add', |
35
|
|
|
'update', |
36
|
|
|
'append', |
37
|
|
|
'insert', |
38
|
|
|
], |
39
|
|
|
'drop' => [ |
40
|
|
|
'destroy', |
41
|
|
|
'drop', |
42
|
|
|
], |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The constructor. |
47
|
|
|
* |
48
|
|
|
* @param string $name |
49
|
|
|
*/ |
50
|
|
|
public function __construct($name) |
51
|
|
|
{ |
52
|
|
|
$this->name = $name; |
53
|
|
|
$this->data = $this->fetchData(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Fetch the migration name to an array data. |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
protected function fetchData() |
62
|
|
|
{ |
63
|
|
|
return explode('_', $this->name); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get original migration name. |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function getOriginalName() |
72
|
|
|
{ |
73
|
|
|
return $this->name; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get table name. |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getTable() |
82
|
|
|
{ |
83
|
|
|
return $this->getTableName(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the table will be used. |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getTableName() |
92
|
|
|
{ |
93
|
|
|
$matches = array_reverse($this->getMatches()); |
94
|
|
|
|
95
|
|
|
return array_shift($matches); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get matches data from regex. |
100
|
|
|
* |
101
|
|
|
* @return string[] |
102
|
|
|
*/ |
103
|
|
|
public function getMatches() |
104
|
|
|
{ |
105
|
|
|
preg_match($this->getPattern(), $this->name, $matches); |
106
|
|
|
|
107
|
|
|
return $matches; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get name pattern. |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function getPattern() |
116
|
|
|
{ |
117
|
|
|
switch ($action = $this->getAction()) { |
118
|
|
|
case 'add': |
119
|
|
|
case 'append': |
120
|
|
|
case 'update': |
121
|
|
|
case 'insert': |
122
|
|
|
return "/{$action}_(.*)_to_(.*)_table/"; |
123
|
|
|
break; |
|
|
|
|
124
|
|
|
|
125
|
|
|
case 'delete': |
126
|
|
|
case 'remove': |
127
|
|
|
case 'alter': |
128
|
|
|
return "/{$action}_(.*)_from_(.*)_table/"; |
129
|
|
|
break; |
|
|
|
|
130
|
|
|
default: |
131
|
|
|
return "/{$action}_(.*)_table/"; |
132
|
|
|
break; |
|
|
|
|
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get schema type or action. |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function getAction() |
142
|
|
|
{ |
143
|
|
|
return head($this->data); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get the array data. |
148
|
|
|
* |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
public function getData() |
152
|
|
|
{ |
153
|
|
|
return $this->data; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Determine whether the given type is same with the current schema action or type. |
158
|
|
|
* |
159
|
|
|
* @param $type |
160
|
|
|
* |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
|
|
public function is($type) |
164
|
|
|
{ |
165
|
|
|
return $type == $this->getAction(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Determine whether the current schema action is a adding action. |
170
|
|
|
* |
171
|
|
|
* @return bool |
172
|
|
|
*/ |
173
|
|
|
public function isAdd() |
174
|
|
|
{ |
175
|
|
|
return in_array($this->getAction(), $this->actions['add']); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Determine whether the current schema action is a deleting action. |
180
|
|
|
* |
181
|
|
|
* @return bool |
182
|
|
|
*/ |
183
|
|
|
public function isDelete() |
184
|
|
|
{ |
185
|
|
|
return in_array($this->getAction(), $this->actions['delete']); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Determine whether the current schema action is a creating action. |
190
|
|
|
* |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
|
|
public function isCreate() |
194
|
|
|
{ |
195
|
|
|
return in_array($this->getAction(), $this->actions['create']); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Determine whether the current schema action is a dropping action. |
200
|
|
|
* |
201
|
|
|
* @return bool |
202
|
|
|
*/ |
203
|
|
|
public function isDrop() |
204
|
|
|
{ |
205
|
|
|
return in_array($this->getAction(), $this->actions['drop']); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.