| @@ -23,59 +23,125 @@ | ||
| 23 | 23 | * @package UserAccessManager\Setup\Database |
| 24 | 24 | */ |
| 25 | 25 | class Column |
| 26 | 26 | { |
| 27 | + /** | |
| 28 | + * @var string | |
| 29 | + */ | |
| 30 | + private $name; | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * @var string | |
| 34 | + */ | |
| 35 | + private $type; | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * @var int|string | |
| 39 | + */ | |
| 40 | + private $default; | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * @var bool | |
| 44 | + */ | |
| 45 | + private $isNull; | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * @var bool | |
| 49 | + */ | |
| 50 | + private $isKey; | |
| 51 | + | |
| 52 | + /** | |
| 53 | + * @var bool | |
| 54 | + */ | |
| 55 | + private $isAutoIncrement; | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * Column constructor. | |
| 59 | + * @param string $name | |
| 60 | + * @param string $type | |
| 61 | + * @param bool $isNull | |
| 62 | + * @param mixed $default | |
| 63 | + * @param bool $isKey | |
| 64 | + * @param bool $isAutoIncrement | |
| 65 | + */ | |
| 27 | 66 | public function __construct( |
| 28 | - private string $name, | |
| 29 | - private string $type, | |
| 30 | - private bool $isNull = false, | |
| 31 | - private mixed $default = null, | |
| 32 | - private bool $isKey = false, | |
| 33 | - private bool $isAutoIncrement = false | |
| 67 | + string $name, | |
| 68 | + string $type, | |
| 69 | + $isNull = false, | |
| 70 | + $default = null, | |
| 71 | + $isKey = false, | |
| 72 | + $isAutoIncrement = false | |
| 34 | 73 | ) { |
| 74 | + $this->name = $name; | |
| 75 | + $this->type = $type; | |
| 76 | + $this->isNull = $isNull; | |
| 77 | + $this->default = $default; | |
| 78 | + $this->isKey = $isKey; | |
| 79 | + $this->isAutoIncrement = $isAutoIncrement; | |
| 35 | 80 | } |
| 36 | 81 | |
| 82 | + /** | |
| 83 | + * @return string | |
| 84 | + */ | |
| 37 | 85 | public function getName(): string |
| 38 | 86 | { |
| 39 | 87 | return $this->name; |
| 40 | 88 | } |
| 41 | 89 | |
| 90 | + /** | |
| 91 | + * @return string | |
| 92 | + */ | |
| 42 | 93 | public function getType(): string |
| 43 | 94 | { |
| 44 | 95 | return $this->type; |
| 45 | 96 | } |
| 46 | 97 | |
| 47 | - public function getDefault(): int|string|null | |
| 98 | + /** | |
| 99 | + * @return int|string | |
| 100 | + */ | |
| 101 | + public function getDefault() | |
| 48 | 102 | { |
| 49 | 103 | return $this->default; |
| 50 | 104 | } |
| 51 | 105 | |
| 106 | + /** | |
| 107 | + * @return bool | |
| 108 | + */ | |
| 52 | 109 | public function isNull(): bool |
| 53 | 110 | { |
| 54 | 111 | return $this->isNull; |
| 55 | 112 | } |
| 56 | 113 | |
| 114 | + /** | |
| 115 | + * @return bool | |
| 116 | + */ | |
| 57 | 117 | public function isKey(): bool |
| 58 | 118 | { |
| 59 | 119 | return $this->isKey; |
| 60 | 120 | } |
| 61 | 121 | |
| 122 | + /** | |
| 123 | + * @return bool | |
| 124 | + */ | |
| 62 | 125 | public function isAutoIncrement(): bool |
| 63 | 126 | { |
| 64 | 127 | return $this->isAutoIncrement; |
| 65 | 128 | } |
| 66 | 129 | |
| 130 | + /** | |
| 131 | + * Returns a mysql column string. | |
| 132 | + * @return string | |
| 133 | + */ | |
| 67 | 134 | public function __toString(): string |
| 68 | 135 | { |
| 69 | 136 | $nullConstraint = ($this->isNull) ? 'NULL' : 'NOT NULL'; |
| 70 | - $type = $this->type === 'INT(11)' ? 'INT' : $this->type; | |
| 71 | - $column = "`$this->name` $type $nullConstraint"; | |
| 137 | + $column = "`{$this->name}` {$this->type} {$nullConstraint}"; | |
| 72 | 138 | |
| 73 | 139 | if ($this->default === null && $this->isNull) { |
| 74 | 140 | $column .= ' DEFAULT NULL'; |
| 75 | 141 | } elseif ($this->default !== null) { |
| 76 | - $defaultValue = is_numeric($this->default) === false ? "'$this->default'" : $this->default; | |
| 77 | - $column .= " DEFAULT $defaultValue"; | |
| 142 | + $defaultValue = is_numeric($this->default) === false ? "'{$this->default}'" : $this->default; | |
| 143 | + $column .= " DEFAULT {$defaultValue}"; | |
| 78 | 144 | } |
| 79 | 145 | |
| 80 | 146 | if ($this->isAutoIncrement) { |
| 81 | 147 | $column .= ' AUTO_INCREMENT'; |