|
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
|
|
|
|