1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace RunOpenCode\Bundle\DoctrineNamingStrategy\NamingStrategy; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Mapping\NamingStrategy; |
8
|
|
|
use RunOpenCode\Bundle\DoctrineNamingStrategy\Exception\InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @psalm-suppress UnusedClass |
12
|
|
|
*/ |
13
|
|
|
final class UnderscoredNamerCollection implements NamingStrategy |
14
|
|
|
{ |
15
|
|
|
protected NamingStrategy $defaultNamingStrategy; |
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var NamingStrategy[] |
19
|
|
|
*/ |
20
|
|
|
protected array $concurrentNamingStrategies; |
21
|
|
|
|
22
|
|
|
protected bool $joinTableFieldSuffix; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param NamingStrategy $defaultNamingStrategy |
26
|
|
|
* @param NamingStrategy[] $concurrentNamingStrategies |
27
|
|
|
* @param array<array-key,mixed> $configuration |
28
|
|
|
*/ |
29
|
8 |
|
public function __construct(NamingStrategy $defaultNamingStrategy, array $concurrentNamingStrategies = [], array $configuration = []) |
30
|
|
|
{ |
31
|
8 |
|
$this->defaultNamingStrategy = $defaultNamingStrategy; |
32
|
8 |
|
$this->concurrentNamingStrategies = []; |
33
|
|
|
|
34
|
8 |
|
foreach ($concurrentNamingStrategies as $namingStrategy) { |
35
|
8 |
|
$this->registerNamingStrategy($namingStrategy); |
36
|
|
|
} |
37
|
|
|
|
38
|
8 |
|
$configuration = \array_merge([ |
39
|
8 |
|
'join_table_field_suffix' => true, |
40
|
8 |
|
], $configuration); |
41
|
|
|
|
42
|
8 |
|
$this->joinTableFieldSuffix = (bool)$configuration['join_table_field_suffix']; |
43
|
8 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @throws InvalidArgumentException |
47
|
|
|
*/ |
48
|
8 |
|
public function registerNamingStrategy(NamingStrategy $namingStrategy): void |
49
|
|
|
{ |
50
|
8 |
|
if ($namingStrategy === $this->defaultNamingStrategy) { |
51
|
1 |
|
throw new InvalidArgumentException('Concurent naming strategy can not be default naming strategy.'); |
52
|
|
|
} |
53
|
|
|
|
54
|
8 |
|
$this->concurrentNamingStrategies[] = $namingStrategy; |
55
|
8 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
2 |
|
public function classToTableName($className): string |
61
|
|
|
{ |
62
|
2 |
|
return $this->findNamer($className)->classToTableName($className); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
2 |
|
public function propertyToColumnName($propertyName, $className = null): string |
69
|
|
|
{ |
70
|
2 |
|
return $this->findNamer($className)->propertyToColumnName($propertyName, $className); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
1 |
|
public function embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className = null, $embeddedClassName = null): string |
77
|
|
|
{ |
78
|
1 |
|
$namer = $this->findNamer($embeddedClassName); |
79
|
|
|
|
80
|
1 |
|
if ($namer === $this->defaultNamingStrategy && null !== $className) { |
81
|
1 |
|
$namer = $this->findNamer($className); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
return $namer->embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className, $embeddedClassName); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
1 |
|
public function referenceColumnName(): string |
91
|
|
|
{ |
92
|
1 |
|
return $this->defaultNamingStrategy->referenceColumnName(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
1 |
|
public function joinColumnName($propertyName): string |
99
|
|
|
{ |
100
|
1 |
|
return $this->findNamer()->joinColumnName($propertyName); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
1 |
|
public function joinTableName($sourceEntity, $targetEntity, $propertyName = null): string |
107
|
|
|
{ |
108
|
|
|
return |
109
|
1 |
|
$this->classToTableName($sourceEntity) . '_' . $this->classToTableName($targetEntity) |
110
|
|
|
. |
111
|
1 |
|
(($this->joinTableFieldSuffix && !empty($propertyName)) ? '_' . $this->propertyToColumnName($propertyName, $sourceEntity) : ''); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
1 |
|
public function joinKeyColumnName($entityName, $referencedColumnName = null): string |
118
|
|
|
{ |
119
|
1 |
|
$namer = $this->findNamer($entityName); |
120
|
1 |
|
$classTableName = $namer->classToTableName($entityName); |
121
|
1 |
|
$propertyColumnName = $namer->referenceColumnName(); |
122
|
|
|
|
123
|
1 |
|
if (null !== $referencedColumnName) { |
124
|
1 |
|
$propertyColumnName = $namer->propertyToColumnName($referencedColumnName) ?: $namer->referenceColumnName(); |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
return \sprintf('%s_%s', $classTableName, $propertyColumnName); |
128
|
|
|
} |
129
|
|
|
|
130
|
6 |
|
private function findNamer(?string $className = null): NamingStrategy |
131
|
|
|
{ |
132
|
6 |
|
if ($className === null) { |
133
|
3 |
|
return $this->defaultNamingStrategy; |
134
|
|
|
} |
135
|
|
|
|
136
|
5 |
|
$defaultName = \strtolower($this->defaultNamingStrategy->classToTableName($className)); |
137
|
|
|
|
138
|
5 |
|
foreach ($this->concurrentNamingStrategies as $concurrentNamer) { |
139
|
5 |
|
if (\strtolower($concurrentNamer->classToTableName($className)) !== $defaultName) { |
140
|
5 |
|
return $concurrentNamer; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
2 |
|
return $this->defaultNamingStrategy; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|