Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.44% covered (success)
94.44%
17 / 18
85.71% covered (warning)
85.71%
6 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ModelGen
94.44% covered (success)
94.44%
17 / 18
85.71% covered (warning)
85.71%
6 / 7
16.04
0.00% covered (danger)
0.00%
0 / 1
 getModelName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUsesModels
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSoftDelete
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getFillable
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getBelongsTo
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getHasMany
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 getCasts
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
4.25
1<?php
2
3namespace San\Crud\Generators;
4
5use San\Crud\Utils\NameUtils;
6use San\Crud\Utils\SchemaUtils;
7
8class ModelGen extends BaseGen {
9
10    public function getModelName() {
11        return NameUtils::getModelName($this->mainTable());
12    }
13
14    public function getUsesModels() {
15        return join("\n", array_map(fn($table) => sprintf('use App\Models\%s;', NameUtils::getModelName((array) $table)), $this->tables));
16    }
17
18    public function getSoftDelete() {
19        return $this->hasSoftDeletes() ? 'use \Illuminate\Database\Eloquent\SoftDeletes;' : '';
20    }
21
22    public function getFillable() {
23        foreach ($this->getFillableFields() as $field) {
24            $fillable[] = $field['id'];
25        }
26
27        return join(', ', array_map(fn($f) => sprintf('"%s"', $f), $fillable ?? []));
28    }
29
30    public function getBelongsTo() {
31        foreach (SchemaUtils::getTableFieldsWithIds($this->mainTable(), ['user_id']) as $field) {
32            $relations[] = sprintf("public function %s() {\n\t\treturn \$this->belongsTo(%s::class);\n\t}", $field['relation'], NameUtils::getModelName((array) $field['related_table']));
33        }
34
35        return join("\n\n", $relations ?? []);
36    }
37
38    public function getHasMany() {
39        foreach (SchemaUtils::getTables($this->mainTable()) as $table) {
40            foreach (SchemaUtils::getTableFieldsWithIds($table, ['user_id']) as $field) {
41                if ($field['related_table'] == $this->mainTable()) {
42                    $relations[] = sprintf("public function %s() {\n\t\treturn \$this->hasMany(%s::class);\n\t}", NameUtils::getVariableNamePlural($table), NameUtils::getModelName((array) $table));
43                }
44            }
45        }
46
47        return join("\n\n", $relations ?? []);
48    }
49
50    public function getCasts() {
51        foreach (SchemaUtils::getTableFields($this->mainTable(), ['user_id']) as $field) {
52            if ($field['type'] == 'json') {
53                $casts[] = sprintf("'%s' => AsArrayObject::class", $field['id']);
54            }
55        }
56
57        return !empty($casts) ? sprintf("protected \$casts = [\n\t\t%s\n\t];", join(",\n\t\t", $casts)) : '';
58    }
59}