Completed
Push — master ( 662b61...b40adf )
by George
04:45 queued 02:21
created

ModelPattern::processColumn()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 10
nc 32
nop 1
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
18
class ModelPattern
19
{
20
    public $name;
21
    public $rules;
22
    public $fillable;
23
    public $dates;
24
    public $touches;
25
    public $casts;
26
    protected $softDeletes;
27
    protected $migration;
28
    public $columns;
29
    protected $files;
30
31
    public function __construct()
32
    {
33
        $this->files = new Filesystem();
34
    }
35
36
    public static function make(string $migration) {
37
        $model = new ModelPattern();
38
        $model->migration = $migration;
39
        $model->parseModelName()
40
            ->parseColumns()
41
            ->processColumns();
42
        return $model;
43
    }
44
45
    private function parseModelName()
46
    {
47
        preg_match("/(?<=create\(\')(.*)(?=\')/", $this->migration, $result);
48
        $this->name = Str::singular(Str::studly($result[0]));
49
        return $this;
50
    }
51
52
    private function parseColumns()
53
    {
54
        preg_match_all('/(?<=\$table\-\>)(.*)(?=\;)/', $this->migration, $lines);
55
        foreach ($lines[0] as $line) {
56
            preg_match("([^\(]+)", $line, $type);
57
            switch ($type[0]) {
58
                case 'timestamps':
59
                    $this->columns[] = Column::make('created_at', 'timestamp');
60
                    $this->columns[] = Column::make('updated_at', 'timestamp');
61
                    break;
62
                case 'softDeletes':
63
                    $this->softDeletes = true;
64
                    $this->columns[] = Column::make('deleted_at', 'timestamp');
65
                    break;
66
                default:
67
                    preg_match("/['](.*?)[']/", $line, $name);
68
                    $this->columns[] = Column::make($name[1], $type[0]);
69
            }
70
        }
71
        return $this;
72
    }
73
74
    private function processColumns() {
75
        foreach($this->columns as $column) {
76
            $this->processColumn($column);
77
        }
78
    }
79
80
    private function processColumn($column) {
81
        if (!empty($column->config['fillable'])) {
82
            $this->fillable[] = $column->name;
83
        }
84
85
        if (!empty($column->config['dates'])) {
86
            $this->dates[] = $column->name;
87
        }
88
89
        if (!empty($column->config['touches'])) {
90
            $this->touches[] = $column->name;
91
        }
92
93
        if (!empty($column->config['casts'])) {
94
            $this->casts[$column->name] = $column->config['casts'];
95
        }
96
        if (!empty($column->config['rules'])) {
97
            $this->rules[$column->name] = $column->config['rules'];
98
        }
99
    }
100
}