1<?php 2/** 3 * Phinx 4 * 5 * (The MIT license) 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a copy 8 * of this software and associated * documentation files (the "Software"), to 9 * deal in the Software without restriction, including without limitation the 10 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 11 * sell copies of the Software, and to permit persons to whom the Software is 12 * furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 * IN THE SOFTWARE. 24 */ 25namespace Phinx\Db\Action; 26 27use Phinx\Db\Table\ForeignKey; 28use Phinx\Db\Table\Table; 29 30class DropForeignKey extends Action 31{ 32 33 /** 34 * The foreign key to remove 35 * 36 * @var ForeignKey 37 */ 38 protected $foreignKey; 39 40 /** 41 * Constructor 42 * 43 * @param Table $table The table to remove the constraint from 44 * @param ForeignKey $foreignKey The foreign key to remove 45 */ 46 public function __construct(Table $table, ForeignKey $foreignKey) 47 { 48 parent::__construct($table); 49 $this->foreignKey = $foreignKey; 50 } 51 52 /** 53 * Creates a new DropForeignKey object after building the ForeignKey 54 * definition out of the passed arguments. 55 * 56 * @param Table $table The table to delete the foreign key from 57 * @param string|string[] $columns The columns participating in the foreign key 58 * @param string|null $constraint The constraint name 59 * @return DropForeignKey 60 */ 61 public static function build(Table $table, $columns, $constraint = null) 62 { 63 if (is_string($columns)) { 64 $columns = [$columns]; 65 } 66 67 $foreignKey = new ForeignKey(); 68 $foreignKey->setColumns($columns); 69 70 if ($constraint) { 71 $foreignKey->setConstraint($constraint); 72 } 73 74 return new static($table, $foreignKey); 75 } 76 77 /** 78 * Returns the foreign key to remove 79 * 80 * @return ForeignKey 81 */ 82 public function getForeignKey() 83 { 84 return $this->foreignKey; 85 } 86} 87