1 | <?php |
||
30 | class TableBlueprint |
||
31 | { |
||
32 | /** |
||
33 | * @var CapsuleInterface |
||
34 | */ |
||
35 | private $capsule = null; |
||
36 | |||
37 | /** |
||
38 | * Blueprint specific set of operations. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | private $operations = []; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private $table = ''; |
||
48 | |||
49 | /** |
||
50 | * @var null|string |
||
51 | */ |
||
52 | private $database = null; |
||
53 | |||
54 | /** |
||
55 | * @param CapsuleInterface $capsule |
||
56 | * @param string $table |
||
57 | * @param string|null $database |
||
58 | */ |
||
59 | public function __construct(CapsuleInterface $capsule, string $table, string $database = null) |
||
65 | |||
66 | /** |
||
67 | * Get associated table schema. |
||
68 | * |
||
69 | * @return AbstractTable |
||
70 | */ |
||
71 | public function getSchema(): AbstractTable |
||
72 | { |
||
73 | return $this->capsule->getSchema($this->table, $this->database); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Example: |
||
78 | * $table->addColumn('name', 'string', ['length' => 64]); |
||
79 | * $table->addColumn('status', 'enum', [ |
||
80 | * 'values' => ['active', 'disabled'] |
||
81 | * ]); |
||
82 | * |
||
83 | * @param string $name |
||
84 | * @param string $type |
||
85 | * @param array $options |
||
86 | * |
||
87 | * @return TableBlueprint |
||
88 | */ |
||
89 | public function addColumn(string $name, string $type, array $options = []): self |
||
95 | |||
96 | /** |
||
97 | * Example: |
||
98 | * $table->alterColumn('name', 'string', ['length' => 128]); |
||
99 | * |
||
100 | * @param string $name |
||
101 | * @param string $type |
||
102 | * @param array $options |
||
103 | * |
||
104 | * @return TableBlueprint |
||
105 | */ |
||
106 | public function alterColumn(string $name, string $type, array $options = []): self |
||
112 | |||
113 | /** |
||
114 | * Example: |
||
115 | * $table->renameColumn('column', 'new_name'); |
||
116 | * |
||
117 | * @param string $name |
||
118 | * @param string $newName |
||
119 | * |
||
120 | * @return TableBlueprint |
||
121 | */ |
||
122 | public function renameColumn(string $name, string $newName): self |
||
128 | |||
129 | /** |
||
130 | * Example: |
||
131 | * $table->dropColumn('email'); |
||
132 | * |
||
133 | * @param string $name |
||
134 | * |
||
135 | * @return TableBlueprint |
||
136 | */ |
||
137 | public function dropColumn(string $name): self |
||
143 | |||
144 | /** |
||
145 | * Example: |
||
146 | * $table->addIndex(['email'], ['unique' => true]); |
||
147 | * |
||
148 | * @param array $columns |
||
149 | * @param array $options |
||
150 | * |
||
151 | * @return TableBlueprint |
||
152 | */ |
||
153 | public function addIndex(array $columns, array $options = []): self |
||
159 | |||
160 | /** |
||
161 | * Example: |
||
162 | * $table->alterIndex(['email'], ['unique' => false]); |
||
163 | * |
||
164 | * @param array $columns |
||
165 | * @param array $options |
||
166 | * |
||
167 | * @return TableBlueprint |
||
168 | */ |
||
169 | public function alterIndex(array $columns, array $options): self |
||
175 | |||
176 | /** |
||
177 | * Example: |
||
178 | * $table->dropIndex(['email']); |
||
179 | * |
||
180 | * @param array $columns |
||
181 | * |
||
182 | * @return TableBlueprint |
||
183 | */ |
||
184 | public function dropIndex(array $columns): self |
||
190 | |||
191 | /** |
||
192 | * Example: |
||
193 | * $table->addForeignKey('user_id', 'users', 'id', ['delete' => 'CASCADE']); |
||
194 | * |
||
195 | * @param string $column |
||
196 | * @param string $foreignTable Database isolation prefix will be automatically added. |
||
197 | * @param string $foreignKey |
||
198 | * @param array $options |
||
199 | * |
||
200 | * @return TableBlueprint |
||
201 | */ |
||
202 | public function addForeignKey( |
||
219 | |||
220 | /** |
||
221 | * Example: |
||
222 | * $table->alterForeignKey('user_id', 'users', 'id', ['delete' => 'NO ACTION']); |
||
223 | * |
||
224 | * @param string $column |
||
225 | * @param string $foreignTable |
||
226 | * @param string $foreignKey |
||
227 | * @param array $options |
||
228 | * |
||
229 | * @return TableBlueprint |
||
230 | */ |
||
231 | public function alterForeignKey( |
||
248 | |||
249 | /** |
||
250 | * Example: |
||
251 | * $table->dropForeignKey('user_id'); |
||
252 | * |
||
253 | * @param string $column |
||
254 | * |
||
255 | * @return TableBlueprint |
||
256 | */ |
||
257 | public function dropForeignKey(string $column): self |
||
263 | |||
264 | /** |
||
265 | * Set table primary keys index. Attention, you can only call it when table being created. |
||
266 | * |
||
267 | * @param array $keys |
||
268 | * |
||
269 | * @return TableBlueprint |
||
270 | */ |
||
271 | public function setPrimaryKeys(array $keys): self |
||
277 | |||
278 | /** |
||
279 | * Create table schema. |
||
280 | */ |
||
281 | public function create() |
||
289 | |||
290 | /** |
||
291 | * Update table schema. |
||
292 | */ |
||
293 | public function update() |
||
301 | |||
302 | /** |
||
303 | * Drop table. |
||
304 | */ |
||
305 | public function drop() |
||
313 | |||
314 | /** |
||
315 | * Rename table. |
||
316 | * |
||
317 | * @param string $newName |
||
318 | */ |
||
319 | public function rename(string $newName) |
||
327 | |||
328 | /** |
||
329 | * Register new operation. |
||
330 | * |
||
331 | * @param OperationInterface $operation |
||
332 | * |
||
333 | * @return TableBlueprint |
||
334 | */ |
||
335 | public function addOperation(OperationInterface $operation): self |
||
341 | |||
342 | /** |
||
343 | * Execute blueprint operations. |
||
344 | */ |
||
345 | private function execute() |
||
349 | } |