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 const E_USER_DEPRECATED; |
26
|
|
|
use function preg_replace; |
27
|
|
|
use function strpos; |
28
|
|
|
use function strrpos; |
29
|
|
|
use function strtolower; |
30
|
|
|
use function strtoupper; |
31
|
|
|
use function substr; |
32
|
|
|
use function trigger_error; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Naming strategy implementing the underscore naming convention. |
36
|
|
|
* Converts 'MyEntity' to 'my_entity' or 'MY_ENTITY'. |
37
|
|
|
* |
38
|
|
|
* |
39
|
|
|
* @link www.doctrine-project.org |
40
|
|
|
* @since 2.3 |
41
|
|
|
* @author Fabio B. Silva <[email protected]> |
42
|
|
|
*/ |
43
|
|
|
class UnderscoreNamingStrategy implements NamingStrategy |
44
|
|
|
{ |
45
|
|
|
private const DEFAULT_PATTERN = '/(?<=[a-z])([A-Z])/'; |
46
|
|
|
private const NUMBER_AWARE_PATTERN = '/(?<=[a-z0-9])([A-Z])/'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var integer |
50
|
|
|
*/ |
51
|
|
|
private $case; |
52
|
|
|
|
53
|
|
|
/** @var string */ |
54
|
|
|
private $pattern; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Underscore naming strategy construct. |
58
|
|
|
* |
59
|
|
|
* @param int $case CASE_LOWER | CASE_UPPER |
60
|
|
|
*/ |
61
|
7 |
|
public function __construct($case = CASE_LOWER, bool $numberAware = false) |
62
|
|
|
{ |
63
|
7 |
|
if (! $numberAware) { |
64
|
7 |
|
@trigger_error( |
65
|
7 |
|
'Creating ' . self::class . ' without making it number aware is deprecated and will be removed in Doctrine 3.0.', |
66
|
7 |
|
E_USER_DEPRECATED |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
7 |
|
$this->case = $case; |
71
|
7 |
|
$this->pattern = $numberAware ? self::NUMBER_AWARE_PATTERN : self::DEFAULT_PATTERN; |
72
|
7 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return integer CASE_LOWER | CASE_UPPER |
76
|
|
|
*/ |
77
|
|
|
public function getCase() |
78
|
|
|
{ |
79
|
|
|
return $this->case; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Sets string case CASE_LOWER | CASE_UPPER. |
84
|
|
|
* Alphabetic characters converted to lowercase or uppercase. |
85
|
|
|
* |
86
|
|
|
* @param integer $case |
87
|
|
|
* |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
public function setCase($case) |
91
|
|
|
{ |
92
|
|
|
$this->case = $case; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
53 |
|
public function classToTableName($className) |
99
|
|
|
{ |
100
|
53 |
|
if (strpos($className, '\\') !== false) { |
101
|
45 |
|
$className = substr($className, strrpos($className, '\\') + 1); |
102
|
|
|
} |
103
|
|
|
|
104
|
53 |
|
return $this->underscore($className); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
22 |
|
public function propertyToColumnName($propertyName, $className = null) |
111
|
|
|
{ |
112
|
22 |
|
return $this->underscore($propertyName); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
public function embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className = null, $embeddedClassName = null) |
119
|
|
|
{ |
120
|
|
|
return $this->underscore($propertyName).'_'.$embeddedColumnName; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
23 |
|
public function referenceColumnName() |
127
|
|
|
{ |
128
|
23 |
|
return $this->case === CASE_UPPER ? 'ID' : 'id'; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
11 |
|
public function joinColumnName($propertyName, $className = null) |
135
|
|
|
{ |
136
|
11 |
|
return $this->underscore($propertyName) . '_' . $this->referenceColumnName(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
17 |
|
public function joinTableName($sourceEntity, $targetEntity, $propertyName = null) |
143
|
|
|
{ |
144
|
17 |
|
return $this->classToTableName($sourceEntity) . '_' . $this->classToTableName($targetEntity); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
17 |
|
public function joinKeyColumnName($entityName, $referencedColumnName = null) |
151
|
|
|
{ |
152
|
17 |
|
return $this->classToTableName($entityName) . '_' . |
153
|
17 |
|
($referencedColumnName ?: $this->referenceColumnName()); |
154
|
|
|
} |
155
|
|
|
|
156
|
79 |
|
private function underscore(string $string) : string |
157
|
|
|
{ |
158
|
79 |
|
$string = preg_replace($this->pattern, '_$1', $string); |
159
|
|
|
|
160
|
79 |
|
if ($this->case === CASE_UPPER) { |
161
|
45 |
|
return strtoupper($string); |
162
|
|
|
} |
163
|
|
|
|
164
|
34 |
|
return strtolower($string); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|