yaroslawww /
laravel-geonames
| 1 | <?php |
||
| 2 | |||
| 3 | namespace LaraGeoData\Models; |
||
| 4 | |||
| 5 | trait HasTableWithSuffix |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * Get base table name (root name, without suffix). |
||
| 9 | * |
||
| 10 | * @return string |
||
| 11 | */ |
||
| 12 | abstract public function getTableNameRoot(): string; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @inheritDoc |
||
| 16 | */ |
||
| 17 | 4 | public function getTable(): string |
|
| 18 | { |
||
| 19 | 4 | if ($this->table) { |
|
| 20 | 4 | return $this->table; |
|
| 21 | } |
||
| 22 | 2 | $tableName = $this->getTableNameRoot(); |
|
| 23 | 2 | if ($suffix = config('geonames.database.default_suffix')) { |
|
| 24 | 2 | $tableName = "{$tableName}_{$suffix}"; |
|
| 25 | } |
||
| 26 | |||
| 27 | 2 | return $tableName; |
|
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Initialise model with suffixed table name. |
||
| 32 | * |
||
| 33 | * @param string|null $suffix |
||
| 34 | * @return static |
||
| 35 | */ |
||
| 36 | 4 | public static function makeUsingSuffix(?string $suffix = null): static |
|
| 37 | { |
||
| 38 | 4 | $instance = new static(); |
|
| 39 | 4 | $tableName = $instance->getTableNameRoot(); |
|
| 40 | 4 | if ($suffix) { |
|
| 41 | 4 | $tableName = "{$tableName}_{$suffix}"; |
|
| 42 | } |
||
| 43 | |||
| 44 | 4 | return $instance->setTable($tableName); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 45 | } |
||
| 46 | } |
||
| 47 |