1
+ <?php
2
+
3
+ namespace Blueprint \Generators ;
4
+
5
+ use Blueprint \Column ;
6
+ use Blueprint \Model ;
7
+
8
+ class ModelGenerator
9
+ {
10
+ public function output (array $ tree )
11
+ {
12
+ // TODO: what if changing an existing model
13
+ $ stub = file_get_contents ('stubs/model/class.stub ' );
14
+
15
+ /** @var \Blueprint\Model $model */
16
+ foreach ($ tree ['models ' ] as $ model ) {
17
+ file_put_contents (
18
+ $ this ->getPath ($ model ),
19
+ $ this ->populateStub ($ stub , $ model )
20
+ );
21
+ }
22
+ }
23
+
24
+ protected function populateStub (string $ stub , Model $ model )
25
+ {
26
+ $ stub = str_replace ('DummyNamespace ' , 'App ' , $ stub );
27
+ $ stub = str_replace ('DummyClass ' , $ model ->name (), $ stub );
28
+ $ stub = str_replace ('// properties... ' , $ this ->buildProperties ($ model ), $ stub );
29
+
30
+ return $ stub ;
31
+ }
32
+
33
+ private function buildProperties (Model $ model )
34
+ {
35
+ $ properties = '' ;
36
+
37
+ $ property = $ this ->fillableColumns ($ model ->columns ());
38
+ if (!empty ($ property )) {
39
+ $ properties .= str_replace ('[] ' , $ this ->pretty_print_array ($ property , false ), $ this ->propertyStub ('fillable ' ));
40
+ }
41
+
42
+ $ property = $ this ->castableColumns ($ model ->columns ());
43
+ if (!empty ($ property )) {
44
+ $ properties .= str_replace ('[] ' , $ this ->pretty_print_array ($ property ), $ this ->propertyStub ('casts ' ));
45
+ }
46
+
47
+ $ property = $ this ->dateColumns ($ model ->columns ());
48
+ if (!empty ($ property )) {
49
+ $ properties .= str_replace ('[] ' , $ this ->pretty_print_array ($ property , false ), $ this ->propertyStub ('dates ' ));
50
+ }
51
+
52
+ return $ properties ;
53
+ }
54
+
55
+ protected function getPath (Model $ model )
56
+ {
57
+ return 'build/ ' . $ model ->name () . '.php ' ;
58
+ }
59
+
60
+ private function fillableColumns (array $ columns )
61
+ {
62
+ return array_diff (array_keys ($ columns ), [
63
+ 'id ' ,
64
+ 'password ' ,
65
+ 'deleted_at ' ,
66
+ 'created_at ' ,
67
+ 'updated_at '
68
+ ]);
69
+ }
70
+
71
+ private function castableColumns (array $ columns )
72
+ {
73
+ return array_filter (array_map (
74
+ function (Column $ column ) {
75
+ return $ this ->castForColumn ($ column );
76
+ }, $ columns ));
77
+ }
78
+
79
+ private function dateColumns (array $ columns )
80
+ {
81
+ return array_map (
82
+ function (Column $ column ) {
83
+ return $ column ->name ();
84
+ },
85
+ array_filter ($ columns , function (Column $ column ) {
86
+ return stripos ($ column ->dataType (), 'datetime ' ) !== false
87
+ || stripos ($ column ->dataType (), 'timestamp ' ) !== false ;
88
+ }));
89
+ }
90
+
91
+ private function castForColumn (Column $ column )
92
+ {
93
+ if (stripos ($ column ->dataType (), 'integer ' ) || $ column ->dataType () === 'id ' ) {
94
+ return 'integer ' ;
95
+ }
96
+
97
+ if (in_array ($ column ->dataType (), ['boolean ' , 'double ' , 'float ' ])) {
98
+ return strtolower ($ column ->dataType ());
99
+ }
100
+
101
+ if (in_array ($ column ->dataType (), ['decimal ' , 'unsignedDecimal ' ])) {
102
+ if ($ column ->attributes ()) {
103
+ return 'decimal: ' . $ column ->attributes ()[1 ];
104
+ }
105
+
106
+ return 'decimal ' ;
107
+ }
108
+
109
+ return null ;
110
+ }
111
+
112
+ private function pretty_print_array (array $ data , $ assoc = true )
113
+ {
114
+ $ output = var_export ($ data , true );
115
+ $ output = preg_replace (['/^array\s\(/ ' , "/\)$/ " ], ['[ ' , '] ' ], $ output );
116
+
117
+ if (!$ assoc ) {
118
+ $ output = preg_replace ('/^(\s+)[^=]+=>\s+/m ' , '$1 ' , $ output );
119
+ }
120
+
121
+ return $ output ;
122
+ }
123
+
124
+ private function propertyStub (string $ stub )
125
+ {
126
+ static $ stubs = [];
127
+
128
+ if (empty ($ stubs [$ stub ])) {
129
+ $ stubs [$ stub ] = file_get_contents ('stubs/model/ ' . $ stub .'.stub ' );
130
+ }
131
+
132
+ return $ stubs [$ stub ];
133
+ }
134
+ }
0 commit comments