Merge branch '5.3' into 5.4
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / database / table / index / DatabaseTableIndex.class.php
1 <?php
2
3 namespace wcf\system\database\table\index;
4
5 use wcf\system\database\table\TDroppableDatabaseComponent;
6
7 /**
8 * Represents an index of a database table.
9 *
10 * @author Matthias Schmidt
11 * @copyright 2001-2019 WoltLab GmbH
12 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
13 * @package WoltLabSuite\Core\System\Database\Table\Index
14 * @since 5.2
15 */
16 class DatabaseTableIndex
17 {
18 use TDroppableDatabaseComponent;
19
20 /**
21 * indexed columns
22 * @var string[]
23 */
24 protected $columns;
25
26 /**
27 * is `true` if index name has been automatically generated
28 * @var bool
29 */
30 protected $generatedName = false;
31
32 /**
33 * name of index
34 * @var string
35 */
36 protected $name;
37
38 /**
39 * type of index (see `*_TYPE` constants)
40 * @var null|string
41 */
42 protected $type;
43
44 const DEFAULT_TYPE = null;
45
46 const PRIMARY_TYPE = 'PRIMARY';
47
48 const UNIQUE_TYPE = 'UNIQUE';
49
50 const FULLTEXT_TYPE = 'FULLTEXT';
51
52 /**
53 * Creates a new `DatabaseTableIndex` object.
54 *
55 * @param string $name column name
56 */
57 protected function __construct($name)
58 {
59 $this->name = $name;
60 }
61
62 /**
63 * Sets the indexed columns and returns the index.
64 *
65 * @param string[] $columns indexed columns
66 * @return $this this index
67 */
68 public function columns($columns)
69 {
70 $this->columns = \array_values($columns);
71
72 return $this;
73 }
74
75 /**
76 * Sets the automatically generated name of the index.
77 *
78 * @param string $name index name
79 * @return $this this index
80 */
81 public function generatedName($name)
82 {
83 $this->name($name);
84 $this->generatedName = true;
85
86 return $this;
87 }
88
89 /**
90 * Returns the index columns.
91 *
92 * @return string[]
93 */
94 public function getColumns()
95 {
96 return $this->columns;
97 }
98
99 /**
100 * Returns the data used by `DatabaseEditor` to add the index to a table.
101 *
102 * @return array
103 */
104 public function getData()
105 {
106 return [
107 'columns' => \implode(',', $this->columns),
108 'type' => $this->type,
109 ];
110 }
111
112 /**
113 * Returns the name of the index.
114 *
115 * @return string
116 */
117 public function getName()
118 {
119 return $this->name;
120 }
121
122 /**
123 * Returns the type of the index (see `*_TYPE` constants).
124 *
125 * @return null|string
126 */
127 public function getType()
128 {
129 return $this->type;
130 }
131
132 /**
133 * Returns `true` if the name of the index has been automatically generated.
134 *
135 * @return bool
136 */
137 public function hasGeneratedName()
138 {
139 return $this->generatedName;
140 }
141
142 /**
143 * Sets the name of the index.
144 *
145 * @param string $name index name
146 * @return $this this index
147 */
148 public function name($name)
149 {
150 $this->name = $name;
151
152 return $this;
153 }
154
155 /**
156 * Sets the type of the index and returns the index
157 *
158 * @param null|string $type index type
159 * @return $this this index
160 * @throws \InvalidArgumentException if given type is invalid
161 */
162 public function type($type)
163 {
164 if (
165 $type !== static::DEFAULT_TYPE
166 && $type !== static::PRIMARY_TYPE
167 && $type !== static::UNIQUE_TYPE
168 && $type !== static::FULLTEXT_TYPE
169 ) {
170 throw new \InvalidArgumentException("Unknown index type '{$type}'.");
171 }
172
173 $this->type = $type;
174
175 return $this;
176 }
177
178 /**
179 * Returns a `DatabaseTableIndex` object with the given name.
180 *
181 * @param string $name
182 * @return static
183 */
184 public static function create($name = '')
185 {
186 return new static($name);
187 }
188
189 /**
190 * Returns a `DatabaseTableIndex` object with the given name and data.
191 *
192 * @param string $name
193 * @param array $data data returned by `DatabaseEditor::getIndexInformation()`
194 * @return static
195 */
196 public static function createFromData($name, array $data)
197 {
198 return static::create($name)
199 ->type($data['type'])
200 ->columns($data['columns']);
201 }
202 }