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