1
+ <?php
2
+
3
+ namespace Blueprint \Generators ;
4
+
5
+ use Blueprint \Model ;
6
+
7
+ class FactoryGenerator
8
+ {
9
+ public function output (array $ tree )
10
+ {
11
+ // TODO: what if changing an existing model
12
+ $ stub = file_get_contents ('stubs/factory.stub ' );
13
+
14
+ /** @var \Blueprint\Model $model */
15
+ foreach ($ tree ['models ' ] as $ model ) {
16
+ file_put_contents (
17
+ $ this ->getPath ($ model ),
18
+ $ this ->populateStub ($ stub , $ model )
19
+ );
20
+ }
21
+ }
22
+
23
+ protected function getPath (Model $ model )
24
+ {
25
+ return 'build/ ' . $ model ->name () . 'Factory.php ' ;
26
+ }
27
+
28
+ protected function populateStub (string $ stub , Model $ model )
29
+ {
30
+ $ stub = str_replace ('DummyNamespace ' , 'App ' , $ stub );
31
+ $ stub = str_replace ('DummyClass ' , $ model ->name (), $ stub );
32
+ $ stub = str_replace ('// definition... ' , $ this ->buildDefinition ($ model ), $ stub );
33
+
34
+ return $ stub ;
35
+ }
36
+
37
+ protected function buildDefinition (Model $ model )
38
+ {
39
+ $ definition = '' ;
40
+
41
+ /** @var \Blueprint\Column $column */
42
+ foreach ($ model ->columns () as $ column ) {
43
+ if ($ column ->name () === 'id ' ) {
44
+ continue ;
45
+ }
46
+
47
+ $ definition .= "' {$ column ->name ()}' => " ;
48
+ $ faker = $ this ->fakerData ($ column ->name ()) ?? $ this ->fakerDataType ($ column ->dataType ());
49
+ $ definition .= '$faker-> ' . $ faker ;
50
+ $ definition .= ', ' . PHP_EOL ;
51
+ }
52
+
53
+ return trim ($ definition );
54
+ }
55
+
56
+ protected function fakerData (string $ name )
57
+ {
58
+ static $ fakeableNames = [
59
+ 'city ' => 'city ' ,
60
+ 'company ' => 'company ' ,
61
+ 'country ' => 'country ' ,
62
+ 'description ' => 'text ' ,
63
+ 'email ' => 'safeEmail ' ,
64
+ 'first_name ' => 'firstName ' ,
65
+ 'firstname ' => 'firstName ' ,
66
+ 'guid ' => 'uuid ' ,
67
+ 'last_name ' => 'lastName ' ,
68
+ 'lastname ' => 'lastName ' ,
69
+ 'lat ' => 'latitude ' ,
70
+ 'latitude ' => 'latitude ' ,
71
+ 'lng ' => 'longitude ' ,
72
+ 'longitude ' => 'longitude ' ,
73
+ 'name ' => 'name ' ,
74
+ 'password ' => 'password ' ,
75
+ 'phone ' => 'phoneNumber ' ,
76
+ 'phone_number ' => 'phoneNumber ' ,
77
+ 'postcode ' => 'postcode ' ,
78
+ 'postal_code ' => 'postcode ' ,
79
+ 'slug ' => 'slug ' ,
80
+ 'street ' => 'streetName ' ,
81
+ 'address1 ' => 'streetAddress ' ,
82
+ 'address2 ' => 'secondaryAddress ' ,
83
+ 'summary ' => 'text ' ,
84
+ 'title ' => 'sentence(4) ' ,
85
+ 'url ' => 'url ' ,
86
+ 'user_name ' => 'userName ' ,
87
+ 'username ' => 'userName ' ,
88
+ 'uuid ' => 'uuid ' ,
89
+ 'zip ' => 'postcode ' ,
90
+ ];
91
+
92
+ return $ fakeableNames [$ name ] ?? null ;
93
+ }
94
+
95
+ protected function fakerDataType (string $ type )
96
+ {
97
+ $ fakeableTypes = [
98
+ 'string ' => 'word ' ,
99
+ 'text ' => 'text ' ,
100
+ 'date ' => 'date() ' ,
101
+ 'time ' => 'time() ' ,
102
+ 'guid ' => 'word ' ,
103
+ 'datetimetz ' => 'dateTime() ' ,
104
+ 'datetime ' => 'dateTime() ' ,
105
+ 'timestamp ' => 'dateTime() ' ,
106
+ 'integer ' => 'randomNumber() ' ,
107
+ 'bigint ' => 'randomNumber() ' ,
108
+ 'smallint ' => 'randomNumber() ' ,
109
+ 'decimal ' => 'randomFloat() ' ,
110
+ 'float ' => 'randomFloat() ' ,
111
+ 'boolean ' => 'boolean '
112
+ ];
113
+
114
+ return $ fakeableTypes [$ type ] ?? null ;
115
+ }
116
+ }
0 commit comments