| @@ -1,64 +1,148 @@ | ||
| 1 | 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 | + */ | |
| 2 | 15 | |
| 3 | 16 | declare(strict_types=1); |
| 4 | 17 | |
| 5 | 18 | namespace UserAccessManager\Setup\Database; |
| 6 | 19 | |
| 20 | +/** | |
| 21 | + * Class Column | |
| 22 | + * | |
| 23 | + * @package UserAccessManager\Setup\Database | |
| 24 | + */ | |
| 7 | 25 | class Column |
| 8 | 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 | + */ | |
| 9 | 66 | 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 | |
| 67 | + string $name, | |
| 68 | + string $type, | |
| 69 | + $isNull = false, | |
| 70 | + $default = null, | |
| 71 | + $isKey = false, | |
| 72 | + $isAutoIncrement = false | |
| 16 | 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; | |
| 17 | 80 | } |
| 18 | 81 | |
| 82 | + /** | |
| 83 | + * @return string | |
| 84 | + */ | |
| 19 | 85 | public function getName(): string |
| 20 | 86 | { |
| 21 | 87 | return $this->name; |
| 22 | 88 | } |
| 23 | 89 | |
| 90 | + /** | |
| 91 | + * @return string | |
| 92 | + */ | |
| 24 | 93 | public function getType(): string |
| 25 | 94 | { |
| 26 | 95 | return $this->type; |
| 27 | 96 | } |
| 28 | 97 | |
| 29 | - public function getDefault(): int|string|null | |
| 98 | + /** | |
| 99 | + * @return int|string | |
| 100 | + */ | |
| 101 | + public function getDefault() | |
| 30 | 102 | { |
| 31 | 103 | return $this->default; |
| 32 | 104 | } |
| 33 | 105 | |
| 106 | + /** | |
| 107 | + * @return bool | |
| 108 | + */ | |
| 34 | 109 | public function isNull(): bool |
| 35 | 110 | { |
| 36 | 111 | return $this->isNull; |
| 37 | 112 | } |
| 38 | 113 | |
| 114 | + /** | |
| 115 | + * @return bool | |
| 116 | + */ | |
| 39 | 117 | public function isKey(): bool |
| 40 | 118 | { |
| 41 | 119 | return $this->isKey; |
| 42 | 120 | } |
| 43 | 121 | |
| 122 | + /** | |
| 123 | + * @return bool | |
| 124 | + */ | |
| 44 | 125 | public function isAutoIncrement(): bool |
| 45 | 126 | { |
| 46 | 127 | return $this->isAutoIncrement; |
| 47 | 128 | } |
| 48 | 129 | |
| 130 | + /** | |
| 131 | + * Returns a mysql column string. | |
| 132 | + * @return string | |
| 133 | + */ | |
| 49 | 134 | public function __toString(): string |
| 50 | 135 | { |
| 51 | 136 | $nullConstraint = ($this->isNull) ? 'NULL' : 'NOT NULL'; |
| 52 | - // MySQL reports a declared INT as INT(11); normalise so schema comparison sees no difference. | |
| 53 | 137 | $type = $this->type === 'INT(11)' ? 'INT' : $this->type; |
| 54 | - $column = "`$this->name` $type $nullConstraint"; | |
| 138 | + $column = "`{$this->name}` {$type} {$nullConstraint}"; | |
| 55 | 139 | |
| 56 | 140 | if ($this->default === null && $this->isNull) { |
| 57 | 141 | $column .= ' DEFAULT NULL'; |
| 58 | 142 | } elseif ($this->default !== null) { |
| 59 | - $defaultValue = is_numeric($this->default) === false ? "'$this->default'" : $this->default; | |
| 60 | - $column .= " DEFAULT $defaultValue"; | |
| 143 | + $defaultValue = is_numeric($this->default) === false ? "'{$this->default}'" : $this->default; | |
| 144 | + $column .= " DEFAULT {$defaultValue}"; | |
| 61 | 145 | } |
| 62 | 146 | |
| 63 | 147 | if ($this->isAutoIncrement) { |
| 64 | 148 | $column .= ' AUTO_INCREMENT'; |