Passed
Push — main ( 25ff95...174661 )
by Garbuz
13:35
created

Model::make()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 32
ccs 24
cts 24
cp 1
rs 9.472
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GarbuzIvan\LaravelGeneratorPackage\Builder\Components;
6
7
use GarbuzIvan\LaravelGeneratorPackage\Builder\Package;
8
use GarbuzIvan\LaravelGeneratorPackage\Configuration;
9
use GarbuzIvan\LaravelGeneratorPackage\Contracts\FieldInterface;
10
11
class Model
12
{
13
    /**
14
     * @var Configuration
15
     */
16
    private Configuration $config;
17
18
    /**
19
     * @var Package
20
     */
21
    private Package $package;
22
23
    /**
24
     * FileGenerator constructor.
25
     * @param Configuration $config
26
     */
27 2
    public function __construct(Configuration $config)
28
    {
29 2
        $this->config = $config;
30 2
    }
31
32
    /**
33
     * @param Package $package
34
     * @return bool
35
     */
36 2
    public function make(Package $package): bool
37
    {
38 2
        $this->package = $package;
39 2
        $fillable = '';
40 2
        $casts = "\n\t\t'id' => 'integer',";
41 2
        $rules = '';
42 2
        $functions = '';
43 2
        $fields = $this->package->getFields();
44 2
        foreach ($fields as $field) {
45 2
            $fillable .= "\n\t\t'" . $field->getColumn() . "',";
46 2
            $casts .= "\n\t\t'" . $field->getColumn() . "' => '" . $field->getCast() . "',";
47 2
            $rules .= $this->getRules($field);
48 2
            $functions .= $this->getFunction($field);
49
        }
50 2
        $code = str_replace([
51 2
            '%MODEL%',
52
            '%TABLE%',
53
            '%FILLABLE%',
54
            '%CASTS%',
55
            '%RULES%',
56
            '%FUNCTIONS%',
57
        ], [
58 2
            $this->package->getModel(),
59 2
            $this->package->getTable(),
60 2
            $fillable,
61 2
            $casts,
62 2
            $rules,
63 2
            $functions,
64 2
        ], $this->model);
65 2
        $nameFileModel = $this->package->getModel() . '.php';
66 2
        file_put_contents($this->package->getPath('src/Models/' . $nameFileModel), $code);
67 2
        return true;
68
    }
69
70
    /**
71
     * @param FieldInterface $field
72
     * @return string
73
     */
74 2
    public function getFunction(FieldInterface $field): string
75
    {
76 2
        if (!is_null($field->getReferencesField())) {
77
            $code = str_replace([
78
                '%MODEL%',
79
                '%TABLE%',
80
                '%FIELD%',
81
                '%MANY%',
82
                '%FIELD_LOCAL%',
83
            ], [
84
                $field->getReferencesModel(),
85
                $field->getReferencesTable(),
86
                $field->getReferencesField(),
87
                $field->getReferencesHas(),
88
                $field->getColumn(),
89
            ], $this->function);
90
            return $code;
91
        }
92 2
        return '';
93
    }
94
95
    /**
96
     * @param FieldInterface $field
97
     * @return string
98
     */
99 2
    public function getRules(FieldInterface $field): string
100
    {
101 2
        $rule = [];
102 2
        if ($field->isRequired()) {
103 2
            $rule[] = 'required';
104
        }
105 2
        if (!is_null($field->getMax())) {
106
            $rule[] = 'max:' . $field->getMax();
107
        }
108 2
        if (!is_null($field->getMin())) {
109
            $rule[] = 'min:' . $field->getMin();
110
        }
111 2
        if (in_array($field->getType(), ['integer', 'float', 'bigInteger'])) {
112
            $rule[] = 'numeric';
113
        }
114 2
        $rules = implode('|', $rule);
115 2
        if (mb_strlen($rules) > 0) {
116 2
            return "\n\t\t'" . $field->getColumn() . "' => '" . $rules . "',";
117
        }
118
        return '';
119
    }
120
121
    /**
122
     * PHP code model
123
     *
124
     * @var string
125
     */
126
    public string $model = <<<'EOD'
127
<?php
128
129
declare(strict_types=1);
130
131
namespace App\Models;
132
133
use Illuminate\Database\Eloquent\Factories\HasFactory;
134
use Illuminate\Database\Eloquent\Model;
135
136
class %MODEL% extends Model
137
{
138
    use HasFactory;
139
140
    protected $table = '%TABLE%';
141
142
    /**
143
     * @var array
144
     */
145
    protected $fillable = [ %FILLABLE%
146
    ];
147
148
    /**
149
     * The attributes that should be casted to native types.
150
     *
151
     * @var array
152
     */
153
    protected $casts = [ %CASTS%
154
    ];
155
156
    /**
157
     * Validation rules
158
     *
159
     * @var array
160
     */
161
    public static $rules = [ %RULES%
162
    ];
163
    %FUNCTIONS%
164
}
165
EOD;
166
167
    public string $function = <<<'EOD'
168
169
    /**
170
     * @return \Illuminate\Database\Eloquent\Relations\%MANY%
171
     */
172
    public function adsFave()
173
    {
174
        return $this->%MANY%('%MODEL%', '%FIELD%', '%FIELD_LOCAL%');
175
    }
176
EOD;
177
178
179
}
180