Failed Conditions
Pull Request — 2.6 (#7785)
by Luís
12:23
created

DefaultQuoteStrategy::getIdentifierColumnNames()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 14
c 0
b 0
f 0
nop 2
dl 0
loc 27
ccs 15
cts 15
cp 1
crap 4
rs 9.7998
nc 3
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Mapping;
21
22
use Doctrine\DBAL\Platforms\AbstractPlatform;
23
24
/**
25
 * A set of rules for determining the physical column, alias and table quotes
26
 *
27
 * @since   2.3
28
 * @author  Fabio B. Silva <[email protected]>
29
 */
30
class DefaultQuoteStrategy implements QuoteStrategy
31
{
32
    /**
33
     * {@inheritdoc}
34
     */
35 1458
    public function getColumnName($fieldName, ClassMetadata $class, AbstractPlatform $platform)
36
    {
37 1458
        return isset($class->fieldMappings[$fieldName]['quoted'])
38 24
            ? $platform->quoteIdentifier($class->fieldMappings[$fieldName]['columnName'])
39 1458
            : $class->fieldMappings[$fieldName]['columnName'];
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     *
45
     * @todo Table names should be computed in DBAL depending on the platform
46
     */
47 1466
    public function getTableName(ClassMetadata $class, AbstractPlatform $platform)
48
    {
49 1466
        $tableName = $class->table['name'];
50
51 1466
        if ( ! empty($class->table['schema'])) {
52 6
            $tableName = $class->table['schema'] . '.' . $class->table['name'];
53
54 6
            if ( ! $platform->supportsSchemas() && $platform->canEmulateSchemas()) {
55 6
                $tableName = $class->table['schema'] . '__' . $class->table['name'];
56
            }
57
        }
58
59 1466
        return isset($class->table['quoted'])
60 27
            ? $platform->quoteIdentifier($tableName)
61 1466
            : $tableName;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 6
    public function getSequenceName(array $definition, ClassMetadata $class, AbstractPlatform $platform)
68
    {
69 6
        return isset($definition['quoted'])
70
            ? $platform->quoteIdentifier($definition['sequenceName'])
71 6
            : $definition['sequenceName'];
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 1094
    public function getJoinColumnName(array $joinColumn, ClassMetadata $class, AbstractPlatform $platform)
78
    {
79 1094
        return isset($joinColumn['quoted'])
80 14
            ? $platform->quoteIdentifier($joinColumn['name'])
81 1094
            : $joinColumn['name'];
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 602
    public function getReferencedJoinColumnName(array $joinColumn, ClassMetadata $class, AbstractPlatform $platform)
88
    {
89 602
        return isset($joinColumn['quoted'])
90 9
            ? $platform->quoteIdentifier($joinColumn['referencedColumnName'])
91 602
            : $joinColumn['referencedColumnName'];
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 408
    public function getJoinTableName(array $association, ClassMetadata $class, AbstractPlatform $platform)
98
    {
99 408
        $schema = '';
100
101 408
        if (isset($association['joinTable']['schema'])) {
102 1
            $schema = $association['joinTable']['schema'] . '.';
103
        }
104
105 408
        $tableName = $association['joinTable']['name'];
106
107 408
        if (isset($association['joinTable']['quoted'])) {
108 9
            $tableName = $platform->quoteIdentifier($tableName);
109
        }
110
111 408
        return $schema . $tableName;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 385
    public function getIdentifierColumnNames(ClassMetadata $class, AbstractPlatform $platform)
118
    {
119 385
        $quotedColumnNames = [];
120
121 385
        foreach ($class->identifier as $fieldName) {
122 385
            if (isset($class->fieldMappings[$fieldName])) {
123 381
                $quotedColumnNames[] = $this->getColumnName($fieldName, $class, $platform);
124
125 381
                continue;
126
            }
127
128
            // Association defined as Id field
129 8
            $joinColumns            = $class->associationMappings[$fieldName]['joinColumns'];
130 8
            $assocQuotedColumnNames = array_map(
131
                function ($joinColumn) use ($platform)
132
                {
133 8
                    return isset($joinColumn['quoted'])
134 1
                        ? $platform->quoteIdentifier($joinColumn['name'])
135 8
                        : $joinColumn['name'];
136 8
                },
137 8
                $joinColumns
138
            );
139
140 8
            $quotedColumnNames = array_merge($quotedColumnNames, $assocQuotedColumnNames);
141
        }
142
143 385
        return $quotedColumnNames;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149 1169
    public function getColumnAlias($columnName, $counter, AbstractPlatform $platform, ClassMetadata $class = null)
150
    {
151
        // 1 ) Concatenate column name and counter
152
        // 2 ) Trim the column alias to the maximum identifier length of the platform.
153
        //     If the alias is to long, characters are cut off from the beginning.
154
        // 3 ) Strip non alphanumeric characters
155
        // 4 ) Prefix with "_" if the result its numeric
156 1169
        $columnName = $columnName . '_' . $counter;
157 1169
        $columnName = substr($columnName, -$platform->getMaxIdentifierLength());
158 1169
        $columnName = preg_replace('/[^A-Za-z0-9_]/', '', $columnName);
159 1169
        $columnName = is_numeric($columnName) ? '_' . $columnName : $columnName;
160
161 1169
        return $platform->getSQLResultCasing($columnName);
162
    }
163
}
164