1
+ <?php
2
+
3
+ namespace Blueprint \Generators \Statements ;
4
+
5
+ use Blueprint \Column ;
6
+ use Blueprint \Contracts \Generator ;
7
+ use Blueprint \Model ;
8
+ use Blueprint \Models \Statements \ValidateStatement ;
9
+ use Blueprint \Translators \Rules ;
10
+ use Illuminate \Support \Str ;
11
+
12
+ class FormRequestGenerator implements Generator
13
+ {
14
+ private const INDENT = ' ' ;
15
+
16
+ /**
17
+ * @var \Illuminate\Contracts\Filesystem\Filesystem
18
+ */
19
+ private $ files ;
20
+
21
+ private $ models = [];
22
+
23
+ public function __construct ($ files )
24
+ {
25
+ $ this ->files = $ files ;
26
+ }
27
+
28
+ public function output (array $ tree ): array
29
+ {
30
+ $ output = [];
31
+
32
+ $ stub = $ this ->files ->get (STUBS_PATH . '/form-request.stub ' );
33
+
34
+ $ this ->registerModels ($ tree ['models ' ]);
35
+
36
+ /** @var \Blueprint\Controller $controller */
37
+ foreach ($ tree ['controllers ' ] as $ controller ) {
38
+ foreach ($ controller ->methods () as $ method => $ statements ) {
39
+ foreach ($ statements as $ statement ) {
40
+ if (!$ statement instanceof ValidateStatement) {
41
+ continue ;
42
+ }
43
+
44
+ $ context = $ this ->getContextFromController ($ controller ->name ());
45
+ $ name = $ this ->getName ($ context , $ method );
46
+ $ path = $ this ->getPath ($ name );
47
+
48
+ if ($ this ->files ->exists ($ path )) {
49
+ continue ;
50
+ }
51
+
52
+ $ this ->files ->put (
53
+ $ path ,
54
+ $ this ->populateStub ($ stub , $ name , $ context , $ statement )
55
+ );
56
+
57
+ $ output ['created ' ][] = $ path ;
58
+ }
59
+ }
60
+ }
61
+
62
+ return $ output ;
63
+ }
64
+
65
+ protected function getPath (string $ name )
66
+ {
67
+ return 'app/Http/Requests/ ' . $ name . '.php ' ;
68
+ }
69
+
70
+ protected function populateStub (string $ stub , string $ name , $ context , ValidateStatement $ validateStatement )
71
+ {
72
+ $ stub = str_replace ('DummyNamespace ' , 'App \\Http \\Requests ' , $ stub );
73
+ $ stub = str_replace ('DummyClass ' , $ name , $ stub );
74
+ $ stub = str_replace ('// rules... ' , $ this ->buildRules ($ context , $ validateStatement ), $ stub );
75
+
76
+ return $ stub ;
77
+ }
78
+
79
+ private function buildRules (string $ context , ValidateStatement $ validateStatement )
80
+ {
81
+ return trim (array_reduce ($ validateStatement ->data (), function ($ output , $ field ) use ($ context ) {
82
+ [$ qualifier , $ column ] = $ this ->splitField ($ field );
83
+
84
+ if (is_null ($ qualifier )) {
85
+ $ qualifier = $ context ;
86
+ }
87
+
88
+ $ rules = $ this ->validationRules ($ qualifier , $ column );
89
+
90
+ $ output .= self ::INDENT . "' {$ column }' => ' {$ rules }', " . PHP_EOL ;
91
+ return $ output ;
92
+ }, '' ));
93
+ }
94
+
95
+ private function getContextFromController (string $ name )
96
+ {
97
+ $ context = $ name ;
98
+
99
+ if (Str::endsWith ($ name , 'Controller ' )) {
100
+ $ context = Str::substr ($ name , 0 , -10 );
101
+ }
102
+
103
+ return Str::singular ($ context );
104
+ }
105
+
106
+ private function modelForContext (string $ context )
107
+ {
108
+ return $ this ->models [Str::studly ($ context )] ?? $ this ->models [Str::lower ($ context )];
109
+ }
110
+
111
+ private function getName (string $ context , string $ method )
112
+ {
113
+ return $ context . Str::studly ($ method ) . 'Request ' ;
114
+ }
115
+
116
+ private function splitField ($ field )
117
+ {
118
+ if (Str::contains ($ field , '. ' )) {
119
+ return explode ('. ' , $ field , 2 );
120
+ }
121
+
122
+ return [null , $ field ];
123
+ }
124
+
125
+ private function validationRules (string $ qualifier , string $ column )
126
+ {
127
+ /** @var Model $model */
128
+ $ model = $ this ->modelForContext ($ qualifier );
129
+
130
+ if (!is_null ($ model ) && $ model ->hasColumn ($ column )) {
131
+ $ column = $ model ->column ($ column );
132
+
133
+ return implode ('| ' , Rules::fromColumn ($ column ));
134
+ }
135
+
136
+ return 'required ' ;
137
+ }
138
+
139
+ private function registerModels (?array $ models )
140
+ {
141
+ $ this ->models = $ models ?? [];
142
+ }
143
+
144
+
145
+ }
0 commit comments