Passed
Pull Request — 2.6 (#7079)
by
unknown
08:23
created

DefaultQuoteStrategy   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Test Coverage

Coverage 98.18%

Importance

Changes 0
Metric Value
wmc 24
eloc 48
dl 0
loc 133
ccs 54
cts 55
cp 0.9818
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getReferencedJoinColumnName() 0 5 2
A getTableName() 0 15 5
A getColumnName() 0 5 2
A getSequenceName() 0 5 2
A getJoinColumnName() 0 5 2
A getIdentifierColumnNames() 0 27 4
A getColumnAlias() 0 13 2
A 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 1464
    public function getColumnName($fieldName, ClassMetadata $class, AbstractPlatform $platform)
36
    {
37 1464
        return isset($class->fieldMappings[$fieldName]['quoted'])
38 24
            ? $platform->quoteIdentifier($class->fieldMappings[$fieldName]['columnName'])
39 1464
            : $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 1474
    public function getTableName(ClassMetadata $class, AbstractPlatform $platform)
48
    {
49 1474
        $tableName = $class->table['name'];
50
51 1474
        if ( ! empty($class->table['schema'])) {
52 7
            $tableName = $class->table['schema'] . '.' . $class->table['name'];
53
54 7
            if ( ! $platform->supportsSchemas() && $platform->canEmulateSchemas()) {
55 7
                $tableName = $class->table['schema'] . '__' . $class->table['name'];
56
            }
57
        }
58
59 1474
        return isset($class->table['quoted'])
60 27
            ? $platform->quoteIdentifier($tableName)
61 1474
            : $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 1098
    public function getJoinColumnName(array $joinColumn, ClassMetadata $class, AbstractPlatform $platform)
78
    {
79 1098
        return isset($joinColumn['quoted'])
80 14
            ? $platform->quoteIdentifier($joinColumn['name'])
81 1098
            : $joinColumn['name'];
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 603
    public function getReferencedJoinColumnName(array $joinColumn, ClassMetadata $class, AbstractPlatform $platform)
88
    {
89 603
        return isset($joinColumn['quoted'])
90 9
            ? $platform->quoteIdentifier($joinColumn['referencedColumnName'])
91 603
            : $joinColumn['referencedColumnName'];
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 409
    public function getJoinTableName(array $association, ClassMetadata $class, AbstractPlatform $platform)
98
    {
99 409
        $schema = '';
100
101 409
        if (isset($association['joinTable']['schema'])) {
102 2
            $schema  = $association['joinTable']['schema'];
103 2
            $schema .= ! $platform->supportsSchemas() && $platform->canEmulateSchemas() ? '__' : '.';
104
        }
105
106 409
        $tableName = $association['joinTable']['name'];
107
108 409
        if (isset($association['joinTable']['quoted'])) {
109 9
            $tableName = $platform->quoteIdentifier($tableName);
110
        }
111
112 409
        return $schema . $tableName;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 385
    public function getIdentifierColumnNames(ClassMetadata $class, AbstractPlatform $platform)
119
    {
120 385
        $quotedColumnNames = [];
121
122 385
        foreach ($class->identifier as $fieldName) {
123 385
            if (isset($class->fieldMappings[$fieldName])) {
124 381
                $quotedColumnNames[] = $this->getColumnName($fieldName, $class, $platform);
125
126 381
                continue;
127
            }
128
129
            // Association defined as Id field
130 8
            $joinColumns            = $class->associationMappings[$fieldName]['joinColumns'];
131 8
            $assocQuotedColumnNames = array_map(
132
                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 385
        return $quotedColumnNames;
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150 1176
    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 1176
        $columnName = $columnName . '_' . $counter;
158 1176
        $columnName = substr($columnName, -$platform->getMaxIdentifierLength());
159 1176
        $columnName = preg_replace('/[^A-Za-z0-9_]/', '', $columnName);
160 1176
        $columnName = is_numeric($columnName) ? '_' . $columnName : $columnName;
161
162 1176
        return $platform->getSQLResultCasing($columnName);
163
    }
164
}
165