Passed
Pull Request — master (#3233)
by Sergey
12:33
created

ForeignKeyConstraint::getForeignTableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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