Passed
Push — master ( 4de437...05d74c )
by Arthur
04:30
created

MigrationsHelper::getObjectName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace SoliDry\Helpers;
4
5
use SoliDry\Types\PhpInterface;
6
7
/**
8
 * Class MigrationsHelper
9
 * @package SoliDry\Helpers
10
 */
11
class MigrationsHelper
12
{
13
    private const PATTERN_SPLIT_UC = '/(?=[A-Z])/';
14
    private const PATTERN_MERGE_UC = '/(?=[_][A-Z-a-z].)/';
15
    private const DOUBLE_UNDERSCORE = '__';
16
17
    /**
18
     * Generates table_name from TableName objects
19
     * @param string $objectName
20
     * @return string
21
     */
22
    public static function getTableName(string $objectName): string
23
    {
24
        $table = '';
25
        // make entity lc + underscore
26
        $words = preg_split(self::PATTERN_SPLIT_UC, lcfirst($objectName));
27
        foreach($words as $key => $word)
28
        {
29
            $table .= $word;
30
            if(empty($words[$key + 1]) === false)
31
            {
32
                $table .= PhpInterface::UNDERSCORE;
33
            }
34
        }
35
36
        // need post-processing of dbl underscore due to there can be intended underscores in naming by user
37
        $table = str_replace(self::DOUBLE_UNDERSCORE, PhpInterface::UNDERSCORE, $table);
38
        return strtolower($table);
39
    }
40
41
    /**
42
     * Generates table_name from TableName objects
43
     * @param string $tableName
44
     * @return string
45
     */
46
    public static function getObjectName(string $tableName): string
47
    {
48
        $table = '';
49
        // make entity lc + underscore
50
        $words = preg_split(self::PATTERN_MERGE_UC, $tableName);
51
        foreach($words as $key => $word)
52
        {
53
            $table .= ucfirst(str_replace(PhpInterface::UNDERSCORE, '', $word));
54
        }
55
56
        return $table;
57
    }
58
}
59