summaryrefslogtreecommitdiff
path: root/vendor/doctrine/dbal/src/Schema/AbstractAsset.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/doctrine/dbal/src/Schema/AbstractAsset.php')
-rw-r--r--vendor/doctrine/dbal/src/Schema/AbstractAsset.php157
1 files changed, 157 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/Schema/AbstractAsset.php b/vendor/doctrine/dbal/src/Schema/AbstractAsset.php
new file mode 100644
index 0000000..de5b56f
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Schema/AbstractAsset.php
@@ -0,0 +1,157 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Schema;
6
7use Doctrine\DBAL\Platforms\AbstractPlatform;
8
9use function array_map;
10use function crc32;
11use function dechex;
12use function explode;
13use function implode;
14use function str_contains;
15use function str_replace;
16use function strtolower;
17use function strtoupper;
18use function substr;
19
20/**
21 * The abstract asset allows to reset the name of all assets without publishing this to the public userland.
22 *
23 * This encapsulation hack is necessary to keep a consistent state of the database schema. Say we have a list of tables
24 * array($tableName => Table($tableName)); if you want to rename the table, you have to make sure this does not get
25 * recreated during schema migration.
26 */
27abstract class AbstractAsset
28{
29 protected string $_name = '';
30
31 /**
32 * Namespace of the asset. If none isset the default namespace is assumed.
33 */
34 protected ?string $_namespace = null;
35
36 protected bool $_quoted = false;
37
38 /**
39 * Sets the name of this asset.
40 */
41 protected function _setName(string $name): void
42 {
43 if ($this->isIdentifierQuoted($name)) {
44 $this->_quoted = true;
45 $name = $this->trimQuotes($name);
46 }
47
48 if (str_contains($name, '.')) {
49 $parts = explode('.', $name);
50 $this->_namespace = $parts[0];
51 $name = $parts[1];
52 }
53
54 $this->_name = $name;
55 }
56
57 /**
58 * Is this asset in the default namespace?
59 */
60 public function isInDefaultNamespace(string $defaultNamespaceName): bool
61 {
62 return $this->_namespace === $defaultNamespaceName || $this->_namespace === null;
63 }
64
65 /**
66 * Gets the namespace name of this asset.
67 *
68 * If NULL is returned this means the default namespace is used.
69 */
70 public function getNamespaceName(): ?string
71 {
72 return $this->_namespace;
73 }
74
75 /**
76 * The shortest name is stripped of the default namespace. All other
77 * namespaced elements are returned as full-qualified names.
78 */
79 public function getShortestName(?string $defaultNamespaceName): string
80 {
81 $shortestName = $this->getName();
82 if ($this->_namespace === $defaultNamespaceName) {
83 $shortestName = $this->_name;
84 }
85
86 return strtolower($shortestName);
87 }
88
89 /**
90 * Checks if this asset's name is quoted.
91 */
92 public function isQuoted(): bool
93 {
94 return $this->_quoted;
95 }
96
97 /**
98 * Checks if this identifier is quoted.
99 */
100 protected function isIdentifierQuoted(string $identifier): bool
101 {
102 return isset($identifier[0]) && ($identifier[0] === '`' || $identifier[0] === '"' || $identifier[0] === '[');
103 }
104
105 /**
106 * Trim quotes from the identifier.
107 */
108 protected function trimQuotes(string $identifier): string
109 {
110 return str_replace(['`', '"', '[', ']'], '', $identifier);
111 }
112
113 /**
114 * Returns the name of this schema asset.
115 */
116 public function getName(): string
117 {
118 if ($this->_namespace !== null) {
119 return $this->_namespace . '.' . $this->_name;
120 }
121
122 return $this->_name;
123 }
124
125 /**
126 * Gets the quoted representation of this asset but only if it was defined with one. Otherwise
127 * return the plain unquoted value as inserted.
128 */
129 public function getQuotedName(AbstractPlatform $platform): string
130 {
131 $keywords = $platform->getReservedKeywordsList();
132 $parts = explode('.', $this->getName());
133 foreach ($parts as $k => $v) {
134 $parts[$k] = $this->_quoted || $keywords->isKeyword($v) ? $platform->quoteIdentifier($v) : $v;
135 }
136
137 return implode('.', $parts);
138 }
139
140 /**
141 * Generates an identifier from a list of column names obeying a certain string length.
142 *
143 * This is especially important for Oracle, since it does not allow identifiers larger than 30 chars,
144 * however building idents automatically for foreign keys, composite keys or such can easily create
145 * very long names.
146 *
147 * @param array<int, string> $columnNames
148 */
149 protected function _generateIdentifierName(array $columnNames, string $prefix = '', int $maxSize = 30): string
150 {
151 $hash = implode('', array_map(static function ($column): string {
152 return dechex(crc32($column));
153 }, $columnNames));
154
155 return strtoupper(substr($prefix . '_' . $hash, 0, $maxSize));
156 }
157}