Add proper types to DatabaseTableIndex
[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 final class DatabaseTableIndex
17 {
18 use TDroppableDatabaseComponent;
19
20 /**
21 * indexed columns
22 * @var string[]
23 */
24 protected array $columns;
25
26 /**
27 * is `true` if index name has been automatically generated
28 */
29 protected bool $generatedName = false;
30
31 /**
32 * name of index
33 */
34 protected string $name;
35
36 /**
37 * type of index (see `*_TYPE` constants)
38 */
39 protected ?string $type = null;
40
41 const DEFAULT_TYPE = null;
42
43 const PRIMARY_TYPE = 'PRIMARY';
44
45 const UNIQUE_TYPE = 'UNIQUE';
46
47 const FULLTEXT_TYPE = 'FULLTEXT';
48
49 /**
50 * Creates a new `DatabaseTableIndex` object.
51 */
52 protected function __construct(string $name)
53 {
54 $this->name = $name;
55 }
56
57 /**
58 * Sets the indexed columns and returns the index.
59 *
60 * @param string[] $columns indexed columns
61 * @return $this
62 */
63 public function columns(array $columns): self
64 {
65 $this->columns = \array_values($columns);
66
67 return $this;
68 }
69
70 /**
71 * Sets the automatically generated name of the index.
72 *
73 * @return $this
74 */
75 public function generatedName(string $name): self
76 {
77 $this->name($name);
78 $this->generatedName = true;
79
80 return $this;
81 }
82
83 /**
84 * Returns the index columns.
85 *
86 * @return string[]
87 */
88 public function getColumns(): array
89 {
90 if (!isset($this->columns)) {
91 throw new \BadMethodCallException(
92 "Before getting the columns, they must be set for index '{$this->getName()}'."
93 );
94 }
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{columns: string, type: string}
103 */
104 public function getData(): array
105 {
106 return [
107 'columns' => \implode(',', $this->getColumns()),
108 'type' => $this->getType(),
109 ];
110 }
111
112 /**
113 * Returns the name of the index.
114 */
115 public function getName(): string
116 {
117 return $this->name;
118 }
119
120 /**
121 * Returns the type of the index (see `*_TYPE` constants).
122 */
123 public function getType(): ?string
124 {
125 return $this->type;
126 }
127
128 /**
129 * Returns `true` if the name of the index has been automatically generated.
130 */
131 public function hasGeneratedName(): bool
132 {
133 return $this->generatedName;
134 }
135
136 /**
137 * Sets the name of the index.
138 *
139 * @return $this
140 */
141 public function name(string $name): self
142 {
143 $this->name = $name;
144
145 return $this;
146 }
147
148 /**
149 * Sets the type of the index and returns the index
150 *
151 * @throws \InvalidArgumentException if given type is invalid
152 */
153 public function type(?string $type): self
154 {
155 if (
156 $type !== static::DEFAULT_TYPE
157 && $type !== static::PRIMARY_TYPE
158 && $type !== static::UNIQUE_TYPE
159 && $type !== static::FULLTEXT_TYPE
160 ) {
161 throw new \InvalidArgumentException("Unknown index type '{$type}'.");
162 }
163
164 $this->type = $type;
165
166 return $this;
167 }
168
169 /**
170 * Returns a `DatabaseTableIndex` object with the given name.
171 */
172 public static function create(string $name = ''): self
173 {
174 return new self($name);
175 }
176
177 /**
178 * Returns a `DatabaseTableIndex` object with the given name and data.
179 *
180 * @param array $data data returned by `DatabaseEditor::getIndexInformation()`
181 */
182 public static function createFromData(string $name, array $data): self
183 {
184 return self::create($name)
185 ->type($data['type'])
186 ->columns($data['columns']);
187 }
188 }