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