Model::getFunction()   A
last analyzed

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