1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Schema; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* TableName - abstraction for name of table in DataBase |
9
|
|
|
*/ |
10
|
|
|
class TableName implements TableNameInterface |
11
|
|
|
{ |
12
|
|
|
private const DELIMITER = '.'; |
13
|
|
|
|
14
|
|
|
private ?string $prefix = null; |
15
|
|
|
|
16
|
|
|
private string $tableName; |
17
|
|
|
private ?string $schemaName; |
18
|
|
|
private ?string $catalogName; |
19
|
|
|
private ?string $serverName; |
20
|
|
|
|
21
|
|
|
public function __construct( |
22
|
|
|
string $tableName, |
23
|
|
|
?string $schemaName = null, |
24
|
|
|
?string $catalogName = null, |
25
|
|
|
?string $serverName = null |
26
|
|
|
) { |
27
|
|
|
$this->tableName = $tableName; |
28
|
|
|
$this->schemaName = $schemaName; |
29
|
|
|
$this->catalogName = $catalogName; |
30
|
|
|
$this->serverName = $serverName; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getTableName(): string |
34
|
|
|
{ |
35
|
|
|
return $this->addPrefix($this->tableName); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getRawTableName(): string |
39
|
|
|
{ |
40
|
|
|
return $this->tableName; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getPrefix(): ?string |
44
|
|
|
{ |
45
|
|
|
return $this->prefix; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function setPrefix(?string $prefix = null): static |
49
|
|
|
{ |
50
|
|
|
$this->prefix = $prefix; |
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function withPrefix(?string $prefix = null): static |
55
|
|
|
{ |
56
|
|
|
$new = clone $this; |
57
|
|
|
$new->prefix = $prefix; |
58
|
|
|
return $new; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getSchemaName(): ?string |
62
|
|
|
{ |
63
|
|
|
return $this->schemaName; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getCatalogName(): ?string |
67
|
|
|
{ |
68
|
|
|
return $this->catalogName; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getServerName(): ?string |
72
|
|
|
{ |
73
|
|
|
return $this->serverName; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function __toString() |
77
|
|
|
{ |
78
|
|
|
return implode((string) static::DELIMITER, array_filter([ |
79
|
|
|
$this->serverName, |
80
|
|
|
$this->catalogName, |
81
|
|
|
$this->schemaName, |
82
|
|
|
$this->getTableName(), |
83
|
|
|
])); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function addPrefix(string $name): string |
87
|
|
|
{ |
88
|
|
|
if (!str_contains($name, '{{')) { |
89
|
|
|
return $name; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$name = preg_replace('/{{(.*?)}}/', '\1', $name); |
93
|
|
|
|
94
|
|
|
return str_replace('%', $this->prefix ?? '', $name); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|