Completed
Pull Request — master (#3)
by Rougin
02:51
created

TableHelper::getModelName()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 6
nop 3
crap 5
1
<?php
2
3
namespace Rougin\Wildfire\Helpers;
4
5
/**
6
 * Table Helper
7
 *
8
 * @package Wildfire
9
 * @author  Rougin Royce Gutib <[email protected]>
10
 */
11
class TableHelper
12
{
13
    /**
14
     * Parses the table name from Describe class.
15
     *
16
     * @param  string  $table
17
     * @param  mixed   $currentTable
18
     * @param  boolean $isForeignKey
19
     * @return string
20
     */
21 36
    public static function getModelName($table, $currentTable, $isForeignKey = false)
22
    {
23 36
        $tableName = $table;
24
25 36
        if (! $isForeignKey && $currentTable) {
26 24
            $tableName = $currentTable;
27
28 24
            if (is_object($currentTable)) {
29 9
                $tableName = $currentTable->getTableName();
30 9
            }
31 24
        }
32
33 36
        $tableName  = ucfirst(singular($tableName));
34 36
        $tableNames = explode('.', $tableName);
35
36 36
        return isset($tableNames[1]) ? $tableNames[1] : $tableName;
37
    }
38
39
    /**
40
     * Gets the table name specified from the model.
41
     * NOTE: To be removed in v1.0.0. Use $model->getTableName()
42
     *
43
     * @param  object $model
44
     * @return string
45
     */
46 39
    public static function getNameFromModel($model)
47
    {
48 39
        $tableName = '';
49
50 39
        if (method_exists($model, 'getTableName')) {
51 39
            $tableName = $model->getTableName();
52 39
        }
53
54 39
        if (isset($model->table)) {
55 3
            $tableName = $model->table;
56 3
        }
57
58 39
        return $tableName;
59
    }
60
}
61