Exception
2 years ago
Visitor
9 months ago
AbstractAsset.php
2 years ago
AbstractSchemaManager.php
9 months ago
Column.php
2 years ago
ColumnDiff.php
2 years ago
Comparator.php
2 years ago
Constraint.php
2 years ago
DB2SchemaManager.php
2 years ago
DefaultSchemaManagerFactory.php
2 years ago
ForeignKeyConstraint.php
9 months ago
Identifier.php
2 years ago
Index.php
2 years ago
LegacySchemaManagerFactory.php
2 years ago
MySQLSchemaManager.php
9 months ago
OracleSchemaManager.php
2 years ago
PostgreSQLSchemaManager.php
9 months ago
SQLServerSchemaManager.php
9 months ago
Schema.php
2 years ago
SchemaConfig.php
2 years ago
SchemaDiff.php
2 years ago
SchemaException.php
2 years ago
SchemaManagerFactory.php
2 years ago
Sequence.php
2 years ago
SqliteSchemaManager.php
8 months ago
Table.php
2 years ago
TableDiff.php
2 years ago
UniqueConstraint.php
2 years ago
View.php
2 years ago
ForeignKeyConstraint.php
154 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Doctrine\DBAL\Schema; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Doctrine\DBAL\Platforms\AbstractPlatform; |
| 5 | use function array_keys; |
| 6 | use function array_map; |
| 7 | use function strrpos; |
| 8 | use function strtolower; |
| 9 | use function strtoupper; |
| 10 | use function substr; |
| 11 | class ForeignKeyConstraint extends AbstractAsset implements Constraint |
| 12 | { |
| 13 | protected $_localTable; |
| 14 | protected $_localColumnNames; |
| 15 | protected $_foreignTableName; |
| 16 | protected $_foreignColumnNames; |
| 17 | protected $_options; |
| 18 | public function __construct(array $localColumnNames, $foreignTableName, array $foreignColumnNames, $name = null, array $options = []) |
| 19 | { |
| 20 | if ($name !== null) { |
| 21 | $this->_setName($name); |
| 22 | } |
| 23 | $this->_localColumnNames = $this->createIdentifierMap($localColumnNames); |
| 24 | if ($foreignTableName instanceof Table) { |
| 25 | $this->_foreignTableName = $foreignTableName; |
| 26 | } else { |
| 27 | $this->_foreignTableName = new Identifier($foreignTableName); |
| 28 | } |
| 29 | $this->_foreignColumnNames = $this->createIdentifierMap($foreignColumnNames); |
| 30 | $this->_options = $options; |
| 31 | } |
| 32 | private function createIdentifierMap(array $names) : array |
| 33 | { |
| 34 | $identifiers = []; |
| 35 | foreach ($names as $name) { |
| 36 | $identifiers[$name] = new Identifier($name); |
| 37 | } |
| 38 | return $identifiers; |
| 39 | } |
| 40 | public function getLocalTableName() |
| 41 | { |
| 42 | return $this->_localTable->getName(); |
| 43 | } |
| 44 | public function setLocalTable(Table $table) |
| 45 | { |
| 46 | $this->_localTable = $table; |
| 47 | } |
| 48 | public function getLocalTable() |
| 49 | { |
| 50 | return $this->_localTable; |
| 51 | } |
| 52 | public function getLocalColumns() |
| 53 | { |
| 54 | return array_keys($this->_localColumnNames); |
| 55 | } |
| 56 | public function getQuotedLocalColumns(AbstractPlatform $platform) |
| 57 | { |
| 58 | $columns = []; |
| 59 | foreach ($this->_localColumnNames as $column) { |
| 60 | $columns[] = $column->getQuotedName($platform); |
| 61 | } |
| 62 | return $columns; |
| 63 | } |
| 64 | public function getUnquotedLocalColumns() |
| 65 | { |
| 66 | return array_map([$this, 'trimQuotes'], $this->getLocalColumns()); |
| 67 | } |
| 68 | public function getUnquotedForeignColumns() |
| 69 | { |
| 70 | return array_map([$this, 'trimQuotes'], $this->getForeignColumns()); |
| 71 | } |
| 72 | public function getColumns() |
| 73 | { |
| 74 | return $this->getLocalColumns(); |
| 75 | } |
| 76 | public function getQuotedColumns(AbstractPlatform $platform) |
| 77 | { |
| 78 | return $this->getQuotedLocalColumns($platform); |
| 79 | } |
| 80 | public function getForeignTableName() |
| 81 | { |
| 82 | return $this->_foreignTableName->getName(); |
| 83 | } |
| 84 | public function getUnqualifiedForeignTableName() |
| 85 | { |
| 86 | $name = $this->_foreignTableName->getName(); |
| 87 | $position = strrpos($name, '.'); |
| 88 | if ($position !== \false) { |
| 89 | $name = substr($name, $position + 1); |
| 90 | } |
| 91 | if ($this->isIdentifierQuoted($name)) { |
| 92 | $name = $this->trimQuotes($name); |
| 93 | } |
| 94 | return strtolower($name); |
| 95 | } |
| 96 | public function getQuotedForeignTableName(AbstractPlatform $platform) |
| 97 | { |
| 98 | return $this->_foreignTableName->getQuotedName($platform); |
| 99 | } |
| 100 | public function getForeignColumns() |
| 101 | { |
| 102 | return array_keys($this->_foreignColumnNames); |
| 103 | } |
| 104 | public function getQuotedForeignColumns(AbstractPlatform $platform) |
| 105 | { |
| 106 | $columns = []; |
| 107 | foreach ($this->_foreignColumnNames as $column) { |
| 108 | $columns[] = $column->getQuotedName($platform); |
| 109 | } |
| 110 | return $columns; |
| 111 | } |
| 112 | public function hasOption($name) |
| 113 | { |
| 114 | return isset($this->_options[$name]); |
| 115 | } |
| 116 | public function getOption($name) |
| 117 | { |
| 118 | return $this->_options[$name]; |
| 119 | } |
| 120 | public function getOptions() |
| 121 | { |
| 122 | return $this->_options; |
| 123 | } |
| 124 | public function onUpdate() |
| 125 | { |
| 126 | return $this->onEvent('onUpdate'); |
| 127 | } |
| 128 | public function onDelete() |
| 129 | { |
| 130 | return $this->onEvent('onDelete'); |
| 131 | } |
| 132 | private function onEvent($event) : ?string |
| 133 | { |
| 134 | if (isset($this->_options[$event])) { |
| 135 | $onEvent = strtoupper($this->_options[$event]); |
| 136 | if ($onEvent !== 'NO ACTION' && $onEvent !== 'RESTRICT') { |
| 137 | return $onEvent; |
| 138 | } |
| 139 | } |
| 140 | return null; |
| 141 | } |
| 142 | public function intersectsIndexColumns(Index $index) |
| 143 | { |
| 144 | foreach ($index->getColumns() as $indexColumn) { |
| 145 | foreach ($this->_localColumnNames as $localColumn) { |
| 146 | if (strtolower($indexColumn) === strtolower($localColumn->getName())) { |
| 147 | return \true; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | return \false; |
| 152 | } |
| 153 | } |
| 154 |