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
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;
16

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

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

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

38
    /**
Benjamin Morel's avatar
Benjamin Morel committed
39
     * Sets the name of this asset.
40 41
     *
     * @param string $name
Benjamin Morel's avatar
Benjamin Morel committed
42 43
     *
     * @return void
44 45 46
     */
    protected function _setName($name)
    {
47
        if ($this->isIdentifierQuoted($name)) {
48
            $this->_quoted = true;
49
            $name          = $this->trimQuotes($name);
50
        }
51 52
        if (strpos($name, '.') !== false) {
            $parts            = explode('.', $name);
53
            $this->_namespace = $parts[0];
54
            $name             = $parts[1];
55
        }
56 57 58
        $this->_name = $name;
    }

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

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

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

121 122 123
        return strtolower($name);
    }

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

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

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

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

169
        return $this->_name;
170
    }
171

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

186
        return implode('.', $parts);
187 188
    }

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

208
        return strtoupper(substr($prefix . '_' . $hash, 0, $maxSize));
209
    }
210
}