1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Salah3id\Domains\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
|
|
|
public function __construct($name) |
53
|
|
|
{ |
54
|
|
|
$this->name = $name; |
55
|
|
|
$this->data = $this->fetchData(); |
56
|
|
|
} |
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
|
|
|
public function getAction() |
74
|
|
|
{ |
75
|
|
|
return head($this->data); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the table will be used. |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getTableName() |
84
|
|
|
{ |
85
|
|
|
$matches = array_reverse($this->getMatches()); |
86
|
|
|
|
87
|
|
|
return array_shift($matches); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get matches data from regex. |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function getMatches() |
96
|
|
|
{ |
97
|
|
|
preg_match($this->getPattern(), $this->name, $matches); |
98
|
|
|
|
99
|
|
|
return $matches; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get name pattern. |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getPattern() |
108
|
|
|
{ |
109
|
|
|
switch ($action = $this->getAction()) { |
110
|
|
|
case 'add': |
111
|
|
|
case 'append': |
112
|
|
|
case 'update': |
113
|
|
|
case 'insert': |
114
|
|
|
return "/{$action}_(.*)_to_(.*)_table/"; |
115
|
|
|
|
116
|
|
|
break; |
|
|
|
|
117
|
|
|
|
118
|
|
|
case 'delete': |
119
|
|
|
case 'remove': |
120
|
|
|
case 'alter': |
121
|
|
|
return "/{$action}_(.*)_from_(.*)_table/"; |
122
|
|
|
|
123
|
|
|
break; |
124
|
|
|
|
125
|
|
|
default: |
126
|
|
|
return "/{$action}_(.*)_table/"; |
127
|
|
|
|
128
|
|
|
break; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Fetch the migration name to an array data. |
134
|
|
|
* |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
protected function fetchData() |
138
|
|
|
{ |
139
|
|
|
return explode('_', $this->name); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get the array data. |
144
|
|
|
* |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
|
|
public function getData() |
148
|
|
|
{ |
149
|
|
|
return $this->data; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Determine whether the given type is same with the current schema action or type. |
154
|
|
|
* |
155
|
|
|
* @param $type |
156
|
|
|
* |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
|
|
public function is($type) |
160
|
|
|
{ |
161
|
|
|
return $type === $this->getAction(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Determine whether the current schema action is a adding action. |
166
|
|
|
* |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
|
|
public function isAdd() |
170
|
|
|
{ |
171
|
|
|
return in_array($this->getAction(), $this->actions['add']); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Determine whether the current schema action is a deleting action. |
176
|
|
|
* |
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
|
|
public function isDelete() |
180
|
|
|
{ |
181
|
|
|
return in_array($this->getAction(), $this->actions['delete']); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Determine whether the current schema action is a creating action. |
186
|
|
|
* |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
|
|
public function isCreate() |
190
|
|
|
{ |
191
|
|
|
return in_array($this->getAction(), $this->actions['create']); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Determine whether the current schema action is a dropping action. |
196
|
|
|
* |
197
|
|
|
* @return bool |
198
|
|
|
*/ |
199
|
|
|
public function isDrop() |
200
|
|
|
{ |
201
|
|
|
return in_array($this->getAction(), $this->actions['drop']); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
The
break
statement is not necessary if it is preceded for example by areturn
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.