5
5
use Blueprint \Contracts \Generator ;
6
6
use Blueprint \Models \Column ;
7
7
use Blueprint \Models \Model ;
8
+ use Illuminate \Support \Str ;
8
9
9
10
class ModelGenerator implements Generator
10
11
{
@@ -40,7 +41,12 @@ protected function populateStub(string $stub, Model $model)
40
41
{
41
42
$ stub = str_replace ('DummyNamespace ' , 'App ' , $ stub );
42
43
$ stub = str_replace ('DummyClass ' , $ model ->name (), $ stub );
43
- $ stub = str_replace ('// properties... ' , $ this ->buildProperties ($ model ), $ stub );
44
+
45
+ $ body = $ this ->buildProperties ($ model );
46
+ $ body .= PHP_EOL . PHP_EOL ;
47
+ $ body .= $ this ->buildRelationships ($ model );
48
+
49
+ $ stub = str_replace ('// ... ' , trim ($ body ), $ stub );
44
50
$ stub = $ this ->addTraits ($ model , $ stub );
45
51
46
52
return $ stub ;
@@ -52,24 +58,52 @@ private function buildProperties(Model $model)
52
58
53
59
$ columns = $ this ->fillableColumns ($ model ->columns ());
54
60
if (!empty ($ columns )) {
55
- $ properties .= PHP_EOL . str_replace ('[] ' , $ this ->pretty_print_array ($ columns , false ), $ this ->propertyStub ('fillable ' ));
61
+ $ properties .= PHP_EOL . str_replace ('[] ' , $ this ->pretty_print_array ($ columns , false ), $ this ->getStub ('fillable ' ));
56
62
} else {
57
- $ properties .= $ this ->propertyStub ('fillable ' );
63
+ $ properties .= $ this ->getStub ('fillable ' );
58
64
}
59
65
60
66
$ columns = $ this ->castableColumns ($ model ->columns ());
61
67
if (!empty ($ columns )) {
62
- $ properties .= PHP_EOL . str_replace ('[] ' , $ this ->pretty_print_array ($ columns ), $ this ->propertyStub ('casts ' ));
68
+ $ properties .= PHP_EOL . str_replace ('[] ' , $ this ->pretty_print_array ($ columns ), $ this ->getStub ('casts ' ));
63
69
}
64
70
65
71
$ columns = $ this ->dateColumns ($ model ->columns ());
66
72
if (!empty ($ columns )) {
67
- $ properties .= PHP_EOL . str_replace ('[] ' , $ this ->pretty_print_array ($ columns , false ), $ this ->propertyStub ('dates ' ));
73
+ $ properties .= PHP_EOL . str_replace ('[] ' , $ this ->pretty_print_array ($ columns , false ), $ this ->getStub ('dates ' ));
68
74
}
69
75
70
76
return trim ($ properties );
71
77
}
72
78
79
+ private function buildRelationships (Model $ model )
80
+ {
81
+ $ columns = array_filter ($ model ->columns (), function (Column $ column ) {
82
+ return Str::endsWith ($ column ->name (), '_id ' );
83
+ });
84
+
85
+ if (empty ($ columns )) {
86
+ return '' ;
87
+ }
88
+
89
+ $ methods = '' ;
90
+ $ template = $ this ->getStub ('method ' );
91
+
92
+ /** @var Column $column */
93
+ foreach ($ columns as $ column ) {
94
+ $ name = Str::substr ($ column ->name (), 0 , -3 );
95
+ $ class = Str::studly ($ column ->attributes ()[0 ] ?? $ name );
96
+ $ relationship = sprintf ("\$this->belongsTo(\App\%s::class) " , $ class );
97
+
98
+ $ method = str_replace ('DummyName ' , Str::camel ($ name ), $ template );
99
+ $ method = str_replace ('null ' , $ relationship , $ method );
100
+
101
+ $ methods .= PHP_EOL . $ method ;
102
+ }
103
+
104
+ return $ methods ;
105
+ }
106
+
73
107
protected function getPath (Model $ model )
74
108
{
75
109
return 'app/ ' . $ model ->name () . '.php ' ;
@@ -140,7 +174,7 @@ private function pretty_print_array(array $data, $assoc = true)
140
174
return trim ($ output );
141
175
}
142
176
143
- private function propertyStub (string $ stub )
177
+ private function getStub (string $ stub )
144
178
{
145
179
static $ stubs = [];
146
180
0 commit comments