summaryrefslogtreecommitdiff
path: root/vendor/doctrine/dbal/src/Schema/ForeignKeyConstraint.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/doctrine/dbal/src/Schema/ForeignKeyConstraint.php')
-rw-r--r--vendor/doctrine/dbal/src/Schema/ForeignKeyConstraint.php291
1 files changed, 291 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/Schema/ForeignKeyConstraint.php b/vendor/doctrine/dbal/src/Schema/ForeignKeyConstraint.php
new file mode 100644
index 0000000..bb5ef7f
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Schema/ForeignKeyConstraint.php
@@ -0,0 +1,291 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Schema;
6
7use Doctrine\DBAL\Platforms\AbstractPlatform;
8
9use function array_keys;
10use function array_map;
11use function strrpos;
12use function strtolower;
13use function strtoupper;
14use function substr;
15
16/**
17 * An abstraction class for a foreign key constraint.
18 */
19class ForeignKeyConstraint extends AbstractAsset
20{
21 /**
22 * Asset identifier instances of the referencing table column names the foreign key constraint is associated with.
23 *
24 * @var array<string, Identifier>
25 */
26 protected array $_localColumnNames;
27
28 /**
29 * Table or asset identifier instance of the referenced table name the foreign key constraint is associated with.
30 */
31 protected Identifier $_foreignTableName;
32
33 /**
34 * Asset identifier instances of the referenced table column names the foreign key constraint is associated with.
35 *
36 * @var array<string, Identifier>
37 */
38 protected array $_foreignColumnNames;
39
40 /**
41 * Initializes the foreign key constraint.
42 *
43 * @param array<int, string> $localColumnNames Names of the referencing table columns.
44 * @param string $foreignTableName Referenced table.
45 * @param array<int, string> $foreignColumnNames Names of the referenced table columns.
46 * @param string $name Name of the foreign key constraint.
47 * @param array<string, mixed> $options Options associated with the foreign key constraint.
48 */
49 public function __construct(
50 array $localColumnNames,
51 string $foreignTableName,
52 array $foreignColumnNames,
53 string $name = '',
54 protected array $options = [],
55 ) {
56 $this->_setName($name);
57
58 $this->_localColumnNames = $this->createIdentifierMap($localColumnNames);
59 $this->_foreignTableName = new Identifier($foreignTableName);
60
61 $this->_foreignColumnNames = $this->createIdentifierMap($foreignColumnNames);
62 }
63
64 /**
65 * @param array<int, string> $names
66 *
67 * @return array<string, Identifier>
68 */
69 private function createIdentifierMap(array $names): array
70 {
71 $identifiers = [];
72
73 foreach ($names as $name) {
74 $identifiers[$name] = new Identifier($name);
75 }
76
77 return $identifiers;
78 }
79
80 /**
81 * Returns the names of the referencing table columns
82 * the foreign key constraint is associated with.
83 *
84 * @return array<int, string>
85 */
86 public function getLocalColumns(): array
87 {
88 return array_keys($this->_localColumnNames);
89 }
90
91 /**
92 * Returns the quoted representation of the referencing table column names
93 * the foreign key constraint is associated with.
94 *
95 * But only if they were defined with one or the referencing table column name
96 * is a keyword reserved by the platform.
97 * Otherwise the plain unquoted value as inserted is returned.
98 *
99 * @param AbstractPlatform $platform The platform to use for quotation.
100 *
101 * @return array<int, string>
102 */
103 public function getQuotedLocalColumns(AbstractPlatform $platform): array
104 {
105 $columns = [];
106
107 foreach ($this->_localColumnNames as $column) {
108 $columns[] = $column->getQuotedName($platform);
109 }
110
111 return $columns;
112 }
113
114 /**
115 * Returns unquoted representation of local table column names for comparison with other FK
116 *
117 * @return array<int, string>
118 */
119 public function getUnquotedLocalColumns(): array
120 {
121 return array_map($this->trimQuotes(...), $this->getLocalColumns());
122 }
123
124 /**
125 * Returns unquoted representation of foreign table column names for comparison with other FK
126 *
127 * @return array<int, string>
128 */
129 public function getUnquotedForeignColumns(): array
130 {
131 return array_map($this->trimQuotes(...), $this->getForeignColumns());
132 }
133
134 /**
135 * Returns the name of the referenced table
136 * the foreign key constraint is associated with.
137 */
138 public function getForeignTableName(): string
139 {
140 return $this->_foreignTableName->getName();
141 }
142
143 /**
144 * Returns the non-schema qualified foreign table name.
145 */
146 public function getUnqualifiedForeignTableName(): string
147 {
148 $name = $this->_foreignTableName->getName();
149 $position = strrpos($name, '.');
150
151 if ($position !== false) {
152 $name = substr($name, $position + 1);
153 }
154
155 return strtolower($name);
156 }
157
158 /**
159 * Returns the quoted representation of the referenced table name
160 * the foreign key constraint is associated with.
161 *
162 * But only if it was defined with one or the referenced table name
163 * is a keyword reserved by the platform.
164 * Otherwise the plain unquoted value as inserted is returned.
165 *
166 * @param AbstractPlatform $platform The platform to use for quotation.
167 */
168 public function getQuotedForeignTableName(AbstractPlatform $platform): string
169 {
170 return $this->_foreignTableName->getQuotedName($platform);
171 }
172
173 /**
174 * Returns the names of the referenced table columns
175 * the foreign key constraint is associated with.
176 *
177 * @return array<int, string>
178 */
179 public function getForeignColumns(): array
180 {
181 return array_keys($this->_foreignColumnNames);
182 }
183
184 /**
185 * Returns the quoted representation of the referenced table column names
186 * the foreign key constraint is associated with.
187 *
188 * But only if they were defined with one or the referenced table column name
189 * is a keyword reserved by the platform.
190 * Otherwise the plain unquoted value as inserted is returned.
191 *
192 * @param AbstractPlatform $platform The platform to use for quotation.
193 *
194 * @return array<int, string>
195 */
196 public function getQuotedForeignColumns(AbstractPlatform $platform): array
197 {
198 $columns = [];
199
200 foreach ($this->_foreignColumnNames as $column) {
201 $columns[] = $column->getQuotedName($platform);
202 }
203
204 return $columns;
205 }
206
207 /**
208 * Returns whether or not a given option
209 * is associated with the foreign key constraint.
210 */
211 public function hasOption(string $name): bool
212 {
213 return isset($this->options[$name]);
214 }
215
216 /**
217 * Returns an option associated with the foreign key constraint.
218 */
219 public function getOption(string $name): mixed
220 {
221 return $this->options[$name];
222 }
223
224 /**
225 * Returns the options associated with the foreign key constraint.
226 *
227 * @return array<string, mixed>
228 */
229 public function getOptions(): array
230 {
231 return $this->options;
232 }
233
234 /**
235 * Returns the referential action for UPDATE operations
236 * on the referenced table the foreign key constraint is associated with.
237 */
238 public function onUpdate(): ?string
239 {
240 return $this->onEvent('onUpdate');
241 }
242
243 /**
244 * Returns the referential action for DELETE operations
245 * on the referenced table the foreign key constraint is associated with.
246 */
247 public function onDelete(): ?string
248 {
249 return $this->onEvent('onDelete');
250 }
251
252 /**
253 * Returns the referential action for a given database operation
254 * on the referenced table the foreign key constraint is associated with.
255 *
256 * @param string $event Name of the database operation/event to return the referential action for.
257 */
258 private function onEvent(string $event): ?string
259 {
260 if (isset($this->options[$event])) {
261 $onEvent = strtoupper($this->options[$event]);
262
263 if ($onEvent !== 'NO ACTION' && $onEvent !== 'RESTRICT') {
264 return $onEvent;
265 }
266 }
267
268 return null;
269 }
270
271 /**
272 * Checks whether this foreign key constraint intersects the given index columns.
273 *
274 * Returns `true` if at least one of this foreign key's local columns
275 * matches one of the given index's columns, `false` otherwise.
276 *
277 * @param Index $index The index to be checked against.
278 */
279 public function intersectsIndexColumns(Index $index): bool
280 {
281 foreach ($index->getColumns() as $indexColumn) {
282 foreach ($this->_localColumnNames as $localColumn) {
283 if (strtolower($indexColumn) === strtolower($localColumn->getName())) {
284 return true;
285 }
286 }
287 }
288
289 return false;
290 }
291}