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