-
Notifications
You must be signed in to change notification settings - Fork 21
/
PivotGenerator.php
143 lines (126 loc) · 2.67 KB
/
PivotGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace Pingpong\Generators;
use Illuminate\Support\Str;
class PivotGenerator extends Generator
{
/**
* Get stub name.
*
* @var string
*/
protected $stub = 'migration/pivot';
/**
* Get base path of destination file.
*
* @return string
*/
public function getBasePath()
{
return base_path().'/database/migrations/';
}
/**
* Get destination path for generated file.
*
* @return string
*/
public function getPath()
{
return $this->getBasePath().$this->getFilename().'.php';
}
/**
* Get filename.
*
* @return string
*/
public function getFilename()
{
return date('Y_m_d_His_').$this->getMigrationName();
}
/**
* Get migration name.
*
* @return string
*/
public function getMigrationName()
{
return 'create_'.$this->getPivotTableName().'_pivot_table';
}
/**
* Get class name.
*
* @return string
*/
public function getClass()
{
return Str::studly($this->getMigrationName());
}
/**
* Get the name of the pivot table.
*
* @return string
*/
public function getPivotTableName()
{
return implode('_', array_map('str_singular', $this->getSortedTableNames()));
}
/**
* Get sorted table names.
*
* @return array
*/
public function getSortedTableNames()
{
$tables = [
strtolower($this->table_one),
strtolower($this->table_two),
];
sort($tables);
return $tables;
}
/**
* Get stub replacements.
*
* @return array
*/
public function getReplacements()
{
return array_merge(parent::getReplacements(), [
'table_one' => $this->table_one,
'table_two' => $this->table_two,
'column_one' => $this->getColumnOne(),
'column_two' => $this->getColumnTwo(),
'table_pivot' => $this->getPivotTableName(),
'timestamp' => $this->getTimestampReplacement(),
]);
}
/**
* Get replacement for TIMESTAMP.
*
* @return string|null
*/
public function getTimestampReplacement()
{
if ($this->timestamp) {
return '$table->timestamps();';
}
return;
}
/**
* Get column one.
*
* @return string
*/
public function getColumnOne()
{
return str_singular($this->table_one);
}
/**
* Get column two.
*
* @return string
*/
public function getColumnTwo()
{
return str_singular($this->table_two);
}
}