Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
28 / 28 |
|
100.00% |
18 / 18 |
CRAP | |
100.00% |
1 / 1 |
| BaseGen | |
100.00% |
28 / 28 |
|
100.00% |
18 / 18 |
26 | |
100.00% |
1 / 1 |
| __construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| getVarName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getVarNamePlural | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getTitle | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getTitlePlural | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getParentVarName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getParentVarNamePlural | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasParentTable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| parentTables | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| mainTable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| parentTable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getVars | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| getRawVars | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| hasUserId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasTimestamps | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasSoftDeletes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getFirstReadableField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| getFillableFields | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| getExternallyRelatedFields | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace San\Crud\Generators; |
| 4 | |
| 5 | use Illuminate\Support\Str; |
| 6 | use San\Crud\Utils\NameUtils; |
| 7 | use San\Crud\Utils\SchemaUtils; |
| 8 | |
| 9 | class BaseGen { |
| 10 | public function __construct(public array $tables) { } |
| 11 | |
| 12 | public function getVarName() { |
| 13 | return Str::singular($this->getVarNamePlural()); |
| 14 | } |
| 15 | |
| 16 | public function getVarNamePlural() { |
| 17 | return NameUtils::getVariableNamePlural($this->mainTable()); |
| 18 | } |
| 19 | |
| 20 | public function getTitle() { |
| 21 | return Str::singular($this->getTitlePlural()); |
| 22 | } |
| 23 | |
| 24 | public function getTitlePlural() { |
| 25 | return NameUtils::titleCase($this->mainTable()); |
| 26 | } |
| 27 | |
| 28 | public function getParentVarName() { |
| 29 | return Str::singular($this->getParentVarNamePlural()); |
| 30 | } |
| 31 | |
| 32 | public function getParentVarNamePlural() { |
| 33 | return NameUtils::getVariableNamePlural($this->parentTable()); |
| 34 | } |
| 35 | |
| 36 | public function hasParentTable() { |
| 37 | return count($this->tables) > 1; |
| 38 | } |
| 39 | |
| 40 | public function parentTables() { |
| 41 | return array_slice($this->tables, 0, -1); |
| 42 | } |
| 43 | |
| 44 | public function mainTable() { |
| 45 | return $this->tables[count($this->tables) - 1]; |
| 46 | } |
| 47 | |
| 48 | public function parentTable() { |
| 49 | return $this->tables[count($this->tables) - 2]; |
| 50 | } |
| 51 | |
| 52 | public function getVars(array $tables, array $extraVars = []) { |
| 53 | $vars = $this->getRawVars($tables, $extraVars); |
| 54 | |
| 55 | return !empty($vars) ? sprintf("compact(%s)", join(', ', array_map(fn($var) => sprintf("'%s'", $var), $vars))) : '[]'; |
| 56 | } |
| 57 | |
| 58 | public function getRawVars(array $tables, ?array $extraVars = []) { |
| 59 | foreach ($tables as $table) { |
| 60 | $vars[] = NameUtils::getVariableName($table); |
| 61 | } |
| 62 | |
| 63 | return array_merge($vars ?? [], (array) $extraVars); |
| 64 | } |
| 65 | |
| 66 | protected function hasUserId() { |
| 67 | return SchemaUtils::getUserIdField($this->mainTable()); |
| 68 | } |
| 69 | |
| 70 | protected function hasTimestamps() { |
| 71 | return SchemaUtils::hasTimestamps($this->mainTable()); |
| 72 | } |
| 73 | |
| 74 | protected function hasSoftDeletes() { |
| 75 | return SchemaUtils::hasSoftDelete($this->mainTable()); |
| 76 | } |
| 77 | |
| 78 | protected function getFirstReadableField($key = NULL) { |
| 79 | return SchemaUtils::firstHumanReadableField($this->mainTable(), $key) ?: 'id'; |
| 80 | } |
| 81 | |
| 82 | protected function getFillableFields($exceptColumns = ['user_id']) { |
| 83 | foreach (SchemaUtils::getTableFields($this->mainTable(), $exceptColumns) as $field) { |
| 84 | if (in_array($field['related_table'] ?? '', $this->tables)) continue; |
| 85 | $fillable[] = $field; |
| 86 | } |
| 87 | |
| 88 | return $fillable ?? []; |
| 89 | } |
| 90 | |
| 91 | protected function getExternallyRelatedFields() { |
| 92 | $fields = SchemaUtils::getTableFieldsWithIds($this->mainTable(), ['user_id']); |
| 93 | |
| 94 | foreach ($fields as $field) { |
| 95 | if (in_array($field['related_table'], $this->tables)) continue; |
| 96 | $externallyRelatedFields[] = $field; |
| 97 | } |
| 98 | |
| 99 | return $externallyRelatedFields ?? []; |
| 100 | } |
| 101 | } |