PluginProbe
User Access Manager / 2.2.21
User Access Manager v2.2.21
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / Setup / Database / Column.php

Column.php in User Access Manager 2.2.21, at src/Setup/Database/Column.php

154 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /**
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 */
66 public function __construct(
67 string $name,
68 string $type,
69 $isNull = false,
70 $default = null,
71 $isKey = false,
72 $isAutoIncrement = false
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;
80 }
81
82 /**
83 * @return string
84 */
85 public function getName(): string
86 {
87 return $this->name;
88 }
89
90 /**
91 * @return string
92 */
93 public function getType(): string
94 {
95 return $this->type;
96 }
97
98 /**
99 * @return int|string
100 */
101 public function getDefault()
102 {
103 return $this->default;
104 }
105
106 /**
107 * @return bool
108 */
109 public function isNull(): bool
110 {
111 return $this->isNull;
112 }
113
114 /**
115 * @return bool
116 */
117 public function isKey(): bool
118 {
119 return $this->isKey;
120 }
121
122 /**
123 * @return bool
124 */
125 public function isAutoIncrement(): bool
126 {
127 return $this->isAutoIncrement;
128 }
129
130 /**
131 * Returns a mysql column string.
132 * @return string
133 */
134 public function __toString(): string
135 {
136 $nullConstraint = ($this->isNull) ? 'NULL' : 'NOT NULL';
137 $type = $this->type === 'INT(11)' ? 'INT' : $this->type;
138 $column = "`{$this->name}` {$type} {$nullConstraint}";
139
140 if ($this->default === null && $this->isNull) {
141 $column .= ' DEFAULT NULL';
142 } elseif ($this->default !== null) {
143 $defaultValue = is_numeric($this->default) === false ? "'{$this->default}'" : $this->default;
144 $column .= " DEFAULT {$defaultValue}";
145 }
146
147 if ($this->isAutoIncrement) {
148 $column .= ' AUTO_INCREMENT';
149 }
150
151 return $column;
152 }
153 }
154