Passed
Push — main ( 0ce457...046b0d )
by Garbuz
03:13
created

Model::getFunction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

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