1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dami\Migration; |
4
|
|
|
|
5
|
|
|
use Stringy\StaticStringy as S; |
6
|
|
|
|
7
|
|
|
class MigrationNameParser |
8
|
|
|
{ |
9
|
|
|
private $migrationName; |
10
|
|
|
private $action; |
11
|
|
|
private $actionObject; |
12
|
|
|
private $model; |
13
|
|
|
private $validActions = array('create', 'add', 'drop', 'remove'); |
14
|
|
|
private $validActionObjects = array('table', 'column', 'index'); |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Sets a migration name. |
18
|
|
|
* |
19
|
|
|
* @param string $migrationName A migration name. |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
|
|
public function setMigrationName($migrationName) |
24
|
|
|
{ |
25
|
|
|
$this->migrationName = $migrationName; |
26
|
|
|
$migrationNameAsUnderscoreSrtring = S::underscored($migrationName); |
27
|
|
|
$items = explode('_', $migrationNameAsUnderscoreSrtring); |
28
|
|
|
if (count($items) >= 3) { |
29
|
|
|
$this->action = in_array($items[0], $this->validActions) |
30
|
|
|
? $items[0] |
31
|
|
|
: null; |
32
|
|
|
$this->actionObject = in_array($items[1], $this->validActionObjects) |
33
|
|
|
? $items[1] |
34
|
|
|
: null; |
35
|
|
|
$this->model = str_replace($items[0] . '_' . $items[1] . '_', '', $migrationNameAsUnderscoreSrtring); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Gets an action name of migration. |
41
|
|
|
* |
42
|
|
|
* @return string Action name. |
43
|
|
|
*/ |
44
|
|
|
public function getAction() |
45
|
|
|
{ |
46
|
|
|
return $this->action; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Gets an object of action. |
51
|
|
|
* |
52
|
|
|
* @return string Object action name. |
53
|
|
|
*/ |
54
|
|
|
public function getActionObject() |
55
|
|
|
{ |
56
|
|
|
return $this->actionObject; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Gets a model name of migration. |
61
|
|
|
* |
62
|
|
|
* @return string Model name. |
63
|
|
|
*/ |
64
|
|
|
public function getModel() |
65
|
|
|
{ |
66
|
|
|
return $this->model; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|