Merge branch '5.4' into 5.5
[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 if (!isset($this->columns)) {
97 throw new \BadMethodCallException(
98 "Before getting the columns, they must be set for index '{$this->getName()}'."
99 );
100 }
101
102 return $this->columns;
103 }
104
105 /**
106 * Returns the data used by `DatabaseEditor` to add the index to a table.
107 *
108 * @return array
109 */
110 public function getData()
111 {
112 return [
113 'columns' => \implode(',', $this->getColumns()),
114 'type' => $this->getType(),
115 ];
116 }
117
118 /**
119 * Returns the name of the index.
120 *
121 * @return string
122 */
123 public function getName()
124 {
125 return $this->name;
126 }
127
128 /**
129 * Returns the type of the index (see `*_TYPE` constants).
130 *
131 * @return null|string
132 */
133 public function getType()
134 {
135 return $this->type;
136 }
137
138 /**
139 * Returns `true` if the name of the index has been automatically generated.
140 *
141 * @return bool
142 */
143 public function hasGeneratedName()
144 {
145 return $this->generatedName;
146 }
147
148 /**
149 * Sets the name of the index.
150 *
151 * @param string $name index name
152 * @return $this this index
153 */
154 public function name($name)
155 {
156 $this->name = $name;
157
158 return $this;
159 }
160
161 /**
162 * Sets the type of the index and returns the index
163 *
164 * @param null|string $type index type
165 * @return $this this index
166 * @throws \InvalidArgumentException if given type is invalid
167 */
168 public function type($type)
169 {
170 if (
171 $type !== static::DEFAULT_TYPE
172 && $type !== static::PRIMARY_TYPE
173 && $type !== static::UNIQUE_TYPE
174 && $type !== static::FULLTEXT_TYPE
175 ) {
176 throw new \InvalidArgumentException("Unknown index type '{$type}'.");
177 }
178
179 $this->type = $type;
180
181 return $this;
182 }
183
184 /**
185 * Returns a `DatabaseTableIndex` object with the given name.
186 *
187 * @param string $name
188 * @return static
189 */
190 public static function create($name = '')
191 {
192 return new static($name);
193 }
194
195 /**
196 * Returns a `DatabaseTableIndex` object with the given name and data.
197 *
198 * @param string $name
199 * @param array $data data returned by `DatabaseEditor::getIndexInformation()`
200 * @return static
201 */
202 public static function createFromData($name, array $data)
203 {
204 return static::create($name)
205 ->type($data['type'])
206 ->columns($data['columns']);
207 }
208 }