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