1 | <?php |
||
2 | /** |
||
3 | * Created by PhpStorm. |
||
4 | * User: georg |
||
5 | * Date: 5/29/2018 |
||
6 | * Time: 11:53 AM |
||
7 | */ |
||
8 | |||
9 | namespace Ghaskell\Scaffold; |
||
10 | |||
11 | |||
12 | use Ghaskell\Scaffold\Facades\Vibro; |
||
13 | use Illuminate\Database\Migrations\Migration; |
||
14 | use Ghaskell\Scaffold\ColumnPattern as Column; |
||
15 | use Illuminate\Support\Str; |
||
16 | use Illuminate\Filesystem\Filesystem; |
||
17 | use phpDocumentor\Reflection\Types\Array_; |
||
18 | |||
19 | class ModelPattern |
||
20 | { |
||
21 | public $name; |
||
22 | protected $rules; |
||
23 | protected $fillable = []; |
||
24 | protected $dates = []; |
||
25 | protected $touches = []; |
||
26 | protected $casts = []; |
||
27 | protected $softDeletes; |
||
28 | protected $migration; |
||
29 | public $columns; |
||
30 | protected $files; |
||
31 | |||
32 | public function __construct() |
||
33 | { |
||
34 | $this->files = new Filesystem(); |
||
35 | } |
||
36 | |||
37 | public static function make(string $migration) { |
||
38 | $model = new ModelPattern(); |
||
39 | $model->migration = $migration; |
||
40 | |||
41 | $model->parseModelName() |
||
42 | ->parseColumns() |
||
43 | ->processColumns(); |
||
44 | |||
45 | $model->namespace = "App\\" . config('scaffold.files.model.path'); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
46 | $model->fqname = $model->namespace . "\\" . $model->name; |
||
0 ignored issues
–
show
|
|||
47 | return $model; |
||
48 | } |
||
49 | |||
50 | private function parseModelName() |
||
51 | { |
||
52 | preg_match("/(?<=create\(\')(.*)(?=\')/", $this->migration, $result); |
||
53 | $this->name = Str::singular(Str::studly($result[0])); |
||
54 | return $this; |
||
55 | } |
||
56 | |||
57 | private function parseColumns() |
||
58 | { |
||
59 | preg_match_all('/(?<=\$table\-\>)(.*)(?=\;)/', $this->migration, $lines); |
||
60 | foreach ($lines[0] as $line) { |
||
61 | preg_match("([^\(]+)", $line, $type); |
||
62 | switch ($type[0]) { |
||
63 | case 'timestamps': |
||
64 | $this->columns[] = Column::make('created_at', 'timestamp'); |
||
65 | $this->columns[] = Column::make('updated_at', 'timestamp'); |
||
66 | break; |
||
67 | case 'softDeletes': |
||
68 | $this->softDeletes = true; |
||
69 | $this->columns[] = Column::make('deleted_at', 'timestamp'); |
||
70 | break; |
||
71 | default: |
||
72 | preg_match("/['](.*?)[']/", $line, $name); |
||
73 | $this->columns[] = Column::make($name[1], $type[0]); |
||
74 | } |
||
75 | |||
76 | } |
||
77 | return $this; |
||
78 | } |
||
79 | |||
80 | private function processColumns() { |
||
81 | foreach($this->columns as $column) { |
||
82 | $this->processColumn($column); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | private function processColumn($column) { |
||
87 | if (!empty($column->fillable)) { |
||
88 | $this->fillable[] = $column->name; |
||
89 | } |
||
90 | |||
91 | if (!empty($column->dates)) { |
||
92 | $this->dates[] = $column->name; |
||
93 | } |
||
94 | |||
95 | if (!empty($column->touches)) { |
||
96 | $this->touches[] = $column->name; |
||
97 | } |
||
98 | |||
99 | if (!empty($column->casts)) { |
||
100 | $this->casts[$column->name] = $column->config['casts']; |
||
101 | } |
||
102 | if (!empty($column->rules)) { |
||
103 | $this->rules[$column->name] = $column->config['rules']; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | public function __get($key) { |
||
108 | return self::arrayStringify($this->{$key}); |
||
109 | } |
||
110 | |||
111 | |||
112 | public static function arrayStringify($array) |
||
113 | { |
||
114 | if(count($array) > 0) { |
||
115 | $export = str_replace(['array (', ')', '(', ')'], ['[', ']', '(', ')'], var_export($array, true)); |
||
116 | $export = preg_replace("/ => \n[^\S\n]*\[/m", ' => [', $export); |
||
117 | $export = preg_replace("/ => \[\n[^\S\n]*\]/m", ' => []', $export); |
||
118 | $export = preg_replace('/[\r\n]+/', "\n", $export); |
||
119 | $export = preg_replace('/[ \t]+/', ' ', $export); |
||
120 | $export = preg_replace('/\d\s=>\s/', '', $export); |
||
121 | $export = preg_replace('/\r\n|\r|\n/', '', $export); |
||
122 | return $export; //don't fear the trailing comma |
||
123 | } |
||
124 | return null; |
||
125 | } |
||
126 | } |