|
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_entity' or 'MY_ENTITY'. |
|
20
|
|
|
*/ |
|
21
|
|
|
class UnderscoreNamingStrategy implements NamingStrategy |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var int */ |
|
24
|
|
|
private $case; |
|
25
|
|
|
|
|
26
|
|
|
/** @var bool */ |
|
27
|
|
|
private $plural; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Underscore naming strategy construct. |
|
31
|
|
|
* |
|
32
|
|
|
* @param int $case CASE_LOWER | CASE_UPPER |
|
|
|
|
|
|
33
|
|
|
* @param bool $plural |
|
34
|
|
|
*/ |
|
35
|
2 |
|
public function __construct($case = CASE_LOWER, $plural = false) |
|
36
|
|
|
{ |
|
37
|
2 |
|
$this->case = $case; |
|
|
|
|
|
|
38
|
2 |
|
$this->plural = $plural; |
|
39
|
2 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return int CASE_LOWER | CASE_UPPER |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getCase() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->case; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Sets string case CASE_LOWER | CASE_UPPER. |
|
51
|
|
|
* Alphabetic characters converted to lowercase or uppercase. |
|
52
|
|
|
* |
|
53
|
|
|
* @param int $case |
|
54
|
|
|
*/ |
|
55
|
|
|
public function setCase($case) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->case = $case; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
|
|
public function isPlural() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->plural; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Set naming as plural |
|
70
|
|
|
* Converts 'MyEntity' to 'my_entities' or 'MY_ENTITIES'. |
|
71
|
|
|
* |
|
72
|
|
|
* @param bool $plural |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setPlural($plural) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->plural = $plural; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
10 |
|
public function classToTableName(string $className) : string |
|
83
|
|
|
{ |
|
84
|
10 |
|
return $this->_classToTableName($className, $this->plural); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
10 |
|
public function propertyToColumnName(string $propertyName, ?string $className = null) : string |
|
91
|
|
|
{ |
|
92
|
10 |
|
return $this->underscore($propertyName); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* {@inheritdoc} |
|
97
|
|
|
*/ |
|
98
|
|
|
public function embeddedFieldToColumnName( |
|
99
|
|
|
string $propertyName, |
|
100
|
|
|
string $embeddedColumnName, |
|
101
|
|
|
?string $className = null, |
|
102
|
|
|
?string $embeddedClassName = null |
|
103
|
|
|
) : string { |
|
104
|
|
|
return $this->underscore($propertyName) . '_' . $embeddedColumnName; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* {@inheritdoc} |
|
109
|
|
|
*/ |
|
110
|
10 |
|
public function referenceColumnName() : string |
|
111
|
|
|
{ |
|
112
|
10 |
|
return $this->case === CASE_UPPER ? 'ID' : 'id'; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* {@inheritdoc} |
|
117
|
|
|
*/ |
|
118
|
4 |
|
public function joinColumnName(string $propertyName, ?string $className = null) : string |
|
119
|
|
|
{ |
|
120
|
4 |
|
return $this->underscore($propertyName) . '_' . $this->referenceColumnName(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* {@inheritdoc} |
|
125
|
|
|
*/ |
|
126
|
12 |
|
public function joinTableName(string $sourceEntity, string $targetEntity, ?string $propertyName = null) : string |
|
127
|
|
|
{ |
|
128
|
12 |
|
return $this->_classToTableName($sourceEntity) . '_' . $this->_classToTableName($targetEntity); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* {@inheritdoc} |
|
133
|
|
|
*/ |
|
134
|
8 |
|
public function joinKeyColumnName(string $entityName, ?string $referencedColumnName = null) : string |
|
135
|
|
|
{ |
|
136
|
8 |
|
return $this->_classToTableName($entityName) . '_' . |
|
137
|
8 |
|
($referencedColumnName ?: $this->referenceColumnName()); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
30 |
|
private function _classToTableName(string $className, bool $pluralize = false) : string |
|
141
|
|
|
{ |
|
142
|
30 |
|
if (strpos($className, '\\') !== false) { |
|
143
|
26 |
|
$className = substr($className, strrpos($className, '\\') + 1); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
30 |
|
if ($pluralize) { |
|
147
|
4 |
|
$className = Inflector::pluralize($className); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
30 |
|
return $this->underscore($className); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
42 |
|
private function underscore(string $string) : string |
|
154
|
|
|
{ |
|
155
|
42 |
|
$string = preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $string); |
|
156
|
|
|
|
|
157
|
42 |
|
if ($this->case === CASE_UPPER) { |
|
158
|
24 |
|
return strtoupper($string); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
18 |
|
return strtolower($string); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|