/
TablePrimaryKey.php
60 lines (52 loc) · 1.19 KB
/
TablePrimaryKey.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
declare(strict_types=1);
namespace bizley\migration\table;
use yii\base\BaseObject;
/**
* Class TablePrimaryKey
* @package bizley\migration\table
*/
class TablePrimaryKey extends BaseObject
{
public const GENERIC_PRIMARY_KEY = 'PRIMARYKEY';
/**
* @var string
*/
public $name;
/**
* @var array
*/
public $columns = [];
/**
* Checks if primary key is composite.
* @return bool
*/
public function isComposite(): bool
{
return \count($this->columns) > 1;
}
/**
* Renders the key.
* @param TableStructure $table
* @param int $indent
* @return string
*/
public function render($table, $indent = 8): string
{
return str_repeat(' ', $indent) . "\$this->addPrimaryKey('"
. ($this->name ?: self::GENERIC_PRIMARY_KEY)
. "', '" . $table->renderName() . "', ['"
. implode("', '", $this->columns)
. "']);";
}
/**
* Adds column to the key.
* @param $name
*/
public function addColumn($name): void
{
if (!\in_array($name, $this->columns, true)) {
$this->columns[] = $name;
}
}
}