Completed
Pull Request — master (#7724)
by
unknown
09:18
created

UnderscorePluralNamingStrategy::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
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
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 plural 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 4
    public function classToTableName($className)
59
    {
60 4
        return $this->_classToTableName($className, true);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 4
    public function propertyToColumnName($propertyName, $className = null)
67
    {
68 4
        return $this->underscore($propertyName);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className = null, $embeddedClassName = null)
75
    {
76
        return $this->underscore($propertyName) . '_' . $embeddedColumnName;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 6
    public function referenceColumnName()
83
    {
84 6
        return $this->case === CASE_UPPER ? 'ID' : 'id';
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 2
    public function joinColumnName($propertyName, $className = null)
91
    {
92 2
        return $this->underscore($propertyName) . '_' . $this->referenceColumnName();
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 6
    public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
99
    {
100 6
        return $this->_classToTableName($sourceEntity) . '_' . $this->_classToTableName($targetEntity);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 4
    public function joinKeyColumnName($entityName, $referencedColumnName = null)
107
    {
108 4
        return $this->_classToTableName($entityName) . '_' .
109 4
                ($referencedColumnName ?: $this->referenceColumnName());
110
    }
111
112
    /**
113
     * @param string $string
114
     * @param bool $pluralize
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
115
     *
116
     * @return string
117
     */
118 14
    private function _classToTableName($className, $pluralize = false)
119
    {
120 14
        if (strpos($className, '\\') !== false) {
121 12
            $className = substr($className, strrpos($className, '\\') + 1);
122
        }
123
124 14
        if ($pluralize) {
125 4
            $className = Inflector::pluralize($className);
126
        }
127
128 14
        return $this->underscore($className);
129
    }
130
131
    /**
132
     * @param string $string
133
     *
134
     * @return string
135
     */
136 20
    private function underscore($string)
137
    {
138 20
        $string = preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $string);
139
140 20
        if ($this->case === CASE_UPPER) {
141 11
            return strtoupper($string);
142
        }
143
144 9
        return strtolower($string);
145
    }
146
}
147