| 1 |
<?php |
| 2 |
/** |
| 3 |
* Column.php |
| 4 |
* |
| 5 |
* The Column class file. |
| 6 |
* |
| 7 |
* PHP versions 5 |
| 8 |
* |
| 9 |
* @author Alexander Schneider <alexanderschneider85@gmail.com> |
| 10 |
* @copyright 2008-2017 Alexander Schneider |
| 11 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 |
| 12 |
* @version SVN: $id$ |
| 13 |
* @link http://wordpress.org/extend/plugins/user-access-manager/ |
| 14 |
*/ |
| 15 |
|
| 16 |
declare(strict_types=1); |
| 17 |
|
| 18 |
namespace UserAccessManager\Setup\Database; |
| 19 |
|
| 20 |
/** |
| 21 |
* Class Column |
| 22 |
* |
| 23 |
* @package UserAccessManager\Setup\Database |
| 24 |
*/ |
| 25 |
class Column |
| 26 |
{ |
| 27 |
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 |
| 34 |
) { |
| 35 |
} |
| 36 |
|
| 37 |
public function getName(): string |
| 38 |
{ |
| 39 |
return $this->name; |
| 40 |
} |
| 41 |
|
| 42 |
public function getType(): string |
| 43 |
{ |
| 44 |
return $this->type; |
| 45 |
} |
| 46 |
|
| 47 |
public function getDefault(): int|string|null |
| 48 |
{ |
| 49 |
return $this->default; |
| 50 |
} |
| 51 |
|
| 52 |
public function isNull(): bool |
| 53 |
{ |
| 54 |
return $this->isNull; |
| 55 |
} |
| 56 |
|
| 57 |
public function isKey(): bool |
| 58 |
{ |
| 59 |
return $this->isKey; |
| 60 |
} |
| 61 |
|
| 62 |
public function isAutoIncrement(): bool |
| 63 |
{ |
| 64 |
return $this->isAutoIncrement; |
| 65 |
} |
| 66 |
|
| 67 |
public function __toString(): string |
| 68 |
{ |
| 69 |
$nullConstraint = ($this->isNull) ? 'NULL' : 'NOT NULL'; |
| 70 |
$type = $this->type === 'INT(11)' ? 'INT' : $this->type; |
| 71 |
$column = "`$this->name` $type $nullConstraint"; |
| 72 |
|
| 73 |
if ($this->default === null && $this->isNull) { |
| 74 |
$column .= ' DEFAULT NULL'; |
| 75 |
} elseif ($this->default !== null) { |
| 76 |
$defaultValue = is_numeric($this->default) === false ? "'$this->default'" : $this->default; |
| 77 |
$column .= " DEFAULT $defaultValue"; |
| 78 |
} |
| 79 |
|
| 80 |
if ($this->isAutoIncrement) { |
| 81 |
$column .= ' AUTO_INCREMENT'; |
| 82 |
} |
| 83 |
|
| 84 |
return $column; |
| 85 |
} |
| 86 |
} |
| 87 |
|