| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Mtolhuys\LaravelSchematics\Actions\Resource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Mtolhuys\LaravelSchematics\Services\StubWriter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class CreateFormRequestAction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * @param $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 12 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     public function execute($request) | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |         $model = $request['name']; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |         $namespace = config('schematics.form-request-namespace'); | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |         $stub = __DIR__ . '/../../../resources/stubs/form-request.stub'; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |         $file = app_path(str_replace(['App\\', '\\'], ['', '/'], $namespace) . "/Create{$model}Request.php"); | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |         (new StubWriter($file, $stub))->write([ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |             '$namespace$' => $namespace, | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |             '$class$' => "Create{$model}Request", | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |             '$rules$' => trim($this->fieldsToRules($request['fields'])), | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param array $fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private function fieldsToRules(array $fields): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $rules = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         foreach ($fields as $index => $field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             $rule = $this->parseRule($field['type']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             if ($rule === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             if ($index === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 $rules .= "'{$field['name']}' => '{$rule}',"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 $rules .= PHP_EOL . str_repeat(' ', 12) . "'{$field['name']}' => '{$rule}',"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         return $rules; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     private function parseRule($type = null): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         if (! $type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             return 'string|max:255'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $valid = array_diff(explode('|', $type), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             'dropColumn', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             'increments', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             'renameColumn', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             'unsigned', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if (empty($valid)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         return $this->toRule(implode('|', $valid)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param string $rawRules | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     private function toRule(string $rawRules): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $translations = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             'boolean' => 'boolean', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             'dateTime' => 'date', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             'date' => 'date', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             'decimal' => 'numeric', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             'integer' => 'numeric', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             'ipAddress' => 'ip', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             'string' => 'string', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             'text' => 'string', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             'timestamp' => 'date', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             'time' => 'date', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         return str_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             array_keys($translations), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             array_values($translations), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             $rawRules | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 101 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |  |