Failed Conditions
Push — master ( 30b923...92920e )
by Marco
19s queued 13s
created

lib/Doctrine/DBAL/Schema/ForeignKeyConstraint.php (1 issue)

1
<?php
2
3
namespace Doctrine\DBAL\Schema;
4
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
use function array_keys;
7
use function array_map;
8
use function in_array;
9
use function strrpos;
10
use function strtolower;
11
use function strtoupper;
12
use function substr;
13
14
/**
15
 * An abstraction class for a foreign key constraint.
16
 */
17
class ForeignKeyConstraint extends AbstractAsset implements Constraint
18
{
19
    /**
20
     * Instance of the referencing table the foreign key constraint is associated with.
21
     *
22
     * @var Table
23
     */
24
    protected $_localTable;
25
26
    /**
27
     * Asset identifier instances of the referencing table column names the foreign key constraint is associated with.
28
     * array($columnName => Identifier)
29
     *
30
     * @var Identifier[]
31
     */
32
    protected $_localColumnNames;
33
34
    /**
35
     * Table or asset identifier instance of the referenced table name the foreign key constraint is associated with.
36
     *
37
     * @var Table|Identifier
38
     */
39
    protected $_foreignTableName;
40
41
    /**
42
     * Asset identifier instances of the referenced table column names the foreign key constraint is associated with.
43
     * array($columnName => Identifier)
44
     *
45
     * @var Identifier[]
46
     */
47
    protected $_foreignColumnNames;
48
49
    /**
50
     * Options associated with the foreign key constraint.
51
     *
52
     * @var mixed[]
53
     */
54
    protected $_options;
55
56
    /**
57
     * Initializes the foreign key constraint.
58
     *
59
     * @param string[]     $localColumnNames   Names of the referencing table columns.
60
     * @param Table|string $foreignTableName   Referenced table.
61
     * @param string[]     $foreignColumnNames Names of the referenced table columns.
62
     * @param string|null  $name               Name of the foreign key constraint.
63
     * @param mixed[]      $options            Options associated with the foreign key constraint.
64
     */
65 6783
    public function __construct(array $localColumnNames, $foreignTableName, array $foreignColumnNames, $name = null, array $options = [])
66
    {
67 6783
        if ($name !== null) {
68 5891
            $this->_setName($name);
69
        }
70
71 6783
        $this->_localColumnNames = $this->createIdentifierMap($localColumnNames);
72
73 6783
        if ($foreignTableName instanceof Table) {
74 1510
            $this->_foreignTableName = $foreignTableName;
75
        } else {
76 5341
            $this->_foreignTableName = new Identifier($foreignTableName);
77
        }
78
79 6783
        $this->_foreignColumnNames = $this->createIdentifierMap($foreignColumnNames);
80 6783
        $this->_options            = $options;
81 6783
    }
82
83
    /**
84
     * @param string[] $names
85
     *
86
     * @return Identifier[]
87
     */
88 6783
    private function createIdentifierMap(array $names) : array
89
    {
90 6783
        $identifiers = [];
91
92 6783
        foreach ($names as $name) {
93 5811
            $identifiers[$name] = new Identifier($name);
94
        }
95
96 6783
        return $identifiers;
97
    }
98
99
    /**
100
     * Returns the name of the referencing table
101
     * the foreign key constraint is associated with.
102
     *
103
     * @return string
104
     */
105 54
    public function getLocalTableName()
106
    {
107 54
        return $this->_localTable->getName();
108
    }
109
110
    /**
111
     * Sets the Table instance of the referencing table
112
     * the foreign key constraint is associated with.
113
     *
114
     * @param Table $table Instance of the referencing table.
115
     *
116
     * @return void
117
     */
118 3139
    public function setLocalTable(Table $table)
119
    {
120 3139
        $this->_localTable = $table;
121 3139
    }
122
123
    /**
124
     * @return Table
125
     */
126 27
    public function getLocalTable()
127
    {
128 27
        return $this->_localTable;
129
    }
130
131
    /**
132
     * Returns the names of the referencing table columns
133
     * the foreign key constraint is associated with.
134
     *
135
     * @return string[]
136
     */
137 4136
    public function getLocalColumns()
138
    {
139 4136
        return array_keys($this->_localColumnNames);
140
    }
141
142
    /**
143
     * Returns the quoted representation of the referencing table column names
144
     * the foreign key constraint is associated with.
145
     *
146
     * But only if they were defined with one or the referencing table column name
147
     * is a keyword reserved by the platform.
148
     * Otherwise the plain unquoted value as inserted is returned.
149
     *
150
     * @param AbstractPlatform $platform The platform to use for quotation.
151
     *
152
     * @return string[]
153
     */
154 3682
    public function getQuotedLocalColumns(AbstractPlatform $platform)
155
    {
156 3682
        $columns = [];
157
158 3682
        foreach ($this->_localColumnNames as $column) {
159 3574
            $columns[] = $column->getQuotedName($platform);
160
        }
161
162 3682
        return $columns;
163
    }
164
165
    /**
166
     * Returns unquoted representation of local table column names for comparison with other FK
167
     *
168
     * @return string[]
169
     */
170 248
    public function getUnquotedLocalColumns()
171
    {
172 248
        return array_map([$this, 'trimQuotes'], $this->getLocalColumns());
173
    }
174
175
    /**
176
     * Returns unquoted representation of foreign table column names for comparison with other FK
177
     *
178
     * @return string[]
179
     */
180 195
    public function getUnquotedForeignColumns()
181
    {
182 195
        return array_map([$this, 'trimQuotes'], $this->getForeignColumns());
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     *
188
     * @see getLocalColumns
189
     */
190 3139
    public function getColumns()
191
    {
192 3139
        return $this->getLocalColumns();
193
    }
194
195
    /**
196
     * Returns the quoted representation of the referencing table column names
197
     * the foreign key constraint is associated with.
198
     *
199
     * But only if they were defined with one or the referencing table column name
200
     * is a keyword reserved by the platform.
201
     * Otherwise the plain unquoted value as inserted is returned.
202
     *
203
     * @see getQuotedLocalColumns
204
     *
205
     * @param AbstractPlatform $platform The platform to use for quotation.
206
     *
207
     * @return string[]
208
     */
209 351
    public function getQuotedColumns(AbstractPlatform $platform)
210
    {
211 351
        return $this->getQuotedLocalColumns($platform);
212
    }
213
214
    /**
215
     * Returns the name of the referenced table
216
     * the foreign key constraint is associated with.
217
     *
218
     * @return string
219
     */
220 2359
    public function getForeignTableName()
221
    {
222 2359
        return $this->_foreignTableName->getName();
223
    }
224
225
    /**
226
     * Returns the non-schema qualified foreign table name.
227
     *
228
     * @return string
229
     */
230 195
    public function getUnqualifiedForeignTableName()
231
    {
232 195
        $name     = $this->_foreignTableName->getName();
233 195
        $position = strrpos($name, '.');
234
235 195
        if ($position !== false) {
236 27
            $name = substr($name, $position);
237
        }
238
239 195
        return strtolower($name);
240
    }
241
242
    /**
243
     * Returns the quoted representation of the referenced table name
244
     * the foreign key constraint is associated with.
245
     *
246
     * But only if it was defined with one or the referenced table name
247
     * is a keyword reserved by the platform.
248
     * Otherwise the plain unquoted value as inserted is returned.
249
     *
250
     * @param AbstractPlatform $platform The platform to use for quotation.
251
     *
252
     * @return string
253
     */
254 3682
    public function getQuotedForeignTableName(AbstractPlatform $platform)
255
    {
256 3682
        return $this->_foreignTableName->getQuotedName($platform);
257
    }
258
259
    /**
260
     * Returns the names of the referenced table columns
261
     * the foreign key constraint is associated with.
262
     *
263
     * @return string[]
264
     */
265 2440
    public function getForeignColumns()
266
    {
267 2440
        return array_keys($this->_foreignColumnNames);
268
    }
269
270
    /**
271
     * Returns the quoted representation of the referenced table column names
272
     * the foreign key constraint is associated with.
273
     *
274
     * But only if they were defined with one or the referenced table column name
275
     * is a keyword reserved by the platform.
276
     * Otherwise the plain unquoted value as inserted is returned.
277
     *
278
     * @param AbstractPlatform $platform The platform to use for quotation.
279
     *
280
     * @return string[]
281
     */
282 3682
    public function getQuotedForeignColumns(AbstractPlatform $platform)
283
    {
284 3682
        $columns = [];
285
286 3682
        foreach ($this->_foreignColumnNames as $column) {
287 3574
            $columns[] = $column->getQuotedName($platform);
288
        }
289
290 3682
        return $columns;
291
    }
292
293
    /**
294
     * Returns whether or not a given option
295
     * is associated with the foreign key constraint.
296
     *
297
     * @param string $name Name of the option to check.
298
     *
299
     * @return bool
300
     */
301 3196
    public function hasOption($name)
302
    {
303 3196
        return isset($this->_options[$name]);
304
    }
305
306
    /**
307
     * Returns an option associated with the foreign key constraint.
308
     *
309
     * @param string $name Name of the option the foreign key constraint is associated with.
310
     *
311
     * @return mixed
312
     */
313 458
    public function getOption($name)
314
    {
315 458
        return $this->_options[$name];
316
    }
317
318
    /**
319
     * Returns the options associated with the foreign key constraint.
320
     *
321
     * @return mixed[]
322
     */
323 136
    public function getOptions()
324
    {
325 136
        return $this->_options;
326
    }
327
328
    /**
329
     * Returns the referential action for UPDATE operations
330
     * on the referenced table the foreign key constraint is associated with.
331
     *
332
     * @return string|null
333
     */
334 168
    public function onUpdate()
335
    {
336 168
        return $this->onEvent('onUpdate');
337
    }
338
339
    /**
340
     * Returns the referential action for DELETE operations
341
     * on the referenced table the foreign key constraint is associated with.
342
     *
343
     * @return string|null
344
     */
345 141
    public function onDelete()
346
    {
347 141
        return $this->onEvent('onDelete');
348
    }
349
350
    /**
351
     * Returns the referential action for a given database operation
352
     * on the referenced table the foreign key constraint is associated with.
353
     *
354
     * @param string $event Name of the database operation/event to return the referential action for.
355
     *
356
     * @return string|null
357
     */
358 168
    private function onEvent($event)
359
    {
360 168
        if (isset($this->_options[$event])) {
361 81
            $onEvent = strtoupper($this->_options[$event]);
362
363 81
            if (! in_array($onEvent, ['NO ACTION', 'RESTRICT'])) {
364 27
                return $onEvent;
365
            }
366
        }
367
368 168
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type null|string.
Loading history...
369
    }
370
371
    /**
372
     * Checks whether this foreign key constraint intersects the given index columns.
373
     *
374
     * Returns `true` if at least one of this foreign key's local columns
375
     * matches one of the given index's columns, `false` otherwise.
376
     *
377
     * @param Index $index The index to be checked against.
378
     *
379
     * @return bool
380
     */
381 442
    public function intersectsIndexColumns(Index $index)
382
    {
383 442
        foreach ($index->getColumns() as $indexColumn) {
384 442
            foreach ($this->_localColumnNames as $localColumn) {
385 442
                if (strtolower($indexColumn) === strtolower($localColumn->getName())) {
386 392
                    return true;
387
                }
388
            }
389
        }
390
391 108
        return false;
392
    }
393
}
394