AbstractAsset.php 5.57 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Schema;

5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6

7 8 9 10 11 12 13 14 15 16
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;
17

18 19 20 21 22 23 24 25
/**
 * 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
{
26
    /** @var string */
27 28
    protected $_name;

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

36
    /** @var bool */
37 38
    protected $_quoted = false;

39
    /**
Benjamin Morel's avatar
Benjamin Morel committed
40
     * Sets the name of this asset.
41 42
     *
     * @param string $name
Benjamin Morel's avatar
Benjamin Morel committed
43 44
     *
     * @return void
45 46 47
     */
    protected function _setName($name)
    {
48
        if ($this->isIdentifierQuoted($name)) {
49
            $this->_quoted = true;
50
            $name          = $this->trimQuotes($name);
51
        }
Grégoire Paris's avatar
Grégoire Paris committed
52

53 54
        if (strpos($name, '.') !== false) {
            $parts            = explode('.', $name);
55
            $this->_namespace = $parts[0];
56
            $name             = $parts[1];
57
        }
Grégoire Paris's avatar
Grégoire Paris committed
58

59 60 61
        $this->_name = $name;
    }

62 63 64 65
    /**
     * Is this asset in the default namespace?
     *
     * @param string $defaultNamespaceName
Benjamin Morel's avatar
Benjamin Morel committed
66
     *
67
     * @return bool
68 69 70
     */
    public function isInDefaultNamespace($defaultNamespaceName)
    {
71
        return $this->_namespace === $defaultNamespaceName || $this->_namespace === null;
72 73 74
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
75
     * Gets the namespace name of this asset.
76 77 78
     *
     * If NULL is returned this means the default namespace is used.
     *
Benjamin Morel's avatar
Benjamin Morel committed
79
     * @return string|null
80 81 82 83 84 85 86 87 88 89
     */
    public function getNamespaceName()
    {
        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
90
     * @param string|null $defaultNamespaceName
Benjamin Morel's avatar
Benjamin Morel committed
91
     *
92 93 94 95 96
     * @return string
     */
    public function getShortestName($defaultNamespaceName)
    {
        $shortestName = $this->getName();
97
        if ($this->_namespace === $defaultNamespaceName) {
98 99
            $shortestName = $this->_name;
        }
Benjamin Morel's avatar
Benjamin Morel committed
100

101 102 103 104 105 106 107 108 109 110 111 112
        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.
     *
Benjamin Morel's avatar
Benjamin Morel committed
113 114
     * @param string $defaultNamespaceName
     *
115 116 117 118 119
     * @return string
     */
    public function getFullQualifiedName($defaultNamespaceName)
    {
        $name = $this->getName();
120 121
        if (! $this->_namespace) {
            $name = $defaultNamespaceName . '.' . $name;
122
        }
Benjamin Morel's avatar
Benjamin Morel committed
123

124 125 126
        return strtolower($name);
    }

127
    /**
Benjamin Morel's avatar
Benjamin Morel committed
128
     * Checks if this asset's name is quoted.
129
     *
130
     * @return bool
131 132 133 134 135 136
     */
    public function isQuoted()
    {
        return $this->_quoted;
    }

137
    /**
Benjamin Morel's avatar
Benjamin Morel committed
138
     * Checks if this identifier is quoted.
139
     *
Benjamin Morel's avatar
Benjamin Morel committed
140 141
     * @param string $identifier
     *
142
     * @return bool
143
     */
144
    protected function isIdentifierQuoted($identifier)
145
    {
146
        return isset($identifier[0]) && ($identifier[0] === '`' || $identifier[0] === '"' || $identifier[0] === '[');
147 148 149 150
    }

    /**
     * Trim quotes from the identifier.
151
     *
Benjamin Morel's avatar
Benjamin Morel committed
152 153
     * @param string $identifier
     *
154 155 156 157
     * @return string
     */
    protected function trimQuotes($identifier)
    {
158
        return str_replace(['`', '"', '[', ']'], '', $identifier);
159 160
    }

161
    /**
Benjamin Morel's avatar
Benjamin Morel committed
162
     * Returns the name of this schema asset.
163
     *
164 165 166 167
     * @return string
     */
    public function getName()
    {
168
        if ($this->_namespace) {
169
            return $this->_namespace . '.' . $this->_name;
170
        }
171

172
        return $this->_name;
173
    }
174

175
    /**
Benjamin Morel's avatar
Benjamin Morel committed
176
     * Gets the quoted representation of this asset but only if it was defined with one. Otherwise
177 178 179 180 181 182
     * return the plain unquoted value as inserted.
     *
     * @return string
     */
    public function getQuotedName(AbstractPlatform $platform)
    {
183
        $keywords = $platform->getReservedKeywordsList();
184
        $parts    = explode('.', $this->getName());
185
        foreach ($parts as $k => $v) {
186
            $parts[$k] = $this->_quoted || $keywords->isKeyword($v) ? $platform->quoteIdentifier($v) : $v;
187 188
        }

189
        return implode('.', $parts);
190 191
    }

192
    /**
Benjamin Morel's avatar
Benjamin Morel committed
193
     * Generates an identifier from a list of column names obeying a certain string length.
194 195 196 197 198
     *
     * 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.
     *
199 200 201
     * @param string[] $columnNames
     * @param string   $prefix
     * @param int      $maxSize
Benjamin Morel's avatar
Benjamin Morel committed
202
     *
203 204
     * @return string
     */
205
    protected function _generateIdentifierName($columnNames, $prefix = '', $maxSize = 30)
206
    {
207
        $hash = implode('', array_map(static function ($column) {
208 209
            return dechex(crc32($column));
        }, $columnNames));
Benjamin Morel's avatar
Benjamin Morel committed
210

211
        return strtoupper(substr($prefix . '_' . $hash, 0, $maxSize));
212
    }
213
}