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