Passed
Pull Request — 2.6 (#7079)
by
unknown
10:29
created

DefaultQuoteStrategy   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Test Coverage

Coverage 98.21%

Importance

Changes 0
Metric Value
dl 0
loc 133
ccs 55
cts 56
cp 0.9821
rs 10
c 0
b 0
f 0
wmc 24

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getReferencedJoinColumnName() 0 5 2
B getTableName() 0 15 5
A getColumnName() 0 5 2
A getSequenceName() 0 5 2
A getJoinColumnName() 0 5 2
B getIdentifierColumnNames() 0 27 4
A getColumnAlias() 0 13 2
B getJoinTableName() 0 16 5
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 1436
    public function getColumnName($fieldName, ClassMetadata $class, AbstractPlatform $platform)
36
    {
37 1436
        return isset($class->fieldMappings[$fieldName]['quoted'])
38 23
            ? $platform->quoteIdentifier($class->fieldMappings[$fieldName]['columnName'])
39 1436
            : $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 1444
    public function getTableName(ClassMetadata $class, AbstractPlatform $platform)
48
    {
49 1444
        $tableName = $class->table['name'];
50
51 1444
        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 1444
        return isset($class->table['quoted'])
60 26
            ? $platform->quoteIdentifier($tableName)
61 1444
            : $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 1079
    public function getJoinColumnName(array $joinColumn, ClassMetadata $class, AbstractPlatform $platform)
78
    {
79 1079
        return isset($joinColumn['quoted'])
80 13
            ? $platform->quoteIdentifier($joinColumn['name'])
81 1079
            : $joinColumn['name'];
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 593
    public function getReferencedJoinColumnName(array $joinColumn, ClassMetadata $class, AbstractPlatform $platform)
88
    {
89 593
        return isset($joinColumn['quoted'])
90 8
            ? $platform->quoteIdentifier($joinColumn['referencedColumnName'])
91 593
            : $joinColumn['referencedColumnName'];
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 406
    public function getJoinTableName(array $association, ClassMetadata $class, AbstractPlatform $platform)
98
    {
99 406
        $schema = '';
100
101 406
        if (isset($association['joinTable']['schema'])) {
102 1
            $schema = $association['joinTable']['schema'];
103 1
            $schema .= ( ! $platform->supportsSchemas() && $platform->canEmulateSchemas()) ? '__' : '.';
104
        }
105
106 406
        $tableName = $association['joinTable']['name'];
107
108 406
        if (isset($association['joinTable']['quoted'])) {
109 9
            $tableName = $platform->quoteIdentifier($tableName);
110
        }
111
112 406
        return $schema . $tableName;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 383
    public function getIdentifierColumnNames(ClassMetadata $class, AbstractPlatform $platform)
119
    {
120 383
        $quotedColumnNames = [];
121
122 383
        foreach ($class->identifier as $fieldName) {
123 383
            if (isset($class->fieldMappings[$fieldName])) {
124 379
                $quotedColumnNames[] = $this->getColumnName($fieldName, $class, $platform);
125
126 379
                continue;
127
            }
128
129
            // Association defined as Id field
130 8
            $joinColumns            = $class->associationMappings[$fieldName]['joinColumns'];
131 8
            $assocQuotedColumnNames = array_map(
132 8
                function ($joinColumn) use ($platform)
133
                {
134 8
                    return isset($joinColumn['quoted'])
135 1
                        ? $platform->quoteIdentifier($joinColumn['name'])
136 8
                        : $joinColumn['name'];
137 8
                },
138 8
                $joinColumns
139
            );
140
141 8
            $quotedColumnNames = array_merge($quotedColumnNames, $assocQuotedColumnNames);
142
        }
143
144 383
        return $quotedColumnNames;
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150 1153
    public function getColumnAlias($columnName, $counter, AbstractPlatform $platform, ClassMetadata $class = null)
151
    {
152
        // 1 ) Concatenate column name and counter
153
        // 2 ) Trim the column alias to the maximum identifier length of the platform.
154
        //     If the alias is to long, characters are cut off from the beginning.
155
        // 3 ) Strip non alphanumeric characters
156
        // 4 ) Prefix with "_" if the result its numeric
157 1153
        $columnName = $columnName . '_' . $counter;
158 1153
        $columnName = substr($columnName, -$platform->getMaxIdentifierLength());
159 1153
        $columnName = preg_replace('/[^A-Za-z0-9_]/', '', $columnName);
160 1153
        $columnName = is_numeric($columnName) ? '_' . $columnName : $columnName;
161
162 1153
        return $platform->getSQLResultCasing($columnName);
163
    }
164
}
165