Passed
Push — master ( b40adf...1b4971 )
by George
02:45
created

ModelPattern   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 18
dl 0
loc 105
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A parseModelName() 0 5 1
A make() 0 11 1
A __construct() 0 3 1
A arrayStringify() 0 13 2
A __get() 0 2 1
A parseColumns() 0 20 4
A processColumns() 0 3 2
B processColumn() 0 18 6
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
The property namespace does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
46
        $model->fqname = $model->namespace . "\\" . $model->name;
0 ignored issues
show
Bug Best Practice introduced by
The property fqname does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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
        return $this;
77
    }
78
79
    private function processColumns() {
80
        foreach($this->columns as $column) {
81
            $this->processColumn($column);
82
        }
83
    }
84
85
    private function processColumn($column) {
86
        if (!empty($column->config['fillable'])) {
87
            $this->fillable[] = $column->name;
88
        }
89
90
        if (!empty($column->config['dates'])) {
91
            $this->dates[] = $column->name;
92
        }
93
94
        if (!empty($column->config['touches'])) {
95
            $this->touches[] = $column->name;
96
        }
97
98
        if (!empty($column->config['casts'])) {
99
            $this->casts[$column->name] = $column->config['casts'];
100
        }
101
        if (!empty($column->config['rules'])) {
102
            $this->rules[$column->name] = $column->config['rules'];
103
        }
104
    }
105
106
    public function __get($key) {
107
        return self::arrayStringify($this->{$key});
108
    }
109
110
111
    public static function arrayStringify($array)
112
    {
113
        if(count($array) > 0) {
114
            $export = str_replace(['array (', ')', '&#40', '&#41'], ['[', ']', '(', ')'], var_export($array, true));
115
            $export = preg_replace("/ => \n[^\S\n]*\[/m", ' => [', $export);
116
            $export = preg_replace("/ => \[\n[^\S\n]*\]/m", ' => []', $export);
117
            $export = preg_replace('/[\r\n]+/', "\n", $export);
118
            $export = preg_replace('/[ \t]+/', ' ', $export);
119
            $export = preg_replace('/\d\s=>\s/', '', $export);
120
            $export = preg_replace('/\r\n|\r|\n/', '', $export);
121
            return $export; //don't fear the trailing comma
122
        }
123
        return null;
124
    }
125
}