1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Doctrine Naming Strategy Bundle, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2015 RunOpenCode |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace RunOpenCode\Bundle\DoctrineNamingStrategy\NamingStrategy; |
11
|
|
|
|
12
|
|
|
use Doctrine\ORM\Mapping\NamingStrategy; |
13
|
|
|
|
14
|
|
|
class NamerCollection implements NamingStrategy |
15
|
|
|
{ |
16
|
|
|
const UNDERSCORE = 'underscore'; |
17
|
|
|
const NOTHING = 'nothing'; |
18
|
|
|
const UCFIRST = 'ucfirst'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var NamingStrategy |
22
|
|
|
*/ |
23
|
|
|
protected $defaultNamer; |
24
|
|
|
|
25
|
|
|
/** |
26
|
2 |
|
* @var NamingStrategy[] |
27
|
|
|
*/ |
28
|
2 |
|
protected $concurrentNamers; |
29
|
2 |
|
|
30
|
2 |
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $concatenation; |
34
|
|
|
|
35
|
2 |
|
public function __construct(NamingStrategy $defaultNamer, array $concurrentNamers, $concatenation = self::UNDERSCORE) |
36
|
|
|
{ |
37
|
2 |
|
$this->defaultNamer = $defaultNamer; |
38
|
|
|
$this->concurrentNamers = $concurrentNamers; |
39
|
|
|
$this->concatenation = $concatenation; |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
2 |
|
*/ |
45
|
2 |
|
public function classToTableName($className) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$defaultName = $this->defaultNamer->classToTableName($className); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var NamingStrategy $concurrentNamer |
51
|
|
|
*/ |
52
|
|
|
foreach ($this->concurrentNamers as $concurrentNamer) { |
53
|
|
|
|
54
|
|
|
if (($newProposal = $concurrentNamer->classToTableName($className)) != $defaultName) { |
55
|
|
|
return $newProposal; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $defaultName; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function propertyToColumnName($propertyName, $className = null) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$defaultName = $this->defaultNamer->propertyToColumnName($propertyName, $className); |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var NamingStrategy $concurrentNamer |
71
|
|
|
*/ |
72
|
|
|
foreach ($this->concurrentNamers as $concurrentNamer) { |
73
|
|
|
|
74
|
|
|
if (($newProposal = $concurrentNamer->propertyToColumnName($propertyName, $className)) != $defaultName) { |
75
|
|
|
return $newProposal; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $defaultName; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className = null, $embeddedClassName = null) |
86
|
|
|
{ |
87
|
|
|
$defaultName = $this->defaultNamer->embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className, $embeddedClassName); |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @var NamingStrategy $concurrentNamer |
91
|
|
|
*/ |
92
|
|
|
foreach ($this->concurrentNamers as $concurrentNamer) { |
93
|
|
|
|
94
|
|
|
if (($newProposal = $concurrentNamer->embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className, $embeddedClassName)) != $defaultName) { |
95
|
|
|
return $newProposal; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $defaultName; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
public function referenceColumnName() |
106
|
|
|
{ |
107
|
|
|
$defaultName = $this->defaultNamer->referenceColumnName(); |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @var NamingStrategy $concurrentNamer |
111
|
|
|
*/ |
112
|
|
|
foreach ($this->concurrentNamers as $concurrentNamer) { |
113
|
|
|
|
114
|
|
|
if (($newProposal = $concurrentNamer->referenceColumnName()) != $defaultName) { |
115
|
|
|
return $newProposal; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $defaultName; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
public function joinColumnName($propertyName/*, $className = null*/) |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$defaultName = $this->defaultNamer->joinColumnName($propertyName/*, $className */); |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @var NamingStrategy $concurrentNamer |
131
|
|
|
*/ |
132
|
|
|
foreach ($this->concurrentNamers as $concurrentNamer) { |
133
|
|
|
|
134
|
|
|
if (($newProposal = $concurrentNamer->joinColumnName($propertyName/*, $className */)) != $defaultName) { |
135
|
|
|
return $newProposal; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $defaultName; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
|
View Code Duplication |
public function joinTableName($sourceEntity, $targetEntity, $propertyName = null) |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
switch ($this->concatenation) { |
148
|
|
|
case self::UCFIRST: |
149
|
|
|
return $this->classToTableName($sourceEntity) . ucfirst($this->classToTableName($targetEntity)); |
150
|
|
|
break; |
|
|
|
|
151
|
|
|
case self::NOTHING: |
152
|
|
|
return $this->classToTableName($sourceEntity) . $this->classToTableName($targetEntity); |
153
|
|
|
break; |
|
|
|
|
154
|
|
|
case self::UNDERSCORE: // FALL TROUGH |
155
|
|
|
default: |
156
|
|
|
return $this->classToTableName($sourceEntity) . '_' . $this->classToTableName($targetEntity); |
157
|
|
|
break; |
|
|
|
|
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritdoc} |
163
|
|
|
*/ |
164
|
|
View Code Duplication |
public function joinKeyColumnName($entityName, $referencedColumnName = null) |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
switch ($this->concatenation) { |
167
|
|
|
case self::UCFIRST: |
168
|
|
|
return $this->classToTableName($entityName) . ucfirst(($referencedColumnName ?: $this->referenceColumnName())); |
169
|
|
|
break; |
|
|
|
|
170
|
|
|
case self::NOTHING: |
171
|
|
|
return $this->classToTableName($entityName) . ($referencedColumnName ?: $this->referenceColumnName()); |
172
|
|
|
break; |
|
|
|
|
173
|
|
|
case self::UNDERSCORE: // FALL TROUGH |
174
|
|
|
default: |
175
|
|
|
return $this->classToTableName($entityName) . '_' . ($referencedColumnName ?: $this->referenceColumnName()); |
176
|
|
|
break; |
|
|
|
|
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.