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
|
|
|
*/ |
25
|
|
|
namespace Phinx\Db\Action; |
26
|
|
|
|
27
|
|
|
use InvalidArgumentException; |
28
|
|
|
use Phinx\Db\Table\Index; |
29
|
|
|
use Phinx\Db\Table\Table; |
30
|
|
|
|
31
|
|
View Code Duplication |
class AddIndex extends Action |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* The index to add to the table |
35
|
|
|
* |
36
|
|
|
* @var Index |
37
|
|
|
*/ |
38
|
|
|
protected $index; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constructor |
42
|
|
|
* |
43
|
|
|
* @param Table $table The table to add the index to |
44
|
|
|
* @param Index $index The index to be added |
45
|
|
|
*/ |
46
|
|
|
public function __construct(Table $table, Index $index) |
47
|
|
|
{ |
48
|
|
|
parent::__construct($table); |
49
|
|
|
$this->index = $index; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Creates a new AddIndex object after building the index object with the |
54
|
|
|
* provided arguments |
55
|
|
|
* |
56
|
|
|
* @param Table $table The table to add the index to |
57
|
|
|
* @param mixed $columns The columns to index |
58
|
|
|
* @param array $options Additional options for the index creation |
59
|
|
|
* @return AddIndex |
60
|
|
|
*/ |
61
|
|
|
public static function build(Table $table, $columns, array $options = []) |
62
|
|
|
{ |
63
|
|
|
// create a new index object if strings or an array of strings were supplied |
64
|
|
|
$index = $columns; |
65
|
|
|
|
66
|
|
|
if (!$columns instanceof Index) { |
67
|
|
|
$index = new Index(); |
68
|
|
|
|
69
|
|
|
if (is_string($columns)) { |
70
|
|
|
$columns = [$columns]; // str to array |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$index->setColumns($columns); |
74
|
|
|
$index->setOptions($options); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return new static($table, $index); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns the index to be added |
82
|
|
|
* |
83
|
|
|
* @return Index |
84
|
|
|
*/ |
85
|
|
|
public function getIndex() |
86
|
|
|
{ |
87
|
|
|
return $this->index; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.