| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace UserAccessManager\Setup\Database; |
| 6 |
|
| 7 |
class Column |
| 8 |
{ |
| 9 |
public function __construct( |
| 10 |
private string $name, |
| 11 |
private string $type, |
| 12 |
private bool $isNull = false, |
| 13 |
private mixed $default = null, |
| 14 |
private bool $isKey = false, |
| 15 |
private bool $isAutoIncrement = false |
| 16 |
) { |
| 17 |
} |
| 18 |
|
| 19 |
public function getName(): string |
| 20 |
{ |
| 21 |
return $this->name; |
| 22 |
} |
| 23 |
|
| 24 |
public function getType(): string |
| 25 |
{ |
| 26 |
return $this->type; |
| 27 |
} |
| 28 |
|
| 29 |
public function getDefault(): int|string|null |
| 30 |
{ |
| 31 |
return $this->default; |
| 32 |
} |
| 33 |
|
| 34 |
public function isNull(): bool |
| 35 |
{ |
| 36 |
return $this->isNull; |
| 37 |
} |
| 38 |
|
| 39 |
public function isKey(): bool |
| 40 |
{ |
| 41 |
return $this->isKey; |
| 42 |
} |
| 43 |
|
| 44 |
public function isAutoIncrement(): bool |
| 45 |
{ |
| 46 |
return $this->isAutoIncrement; |
| 47 |
} |
| 48 |
|
| 49 |
public function __toString(): string |
| 50 |
{ |
| 51 |
$nullConstraint = ($this->isNull) ? 'NULL' : 'NOT NULL'; |
| 52 |
// MySQL reports a declared INT as INT(11); normalise so schema comparison sees no difference. |
| 53 |
$type = $this->type === 'INT(11)' ? 'INT' : $this->type; |
| 54 |
$column = "`$this->name` $type $nullConstraint"; |
| 55 |
|
| 56 |
if ($this->default === null && $this->isNull) { |
| 57 |
$column .= ' DEFAULT NULL'; |
| 58 |
} elseif ($this->default !== null) { |
| 59 |
$defaultValue = is_numeric($this->default) === false ? "'$this->default'" : $this->default; |
| 60 |
$column .= " DEFAULT $defaultValue"; |
| 61 |
} |
| 62 |
|
| 63 |
if ($this->isAutoIncrement) { |
| 64 |
$column .= ' AUTO_INCREMENT'; |
| 65 |
} |
| 66 |
|
| 67 |
return $column; |
| 68 |
} |
| 69 |
} |
| 70 |
|