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\Column; 28use Phinx\Db\Table\Table; 29 30class ChangeColumn extends Action 31{ 32 /** 33 * The column definition 34 * 35 * @var Column 36 */ 37 protected $column; 38 39 /** 40 * The name of the column to be changed 41 * 42 * @var string 43 */ 44 protected $columnName; 45 46 /** 47 * Constructor 48 * 49 * @param Table $table The table to alter 50 * @param mixed $columnName The name fo the column to change 51 * @param Column $column The column definition 52 */ 53 public function __construct(Table $table, $columnName, Column $column) 54 { 55 parent::__construct($table); 56 $this->columnName = $columnName; 57 $this->column = $column; 58 59 // if the name was omitted use the existing column name 60 if ($column->getName() === null || strlen($column->getName()) === 0) { 61 $column->setName($columnName); 62 } 63 } 64 65 /** 66 * Creates a new ChangeColumn object after building the column definition 67 * out of the provided arguments 68 * 69 * @param Table $table The table to alter 70 * @param mixed $columnName The name of the column to change 71 * @param mixed $type The type of the column 72 * @param mixed $options Additional options for the column 73 * @return ChangeColumn 74 */ 75 public static function build(Table $table, $columnName, $type = null, $options = []) 76 { 77 $column = new Column(); 78 $column->setName($columnName); 79 $column->setType($type); 80 $column->setOptions($options); // map options to column methods 81 82 return new static($table, $columnName, $column); 83 } 84 85 /** 86 * Returns the name of the column to change 87 * 88 * @return string 89 */ 90 public function getColumnName() 91 { 92 return $this->columnName; 93 } 94 95 /** 96 * Returns the column definition 97 * 98 * @return Column 99 */ 100 public function getColumn() 101 { 102 return $this->column; 103 } 104} 105