Completed
Pull Request — master (#7724)
by
unknown
12:00
created

UnderscorePluralNamingStrategy::joinColumnName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Mapping\Factory;
6
7
use Doctrine\Common\Inflector\Inflector;
8
use const CASE_LOWER;
9
use const CASE_UPPER;
10
use function preg_replace;
11
use function strpos;
12
use function strrpos;
13
use function strtolower;
14
use function strtoupper;
15
use function substr;
16
17
/**
18
 * Naming strategy implementing the underscore naming convention.
19
 * Converts 'MyEntity' to 'my_entities' or 'MY_ENTITIES'.
20
 */
21
class UnderscorePluralNamingStrategy implements NamingStrategy
22
{
23
    /** @var int */
24
    private $case;
25
26
    /**
27
     * Underscore naming strategy construct.
28
     *
29
     * @param int $case CASE_LOWER | CASE_UPPER
30
     */
31
    public function __construct($case = CASE_LOWER)
32
    {
33
        $this->case = $case;
34
    }
35
36
    /**
37
     * @return int CASE_LOWER | CASE_UPPER
38
     */
39
    public function getCase()
40
    {
41
        return $this->case;
42
    }
43
44
    /**
45
     * Sets string case CASE_LOWER | CASE_UPPER.
46
     * Alphabetic characters converted to lowercase or uppercase.
47
     *
48
     * @param int $case
49
     */
50
    public function setCase($case)
51
    {
52
        $this->case = $case;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function classToTableName($className)
59
    {
60
        if (strpos($className, '\\') !== false) {
61
            $className = substr($className, strrpos($className, '\\') + 1);
62
        }
63
64
        return $this->underscore($className);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function propertyToColumnName($propertyName, $className = null)
71
    {
72
        return $this->underscore($propertyName);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className = null, $embeddedClassName = null)
79
    {
80
        return $this->underscore($propertyName) . '_' . $embeddedColumnName;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function referenceColumnName()
87
    {
88
        return $this->case === CASE_UPPER ? 'ID' : 'id';
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function joinColumnName($propertyName, $className = null)
95
    {
96
        return $this->underscore($propertyName) . '_' . $this->referenceColumnName();
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
103
    {
104
        return $this->classToTableName($sourceEntity) . '_' . $this->classToTableName($targetEntity);
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function joinKeyColumnName($entityName, $referencedColumnName = null)
111
    {
112
        return $this->classToTableName($entityName) . '_' .
113
                ($referencedColumnName ?: $this->referenceColumnName());
114
    }
115
116
    /**
117
     * @param string $string
118
     *
119
     * @return string
120
     */
121
    private function underscore($string)
122
    {
123
        $string = preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $string);
124
        $string = Inflector::pluralize($string);
125
126
        if ($this->case === CASE_UPPER) {
127
            return strtoupper($string);
128
        }
129
130
        return strtolower($string);
131
    }
132
}
133