AbstractAsset.php 5.14 KB
Newer Older
1 2
<?php

Michael Moravec's avatar
Michael Moravec committed
3 4
declare(strict_types=1);

5 6
namespace Doctrine\DBAL\Schema;

7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8

9 10 11 12 13 14 15 16 17 18
use function array_map;
use function crc32;
use function dechex;
use function explode;
use function implode;
use function str_replace;
use function strpos;
use function strtolower;
use function strtoupper;
use function substr;
19

20 21 22 23 24 25 26 27
/**
 * The abstract asset allows to reset the name of all assets without publishing this to the public userland.
 *
 * This encapsulation hack is necessary to keep a consistent state of the database schema. Say we have a list of tables
 * array($tableName => Table($tableName)); if you want to rename the table, you have to make sure
 */
abstract class AbstractAsset
{
28
    /** @var string */
29
    protected $_name = '';
30

31 32 33
    /**
     * Namespace of the asset. If none isset the default namespace is assumed.
     *
Benjamin Morel's avatar
Benjamin Morel committed
34
     * @var string|null
35
     */
Benjamin Morel's avatar
Benjamin Morel committed
36
    protected $_namespace = null;
37

38
    /** @var bool */
39 40
    protected $_quoted = false;

41
    /**
Benjamin Morel's avatar
Benjamin Morel committed
42
     * Sets the name of this asset.
43
     */
Sergei Morozov's avatar
Sergei Morozov committed
44
    protected function _setName(string $name): void
45
    {
46
        if ($this->isIdentifierQuoted($name)) {
47
            $this->_quoted = true;
48
            $name          = $this->trimQuotes($name);
49
        }
50

51 52
        if (strpos($name, '.') !== false) {
            $parts            = explode('.', $name);
53
            $this->_namespace = $parts[0];
54
            $name             = $parts[1];
55
        }
56

57 58 59
        $this->_name = $name;
    }

60 61 62
    /**
     * Is this asset in the default namespace?
     */
Sergei Morozov's avatar
Sergei Morozov committed
63
    public function isInDefaultNamespace(string $defaultNamespaceName): bool
64
    {
65
        return $this->_namespace === $defaultNamespaceName || $this->_namespace === null;
66 67 68
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
69
     * Gets the namespace name of this asset.
70 71 72
     *
     * If NULL is returned this means the default namespace is used.
     */
Sergei Morozov's avatar
Sergei Morozov committed
73
    public function getNamespaceName(): ?string
74 75 76 77 78 79 80 81
    {
        return $this->_namespace;
    }

    /**
     * The shortest name is stripped of the default namespace. All other
     * namespaced elements are returned as full-qualified names.
     */
Sergei Morozov's avatar
Sergei Morozov committed
82
    public function getShortestName(?string $defaultNamespaceName): string
83 84
    {
        $shortestName = $this->getName();
85
        if ($this->_namespace === $defaultNamespaceName) {
86 87
            $shortestName = $this->_name;
        }
Benjamin Morel's avatar
Benjamin Morel committed
88

89 90 91 92 93 94 95 96 97 98 99 100
        return strtolower($shortestName);
    }

    /**
     * The normalized name is full-qualified and lowerspaced. Lowerspacing is
     * actually wrong, but we have to do it to keep our sanity. If you are
     * using database objects that only differentiate in the casing (FOO vs
     * Foo) then you will NOT be able to use Doctrine Schema abstraction.
     *
     * Every non-namespaced element is prefixed with the default namespace
     * name which is passed as argument to this method.
     */
Sergei Morozov's avatar
Sergei Morozov committed
101
    public function getFullQualifiedName(string $defaultNamespaceName): string
102 103
    {
        $name = $this->getName();
104
        if ($this->_namespace === null) {
105
            $name = $defaultNamespaceName . '.' . $name;
106
        }
Benjamin Morel's avatar
Benjamin Morel committed
107

108 109 110
        return strtolower($name);
    }

111
    /**
Benjamin Morel's avatar
Benjamin Morel committed
112
     * Checks if this asset's name is quoted.
113
     */
Sergei Morozov's avatar
Sergei Morozov committed
114
    public function isQuoted(): bool
115 116 117 118
    {
        return $this->_quoted;
    }

119
    /**
Benjamin Morel's avatar
Benjamin Morel committed
120
     * Checks if this identifier is quoted.
121
     */
Sergei Morozov's avatar
Sergei Morozov committed
122
    protected function isIdentifierQuoted(string $identifier): bool
123
    {
124
        return isset($identifier[0]) && ($identifier[0] === '`' || $identifier[0] === '"' || $identifier[0] === '[');
125 126 127 128 129
    }

    /**
     * Trim quotes from the identifier.
     */
Sergei Morozov's avatar
Sergei Morozov committed
130
    protected function trimQuotes(string $identifier): string
131
    {
132
        return str_replace(['`', '"', '[', ']'], '', $identifier);
133 134
    }

135
    /**
Benjamin Morel's avatar
Benjamin Morel committed
136
     * Returns the name of this schema asset.
137
     */
Sergei Morozov's avatar
Sergei Morozov committed
138
    public function getName(): string
139
    {
140
        if ($this->_namespace !== null) {
141
            return $this->_namespace . '.' . $this->_name;
142
        }
143

144
        return $this->_name;
145
    }
146

147
    /**
Benjamin Morel's avatar
Benjamin Morel committed
148
     * Gets the quoted representation of this asset but only if it was defined with one. Otherwise
149 150
     * return the plain unquoted value as inserted.
     */
Sergei Morozov's avatar
Sergei Morozov committed
151
    public function getQuotedName(AbstractPlatform $platform): string
152
    {
153
        $keywords = $platform->getReservedKeywordsList();
154
        $parts    = explode('.', $this->getName());
155
        foreach ($parts as $k => $v) {
156
            $parts[$k] = $this->_quoted || $keywords->isKeyword($v) ? $platform->quoteIdentifier($v) : $v;
157 158
        }

159
        return implode('.', $parts);
160 161
    }

162
    /**
Benjamin Morel's avatar
Benjamin Morel committed
163
     * Generates an identifier from a list of column names obeying a certain string length.
164 165 166 167 168
     *
     * This is especially important for Oracle, since it does not allow identifiers larger than 30 chars,
     * however building idents automatically for foreign keys, composite keys or such can easily create
     * very long names.
     *
169
     * @param array<int, string> $columnNames
170
     */
Sergei Morozov's avatar
Sergei Morozov committed
171
    protected function _generateIdentifierName(array $columnNames, string $prefix = '', int $maxSize = 30): string
172
    {
173
        $hash = implode('', array_map(static function ($column): string {
174 175
            return dechex(crc32($column));
        }, $columnNames));
Benjamin Morel's avatar
Benjamin Morel committed
176

177
        return strtoupper(substr($prefix . '_' . $hash, 0, $maxSize));
178
    }
179
}