Failed Conditions
Push — master ( 94cec7...7f79d0 )
by Marco
25s queued 13s
created

ForeignKeyConstraint::getLocalTableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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