Completed
Pull Request — 2.6 (#7908)
by Luís
14:56 queued 08:08
created

UnderscoreNamingStrategy::joinTableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license. For more information, see
18
 * <http://www.doctrine-project.org>.
19
 */
20
21
namespace Doctrine\ORM\Mapping;
22
23
use const CASE_LOWER;
24
use const CASE_UPPER;
25
use function preg_replace;
26
use function strpos;
27
use function strrpos;
28
use function strtolower;
29
use function strtoupper;
30
use function substr;
31
32
/**
33
 * Naming strategy implementing the underscore naming convention.
34
 * Converts 'MyEntity' to 'my_entity' or 'MY_ENTITY'.
35
 *
36
 *
37
 * @link    www.doctrine-project.org
38
 * @since   2.3
39
 * @author  Fabio B. Silva <[email protected]>
40
 */
41
class UnderscoreNamingStrategy implements NamingStrategy
42
{
43
    private const DEFAULT_PATTERN      = '/(?<=[a-z])([A-Z])/';
44
    private const NUMBER_AWARE_PATTERN = '/(?<=[a-z0-9])([A-Z])/';
45
46
    /**
47
     * @var integer
48
     */
49
    private $case;
50
51
    /** @var string */
52
    private $pattern;
53
54
    /**
55
     * Underscore naming strategy construct.
56
     *
57
     * @param int $case CASE_LOWER | CASE_UPPER
58
     */
59 7
    public function __construct($case = CASE_LOWER, bool $numberAware = false)
60
    {
61 7
        $this->case    = $case;
62 7
        $this->pattern = $numberAware ? self::NUMBER_AWARE_PATTERN : self::DEFAULT_PATTERN;
63 7
    }
64
65
    /**
66
     * @return integer CASE_LOWER | CASE_UPPER
67
     */
68
    public function getCase()
69
    {
70
        return $this->case;
71
    }
72
73
    /**
74
     * Sets string case CASE_LOWER | CASE_UPPER.
75
     * Alphabetic characters converted to lowercase or uppercase.
76
     *
77
     * @param integer $case
78
     *
79
     * @return void
80
     */
81
    public function setCase($case)
82
    {
83
        $this->case = $case;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 53
    public function classToTableName($className)
90
    {
91 53
        if (strpos($className, '\\') !== false) {
92 45
            $className = substr($className, strrpos($className, '\\') + 1);
93
        }
94
95 53
        return $this->underscore($className);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 22
    public function propertyToColumnName($propertyName, $className = null)
102
    {
103 22
        return $this->underscore($propertyName);
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className = null, $embeddedClassName = null)
110
    {
111
        return $this->underscore($propertyName).'_'.$embeddedColumnName;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 23
    public function referenceColumnName()
118
    {
119 23
        return $this->case === CASE_UPPER ?  'ID' : 'id';
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 11
    public function joinColumnName($propertyName, $className = null)
126
    {
127 11
        return $this->underscore($propertyName) . '_' . $this->referenceColumnName();
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 17
    public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
134
    {
135 17
        return $this->classToTableName($sourceEntity) . '_' . $this->classToTableName($targetEntity);
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141 17
    public function joinKeyColumnName($entityName, $referencedColumnName = null)
142
    {
143 17
        return $this->classToTableName($entityName) . '_' .
144 17
                ($referencedColumnName ?: $this->referenceColumnName());
145
    }
146
147 79
    private function underscore(string $string) : string
148
    {
149 79
        $string = preg_replace($this->pattern, '_$1', $string);
150
151 79
        if ($this->case === CASE_UPPER) {
152 45
            return strtoupper($string);
153
        }
154
155 34
        return strtolower($string);
156
    }
157
}
158